Skip to content

fix(tui): quit when the closing session fails to retire - #699

Merged
mohitranka merged 1 commit into
mainfrom
fix/quit-deferred-cleanup
Sep 15, 2026
Merged

mohitranka merged 1 commit into
mainfrom
fix/quit-deferred-cleanup

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

Root cause

/quit closed the selected session first and only exited when that close
reported success (CommandFollowUp::Quit in crates/forge-tui/src/app/input.rs,
documented as "Quit once the selected session's CloseSession succeeds").
There was no failure branch, so a failed close was dropped and
run_loop's while !app.exit.is_requested() kept spinning — an unquittable
session.

The close fails deterministically past ~5s because retirement is time-boxed:

  • crates/forge-session/src/supervisor.rs:2418 — 5s for the driver to clear
  • crates/forge-session/src/supervisor.rs:2432 — 5s for the session lock
  • crates/forge-session/src/supervisor.rs:2445 — 6s for retire_resources
  • crates/forge-core/src/background.rs:704 — 5s deadline for background tasks

A background shell, subagent, or in-flight provider call that outlives that
window makes CloseSession fail, which then vetoed the quit.

Supporting evidence that the coupling is incidental, not required: Ctrl+D
(crates/forge-tui/src/app/workspace.rs:152) exits directly without closing a
session, so it never hangs.

Fix

Treat the close as cleanup, not a precondition for quitting:

  • On the last session, CommandFollowUp::Quit now exits on failure too, through
    TuiApp::request_quit_after_deferred_cleanup.
  • /quit takes the same fallback when the close cannot even be enqueued
    (SupervisorHandle::submit returns Full/Closed).
  • With a sibling still active, a failed close keeps the app running as before.

Unfinished cleanup is safe: the supervisor's reconcile_worktrees already runs
on the next launch.

Tests

  • quit_exits_even_when_the_last_session_close_fails — fails on the old logic
    ("a failed close on the last session must still quit"), passes now.
  • quit_with_a_sibling_stays_running_when_the_close_fails

Both inject a failed close reply into the tracked completion, which is exactly
what the retirement deadline produces.

  • cargo test --package forge-tui --locked app::tests::multi_task — 38 passed
  • cargo test --package forge-tui --locked app::tests::commands — 69 passed
  • cargo fmt --all -- --check clean
  • cargo clippy --package forge-tui --all-targets --locked — no errors

Not in this PR

  • The 5s retirement deadlines themselves are unchanged; a slow drain can still
    block RemoveManagedWorktree.
  • Shutdown uses the same deadlines and propagates the failure, so a slow drain
    can still surface as a TuiError on exit rather than a clean quit.

`/quit` closed the selected session first and only exited when that close reported success. Retirement is best-effort and time-boxed (5s for the driver to stop, 5s for the session lock, 5-6s for resource retirement), so a workspace that takes longer than that to release makes `CloseSession` fail. The `Quit` follow-up had no failure branch, so the reply was dropped on the floor and `run_loop` kept spinning: the session could not be quit at all.

Exit once the last session's close has resolved either way, and take the same fallback when the close cannot even be queued. Leaving cleanup unfinished is safe because the supervisor reconciles leftover actors and worktrees on the next launch. With a sibling still active, a failed close keeps the app running as before.
@mohitranka
mohitranka merged commit 4ff868a into main Sep 15, 2026
3 checks passed
@mohitranka
mohitranka deleted the fix/quit-deferred-cleanup branch September 15, 2026 15:07
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