Skip to content

fix(sftp): close persistent SFTP channel on page dispose and guard async race#1173

Merged
GT-610 merged 2 commits into
mainfrom
sftp-continue-fix
May 18, 2026
Merged

fix(sftp): close persistent SFTP channel on page dispose and guard async race#1173
GT-610 merged 2 commits into
mainfrom
sftp-continue-fix

Conversation

@GT-610
Copy link
Copy Markdown
Collaborator

@GT-610 GT-610 commented May 18, 2026

  • Close _status.client in dispose() to prevent channel leak when navigating away from the SFTP browser page.
  • Replace ??= with explicit null check to avoid concurrent double-open race when multiple _listDir calls overlap.
  • Add catch block to close and null-out stale client on non-SftpStatusError, allowing recovery on next call.

Summary by CodeRabbit

  • Bug Fixes
    • Improved SFTP connection cleanup to prevent stale connections from persisting.
    • Coordinated concurrent connection attempts to avoid race conditions when opening sessions.
    • Ensured ongoing connection attempts are cleared and active connections are closed during shutdown.
    • Non-standard errors now trigger a proactive connection reset to avoid leaving a broken session open.

Review Change Stack

…ync race

- Close _status.client in dispose() to prevent channel leak when
  navigating away from the SFTP browser page.
- Replace ??= with explicit null check to avoid concurrent
  double-open race when multiple _listDir calls overlap.
- Add catch block to close and null-out stale client on
  non-SftpStatusError, allowing recovery on next call.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 53001e95-3b1a-49b9-ad25-f675c8073239

📥 Commits

Reviewing files that changed from the base of the PR and between 1b0a73d and 9efc948.

📒 Files selected for processing (1)
  • lib/view/page/storage/sftp.dart
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/view/page/storage/sftp.dart

📝 Walkthrough

Walkthrough

SftpPage adds an _openingClientFuture to coordinate in-flight SFTP session opens. _listDirWithFallback now starts or awaits that future when no client exists, assigns _status.client from it, then clears the future before listing. On non-SftpStatusError exceptions the code closes and nulls _status.client, clears _openingClientFuture, and rethrows. dispose() closes any active client, clears _openingClientFuture, disposes controllers, and calls super.dispose().

Possibly related PRs

Suggested reviewers

  • lollipopkit
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main changes: closing SFTP channel on dispose and handling async race conditions in the session opening logic.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sftp-continue-fix

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot requested a review from lollipopkit May 18, 2026 02:58
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/view/page/storage/sftp.dart`:
- Around line 1038-1043: The code can race when initializing _status.client in
_listDirWithFallback: multiple callers may see _status.client == null and each
call _withSftpOpTimeout(_client.sftp()), creating duplicate sessions; introduce
a single-flight guard (e.g. a private Future<SftpClient>? _openingClientFuture)
that is checked/awaited before starting a new open, assign the resulting client
to _status.client once completed, and clear/reset the guard on success or error;
update the existing initialization logic that calls _withSftpOpTimeout('open
browser session', _client.sftp()) to use this shared in-flight future so only
one _client.sftp() is invoked concurrently.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: da3d3c1d-d977-4cd0-b842-9a2111f4f064

📥 Commits

Reviewing files that changed from the base of the PR and between 2df593c and 1b0a73d.

📒 Files selected for processing (1)
  • lib/view/page/storage/sftp.dart

Comment thread lib/view/page/storage/sftp.dart Outdated
- Introduce _openingClientFuture to prevent multiple concurrent
  _client.sftp() calls when rapid navigation races the lazy
  _status.client initialisation.
- Reset _openingClientFuture on dispose and after errors.
- Add mounted check after awaiting the future so unmounted
  pages do not attempt further UI operations.
@GT-610 GT-610 merged commit 0be0ce7 into main May 18, 2026
3 checks passed
@GT-610 GT-610 deleted the sftp-continue-fix branch May 18, 2026 03:21
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