Skip to content

Connection: forcibly close stream if close() is called while the connection is blocked #744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Connection extends EventEmitter {
this.recvSinceLastCheck = false;

this.expectSocketClose = false;
this.blocked = false;
this.freeChannels = new BitSet();
this.channels = [{
channel: { accept: channel0(this) },
Expand Down Expand Up @@ -317,6 +318,18 @@ class Connection extends EventEmitter {
// ignored. We also have to shut down all the channels.
toClosing (capturedStack, k) {
var send = this.sendMethod.bind(this);
invalidateSend(this, 'Connection closing', capturedStack);

if(this.blocked) {
// if the connection is blocked, we will not receive a close-ok until the connection
// becomes unblocked, which may be some time. So just terminate the connection
// immediately
if (k)
k();
var s = stackCapture('Forcibly closing as connection is blocked');
this.toClosed(s, undefined);
return;
}

this.accept = function (f) {
if (f.id === defs.ConnectionCloseOk) {
Expand All @@ -330,7 +343,6 @@ class Connection extends EventEmitter {
}
// else ignore frame
};
invalidateSend(this, 'Connection closing', capturedStack);
}

_closeChannels (capturedStack) {
Expand Down Expand Up @@ -358,6 +370,7 @@ class Connection extends EventEmitter {
// This is certainly true now, if it wasn't before
this.expectSocketClose = true;
this.stream.end();
this.stream.destroy();
this.emit('close', maybeErr);
}

Expand Down Expand Up @@ -606,9 +619,11 @@ function channel0(connection) {
connection.toClosed(s, e);
}
else if (f.id === defs.ConnectionBlocked) {
connection.blocked = true;
connection.emit('blocked', f.fields.reason);
}
else if (f.id === defs.ConnectionUnblocked) {
connection.blocked = false;
connection.emit('unblocked');
}
else {
Expand Down