fix: SFTP server subsystem sends exit-status and EOF before channel close#1492
Open
Luwdo wants to merge 3 commits intomscdex:masterfrom
Open
fix: SFTP server subsystem sends exit-status and EOF before channel close#1492Luwdo wants to merge 3 commits intomscdex:masterfrom
Luwdo wants to merge 3 commits intomscdex:masterfrom
Conversation
…P destroy SFTP.destroy() was sending channelClose without first sending exit-status or channelEOF. This caused SCP clients (which use the SFTP subsystem) to report exit code -1, breaking tools like Mutagen that check exit codes during agent installation. Now matches OpenSSH behavior by sending exitStatus(0) + channelEOF before channelClose when running as a server.
44d1097 to
2eef389
Compare
… state for long enough to be used
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using ssh2 as an SSH server, the server-side SFTP class's destroy() method sends channelClose without first sending exit-status or channelEOF. This causes SCP clients to report exit code -1 (or 0xffffffff), even when the SFTP transfer completes successfully.
Modern OpenSSH's scp uses the SFTP subsystem by default (since OpenSSH 9.0), so this affects any SCP operation through an ssh2-based server.
Root Cause
The SFTP class extends EventEmitter, not Channel. The Channel class handles exit-status properly via its onFinish handler (which calls eof() then close()), but SFTP.destroy() skips straight to channelClose:
In contrast, OpenSSH's sftp-server process exits with code 0 when done, which causes the server to send exit-status(0) → SSH_MSG_CHANNEL_EOF → SSH_MSG_CHANNEL_CLOSE on the channel.
Fix
When running in server mode, send exitStatus(0) and channelEOF before channelClose in destroy():
Testing
Verified with: