Skip to content

Commit

Permalink
lib: fix server channel cleanup for bare sessions
Browse files Browse the repository at this point in the history
Fixes: #1050
  • Loading branch information
mscdex committed Oct 10, 2021
1 parent d4becc9 commit 983a9de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class Session extends EventEmitter {

this.type = 'session';
this.subtype = undefined;
this.server = true;
this._ending = false;
this._channel = undefined;
this._chanInfo = {
Expand Down Expand Up @@ -556,6 +557,8 @@ class Client extends EventEmitter {
reason = CHANNEL_OPEN_FAILURE.CONNECT_FAILED;
}

if (localChan !== -1)
this._chanMgr.remove(localChan);
proto.channelOpenFail(info.sender, reason, '');
};
const reserveChannel = () => {
Expand Down
16 changes: 10 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ function onCHANNEL_CLOSE(self, recipient, channel, err, dead) {
onChannelOpenFailure(self, recipient, err, channel);
return;
}
if (typeof channel !== 'object'
|| channel === null
|| channel.incoming.state === 'closed') {

if (typeof channel !== 'object' || channel === null)
return;

if (channel.incoming && channel.incoming.state === 'closed')
return;

self._chanMgr.remove(recipient);

if (channel.server && channel.constructor.name === 'Session')
return;
}

channel.incoming.state = 'closed';

Expand All @@ -58,8 +64,6 @@ function onCHANNEL_CLOSE(self, recipient, channel, err, dead) {
if (channel.outgoing.state === 'closing')
channel.outgoing.state = 'closed';

self._chanMgr.remove(recipient);

const readState = channel._readableState;
const writeState = channel._writableState;
if (writeState && !writeState.ending && !writeState.finished && !dead)
Expand Down
22 changes: 22 additions & 0 deletions test/test-misc-client-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1424,3 +1424,25 @@ const setup = setupSimple.bind(undefined, debug);
}));
server.injectSocket(socket);
}

{
const { client, server } = setup(
'Server should not error when cleaning up client bare session channels'
);

server.on('connection', mustCall((conn) => {
conn.on('session', mustCall((accept, reject) => {
accept().on('exec', mustCall((accept, reject, info) => {
assert(info.command === 'uptime',
`Wrong exec command: ${info.command}`);
client.end();
}));
}));
}));

client.on('ready', mustCall(() => {
client.exec('uptime', mustCall((err) => {
assert(err instanceof Error);
}));
}));
}

0 comments on commit 983a9de

Please sign in to comment.