Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/ToolSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,22 @@ class ToolSocket {
// infoName() keeps the children in sync if the parent is named later
if (!toolsocket.parallelSockets) toolsocket.parallelSockets = [];
toolsocket.parallelSockets.push(parallel);
// Stop tracking a parallel once it closes. Most parallels are one-shot (the
// proxy closes them after a single request), so a list that only ever grew
// pinned a ToolSocket, its WebSocket, Sender, Receiver and ping Timeout per
// request for the life of the process. A parallel that reconnects re-registers
// itself on 'open', mirroring how the NB layer gates its own teardown.
parallel.addEventListener('close', () => {
const index = toolsocket.parallelSockets.indexOf(parallel);
if (index > -1) {
toolsocket.parallelSockets.splice(index, 1);
}
});
parallel.addEventListener('open', () => {
if (!toolsocket.parallelSockets.includes(parallel)) {
toolsocket.parallelSockets.push(parallel);
}
});
if (toolsocket.remoteInfoName) {
parallel.infoName(toolsocket.remoteInfoName + ' · data');
}
Expand Down
25 changes: 25 additions & 0 deletions src/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,29 @@ describe('ToolSocket', () => {

toolSocket.message('test/binary', { message: 'Binary data' }, null, [binaryData1, binaryData2]);
});

test('a closed parallel socket stops being tracked by its parent', (done) => {
const parallel = ToolSocket.makeParallelSocket(toolSocket);

parallel.addEventListener('open', () => {
try {
expect(toolSocket.parallelSockets).toContain(parallel);
} catch (error) {
done(error);
return;
}
parallel.close();
});

parallel.addEventListener('close', () => {
try {
// Parallels are one-shot, so a list that only ever grew pinned the
// whole socket (and its ping timer) once per proxied request
expect(toolSocket.parallelSockets).not.toContain(parallel);
done();
} catch (error) {
done(error);
}
});
});
});
Loading