Skip to content

fix(remote-ssh): recover from a closed cached SFTP subsystem - #3705

Merged
bobleer merged 2 commits into
openBitFun:mainfrom
Raing5Days:fix/remote-sftp-session-recovery
Nov 3, 2026
Merged

bobleer merged 2 commits into
openBitFun:mainfrom
Raing5Days:fix/remote-sftp-session-recovery

Conversation

@Raing5Days

Copy link
Copy Markdown
Contributor

Summary

Fixes Failed to stat '<path>': session closed, which made the remote file browser unusable until OpenBitFun was restarted.

Each SSH connection caches one SFTP subsystem. A cached session whose channel has closed keeps failing: russh-sftp answers every request on it with UnexpectedBehavior("session closed") and never reopens the channel, and nothing marked that cache entry unusable. The bounded directory channel already retried on stale-transport errors and open-based transfers already retired the session, but stat, lstat, exists and the plain mutation calls (create_dir, remove_file, remove_dir, rename, set_metadata) kept using the dead cache entry forever. The server side stayed healthy throughout and restarting the client cleared the in-memory cache, which matches the reported symptoms.

Type and Areas

Type: bug fix

Areas: Rust core (services-integrations, remote SSH)

Motivation / Impact

  • ManagedSftpSession now retires itself when a request reports a stale transport, so the manager opens a new subsystem channel on the same SSH transport instead of serving the dead one.
  • Idempotent queries (stat, lstat, exists) replay once on the fresh channel, so the file browser recovers inside the same operation instead of surfacing an error.
  • Mutations are not replayed, because a failed request may already have reached the server; they only leave the cache healthy so the caller's next attempt succeeds.
  • No wire, persisted-shape or configuration change. Error text is unchanged; the only new output is one log::warn! on the retry path.

Verification

  • cargo fmt -p openbitfun-services-integrations - applied
  • cargo test --locked -p openbitfun-services-integrations --no-default-features --features remote-ssh-concrete --lib remote_ssh:: - passed (190 passed, 0 failed, 5 ignored)
  • cargo check --locked -p openbitfun-services-integrations --no-default-features - passed
  • New test a_closed_subsystem_channel_is_replaced_instead_of_failing_forever fails on the previous code with Failed to stat '/file': session closed and passes with the fix.
  • Not covered: a live session against a real SSH server that closes the subsystem channel; the test kills the channel of an in-memory SFTP transport. Hosted CI has not run yet.

Reviewer Notes

  • The replay is limited to one attempt and to errors sftp_error_may_be_stale_transport already classifies as transport failures (NoConnection, ConnectionLost, IO, Timeout, UnexpectedBehavior).
  • Remote scenarios: remote-workspace-only code; no controller, peer-device or dispatch surface changed. A timeout now retires one subsystem channel that the next request reopens on the same transport; session, workspace and permission state are untouched.

Generated with OpenBitFun

Each connection caches one SFTP subsystem that every metadata query,
mutation and file operation reused. russh-sftp answers every request on a
closed subsystem channel with `UnexpectedBehavior("session closed")` and
never reopens it, while nothing marked that cache entry unusable. One
closed channel therefore failed every later file operation with
`Failed to stat '<path>': session closed` until the app restarted, even
though the SSH transport and every command channel kept working.

Retire the managed session when a request reports a stale transport, so
the manager opens a new subsystem channel on the same SSH transport
instead of serving the dead one. Idempotent metadata and existence
queries then replay once; mutations are never replayed, because a failed
request may already have reached the server, and instead leave the cache
healthy for the caller's next attempt. Open-based transfers already
retired on those errors, which is why only the plain queries stayed
stuck.

The regression test kills the channel of a cached session over an
in-memory SFTP transport and asserts that stat, exists and lstat recover
on a new subsystem channel. It fails on the previous code with
`Failed to stat '/file': session closed`.
@Raing5Days
Raing5Days marked this pull request as ready for review November 3, 2026 05:52
… channel

A read or a recursive mkdir that was the first request after the cached
subsystem closed still failed once: the dead session was retired, but the
operation was not replayed. Read-only OPEN and mkdir_all (which accepts
existing directories) are idempotent, so they now replay once on a new
channel like stat, lstat and exists.
@bobleer

bobleer commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Reviewed against current main: the bug is still present there (stat/lstat/exists deref to the raw SftpSession, and nothing retires a closed subsystem). The fix lives in the right layer (services-integrations remote SSH) and matches the existing bounded directory-channel retry, so the approach looks good.

I pushed 4f16b9d to close two remaining gaps in the same scenario:

  • Read-only open (sftp_read, sftp_read_with_progress, open_workspace_file_read) retired the dead session but did not replay, so opening a file right after the channel closed still failed once. It now replays the OPEN once on a fresh channel (no data has reached the caller at that point).
  • sftp_mkdir_all kept using the dead session for every step after the first failure. Every step accepts existing directories, so the whole attempt now replays once.

The killable-channel test setup is factored into helpers, and two new tests cover these cases (both fail without the change). cargo test -p openbitfun-services-integrations --no-default-features --features remote-ssh-concrete --lib remote_ssh:: passes (229 passed).

@bobleer
bobleer merged commit ee8a9f0 into openBitFun:main Nov 3, 2026
13 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.

2 participants