Skip to content

Channel: disable autoDestroy so half-open channels wait for the server's close - #1532

Open
froitag wants to merge 1 commit into
mscdex:masterfrom
froitag:channel-no-autodestroy
Open

froitag wants to merge 1 commit into
mscdex:masterfrom
froitag:channel-no-autodestroy

Conversation

@froitag

@froitag froitag commented Nov 23, 2026

Copy link
Copy Markdown

Since Node 14, Duplex streams default to autoDestroy: true: once both the readable and the writable side have ended, the stream is destroyed. Channel#destroy() calls close(), so a client channel sends CHANNEL_CLOSE as soon as both of these have happened:

  • the client ended its side (for example stream.end(stdin) on an exec channel), and
  • the server's EOF arrived.

That happens even though the channel is half-open (allowHalfOpen), and onFinish deliberately sends only EOF.

A server may send exit-status after its EOF. OpenSSH does this whenever the command's output closes before the command has been reaped. Once the client has closed the channel, nothing more can be delivered, so the exit status is lost: 'exit' never fires and 'close' gets no code. Over a fast link this happens intermittently in practice: a few percent of short commands ended with no exit code.

This passes autoDestroy: false in Channel's stream options, the same fix #1009 made for the SFTP read and write streams. The channel then ends with the server's CHANNEL_CLOSE, which ssh2 already answers, or an explicit close() or destroy().

The new test in test/test-exec.js has a server send data and EOF, then the exit status 100 ms later. It fails without the change (Wrong close args: [ undefined ]) and passes with it. The rest of the suite is unchanged; locally only the OpenSSH integration test for old RSA keys fails, with and without this change.

…r's close

Since Node 14, Duplex streams default to autoDestroy, which destroys the
stream once both its readable and writable sides have ended. Channel's
destroy() sends CHANNEL_CLOSE, so a client channel was closed as soon as
the server's EOF arrived after the client ended its side, despite
allowHalfOpen. A server may send exit-status after its EOF (OpenSSH does
when the command's output closes before it is reaped); that status was
then lost and 'exit' never fired, with 'close' getting no code.

This is the same Node 14 change already handled for the SFTP streams.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant