Skip to content

Free SFTP sessions and their open files at teardown - #292

Merged
5cript merged 2 commits into
mainfrom
fix/free-sftp-sessions-at-teardown
Nov 2, 2026
Merged

5cript merged 2 commits into
mainfrom
fix/free-sftp-sessions-at-teardown

Conversation

@5cript

@5cript 5cript commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Closing an SftpSession now frees its libssh session and closes the remote files still open on it. Both leaked when a session was torn down, which is the only place SFTP sessions are closed.

  • SftpSession owns its libssh session as SftpSession::NativeSession from sftp_new on and frees it in the final task of close, on the processing thread.
  • Files still open then are closed there with sftp_close. Before, their close was posted to the strand that close had already finalized, which refused it.
  • After close, lastError reports SSH_FX_CONNECTION_LOST, and the path methods and supportsExtensionInStrand refuse the closed session instead of handing libssh a null session.
  • Closing an open file waits for the server's answer, so on a stalled connection teardown can wait up to the SSH timeout once per open file, and without limit when connectTimeoutSeconds is unset.
  • New tests ClosedSftpSessionEndsOnTheServerWhileTheConnectionStays, ClosingTheSftpSessionClosesItsOpenFilesOnTheServer and LastErrorOfAClosedSftpSessionIsAConnectionLoss. The mock SFTP server can report and wait for its number of open SFTP sessions.

…ng them

- SftpSession owns the libssh session from sftp_new on and frees it in the final task of close, on the processing
  thread. Before, close only dropped the object and the libssh session leaked.
- Files still open then are closed there too: their deleter posted sftp_close to the finalized strand, which refused it.
- Close is only reached at teardown today, the SFTP channel lives for the whole session.
- The mock SFTP server counts its open SFTP sessions and can wait for a count; its CLI acknowledges promises.
- Its libssh session is null after close. lastError, the home directory fallback, requireSetInStrand, which every path
  method runs first, and supportsExtensionInStrand check for it: sftp_open, sftp_unlink and others dereference it.
- Unreachable today, the finalized strand refuses tasks after close.
@5cript
5cript enabled auto-merge (rebase) November 2, 2026 22:53
@5cript
5cript merged commit 09b884e into main Nov 2, 2026
3 checks passed
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