Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): OpenCode stop no longer hangs when a turn is interrupted before submission #12003

MergedSunkenInTime(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/12003/headmerged by shivamhwp(GitHub profile, opens in a new tab) View on GitHub

Fixes #11889

What Changed

OpenCodeAdapter.sendTurn now settles promptAdmission.submissionSettled in an Effect.ensuring finalizer on the admission body, so the deferred resolves no matter how the body exits.

context.promptSemaphore.withPermit(
  Effect.gen(function* () { /* unchanged */ }).pipe(
    Effect.ensuring(/* Deferred.succeed(context.promptAdmission?.submissionSettled) */),
  ),
);

Test: stopSession completes after sendTurn is interrupted before the prompt is submitted gates the Clock so sendTurn is parked right after it publishes the admission (in updateProviderSession's nowIso), interrupts it there, then asserts stopSession finishes and the session is gone. It hangs indefinitely without the fix and passes with it.

Why

sendTurn publishes context.promptAdmission and only settles submissionSettled later — at the early-cancel check or in the forked prompt fiber's onExit. Between those points there are yield points (updateProviderSession, emit). If the caller fiber is interrupted in that window (e.g. the turn start is torn down while a thread is being deleted), the deferred is never settled. stopSession → stopOpenCodeContext → cancelPendingOpenCodePrompt then waits on it forever.

That hang sits inside the thread-deletion worker. ws.ts fences every thread.create on ThreadDeletionReactor.drainThrough, so one stuck OpenCode teardown stops every new chat until the server restarts — the symptom reported in #11889. Settling the deferred on every exit path is the smallest change that removes the pin; the deletion fence itself is left as designed.

Reproduce

This is a fiber-interruption race, so there is no deterministic UI or screenshot repro. The regression test is the reproduction:

  1. Check out main, apply only the test from this PR.
  2. vp test run apps/server/src/provider/Layers/OpenCodeAdapter.test.ts -t "interrupted before the prompt is submitted" — the test never completes (stopSession blocks on submissionSettled).
  3. Apply the adapter change — the test passes (111 passed).

In-app the symptom is: delete an OpenCode thread whose turn was being started, then every subsequent "New chat" spins until restart.

UI Changes

Not applicable (server-only; no deterministic UI reproduction).

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes (N/A)
  • I included a video for animation/interaction changes

Verified with vp test run apps/server/src/provider/Layers/OpenCodeAdapter.test.ts (111 passed), server tsc --noEmit, and vp lint on the two touched files.

Written by Claude (Devin harness).

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where stopping a session could remain stuck if an in-progress message submission was interrupted.
    • Session stopping now completes after an interrupted submission, rather than waiting indefinitely for the submission to settle.
  • Tests

    • Added coverage for stopping a session when a message submission is interrupted while waiting to be sent.
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. Approvability

      Verdict: Approved at 8b9b3c9

      Macroscope's review found this PR approvable — This is a small, self-contained server bug fix that settles the pending OpenCode admission during interrupted turns, preventing teardown hangs. A targeted regression test covers the race, with no product-default, schema, deployment, security, or static-analysis changes.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml

      Review profile: CHILL

      Plan: Advanced

      Run ID: 5a6b38c9-77fa-42ea-8b0f-5ee75435da5f

      📥 Commits

      Reviewing files that changed from the base of the PR and between 8b9b3c91d6cac27c7aa51bae7b4a94cbde616a11 and bc7a6a028369ff518e0533f0f2422949c48f3f74.

      📒 Files selected for processing (2)
      • apps/server/src/provider/Layers/OpenCodeAdapter.test.ts
      • apps/server/src/provider/Layers/OpenCodeAdapter.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


      📝 Walkthrough

      Walkthrough

      sendTurn now resolves the pending prompt-submission signal when its submission flow exits, including on interruption. A regression test interrupts a pre-submit send and verifies that stopSession succeeds and removes the session.

      Changes

      OpenCode shutdown

      Layer / File(s)Summary
      Prompt submission settlement and regression coverage
      apps/server/src/provider/Layers/OpenCodeAdapter.ts, apps/server/src/provider/Layers/OpenCodeAdapter.test.ts
      sendTurn resolves submissionSettled in an Effect.ensuring finalizer. The test gates a clock read, interrupts a pre-submit send, and verifies that session shutdown succeeds and removes the session.

      Priority: ➖ Normal

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Suggested reviewers: t3dotgg

      Merge Risk: ⚪ Minimal · up to bc7a6

      The targeted interruption should no longer leave session shutdown waiting. No actionable merge-blocking risk is established for this change.

      Security Architecture Review

      Security architecture risk: 🔵 Low · up to bc7a6

      The change addresses a shutdown hang without adding a new access path. A separate command-completion path may settle earlier than before, but the shutdown path still interrupts the active command and closes its session resources.

      Retained concerns No architecture-level concerns identified.

      Security review details

      Security Blast Radius

      • inferred — The relevant exposure is availability of OpenCode session teardown and work waiting on that teardown. The examined change does not establish expanded tenant, credential, or provider-command authority.

      Trust Boundaries and Controls

      • observed — Existing sendTurn checks bind model selection to the adapter instance before admission. The new settlement step operates after that check and contains no authorization or external-provider call.

      Resilience and Maintainability Implications

      • observed — The regression test verifies interruption before submission followed by successful stop and session removal; it does not establish the ordering of native-command receipt, fiber exit, and stop.

      Hardening Proposals

      • proposed — Exercise shutdown after a native command’s message receipt but before its prompt fiber exits, to confirm interruption and resource cleanup still complete despite earlier settlement.
      🚥 Pre-merge checks | ✅ 3 | ❌ 2

      ❌ Failed checks (2 warnings)

      Check nameStatusExplanationResolution
      Linked Issues check⚠️ WarningThe change fixes the reported pre-submission interruption path. sendTurn now settles promptAdmission.submissionSettled, and the regression test verifies that stopSession completes and removes th…Add bounded handling for the remaining provider teardown wait, or otherwise ensure deletion cannot block unrelated thread creation. Add tests for reuse of a thread ID waiting for its cleanup and for an unrelated chat completing while anothe…
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (3 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the server bug fix: preventing OpenCode stop operations from hanging when a turn is interrupted before submission.
      Description check✅ PassedThe description includes the required What Changed, Why, UI Changes, and Checklist sections. It explains the interruption race, the implementation, the regression test, reproduction steps, validation …
      Out of Scope Changes check✅ PassedThe changes are limited to OpenCodeAdapter.sendTurn and its regression test. They directly address the prompt-admission cancellation race described in issue #11889. No unrelated production behavior …
      Full details: Linked Issues check

      Explanation

      The change fixes the reported pre-submission interruption path. sendTurn now settles promptAdmission.submissionSettled, and the regression test verifies that stopSession completes and removes the session. Issue #11889 also requires provider teardown to finish or fail within a bounded period and a stuck deletion to allow unrelated chats to start. The PR leaves cancelPendingOpenCodePrompt without a timeout and leaves the shared ThreadDeletionReactor.drainThrough barrier unchanged. The PR does not provide regression coverage for unrelated chat creation or the other cancellation timing path.

      Resolution

      Add bounded handling for the remaining provider teardown wait, or otherwise ensure deletion cannot block unrelated thread creation. Add tests for reuse of a thread ID waiting for its cleanup and for an unrelated chat completing while another deletion is stuck. Preserve the existing interrupted pre-submission regression test.

      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR

      Comment `@coderabbitai help` to get the list of available commands.
    4. Note: GPT-6 on behalf of shivam (@shivamhwp).

      This fixes the interrupted-send cancellation race behind #11889. The remaining gap is that ThreadDeletionReactor.drainThrough still waits for all deletions, so cleanup stuck for another reason can block unrelated new chats.

      Could we make that wait specific to the thread being created? Reusing the same thread ID should still wait for its previous cleanup, while unrelated chats should proceed. Add regression coverage for both behaviors.

      This could be a separate follow-up PR to keep your cancellation fix focused.

Sign in to join this conversation.