fix(provider): handle SSE reader cancel rejections - #44912
AlexanderWillner wants to merge 2 commits into
Conversation
When the chunk-timeout fires, the fetch-body reader is cancelled after the request signal is aborted. Under Bun 1.4 reader.cancel() then rejects, and the voided promise surfaces as an unhandled rejection that fails tests (and can crash strict runtimes). Handle the rejection explicitly.
The 5s deadline is load-sensitive: under a fully parallel test run the PTY exit event was observed too late and the test failed even though the behaviour under test is correct.
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: No duplicate PRs found. The search identified several related but distinct PRs addressing different aspects of SSE stream handling (PRs #39970, #43607, #40010, #39516), but none of them are duplicates of PR #44912. This PR specifically addresses unhandled promise rejections from |
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
…d AbortError #attemptConfigApply ends every directory's subscription (#stopPumps aborts its owner controller) before it SIGTERMs the server. The opencode SDK's SSE client answers that abort with `void reader.cancel()` (@opencode-ai/sdk 1.18.18, dist/gen/core/serverSentEvents.gen.js:29, createSseClient's abortHandler). The abort has already errored the fetch body with the abort's reason, so under Bun 1.4 that cancel rejects with the very AbortError #stopPumps created, and nothing observes it: every config apply logged an unhandledRejection (stack abort <- #stopPumps <- #attemptConfigApply), and a process without a handler went down. The SDK is patched where the promise is made: the cancel is observed, a rejection carrying the subscriber's own abort reason is the expected end of that subscription, and any other rejection is rethrown, so it surfaces exactly as before. Real connection errors reach the pump through reader.read() as they always did. Both dist copies carry the handler, so both are patched. 1.18.33, the latest release, still has the unobserved cancel, so no upgrade fixes it (same defect: anomalyco/opencode#44912, getpaseo/paseo#2027). Repro: the real OpencodeHarness against an isolated opencode, restarted by a config change. Before: an unhandled AbortError at the restart, then one new subscription. After: none; the process survives, with one subscription to the new server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Issue for this PR
Closes #44943
Type of change
What does this PR do?
When the SSE chunk-timeout fires, the fetch-body reader is cancelled after the request signal is aborted. Under Bun 1.4 reader.cancel() then rejects and the voided promise surfaces as an unhandled rejection, which fails the running test even though its assertions passed. This handles the cancellation explicitly in both wrapSSE implementations (packages/opencode/src/provider/provider.ts, packages/core/src/aisdk.ts).
Also relaxes one PTY test's exit-poll deadline from 5s to 20s: under a fully parallel suite run the PTY exit event was observed at ~5.3s, so the poll deadline was exceeded despite correct behaviour. Assertion unchanged.
How did you verify your code works?
Full suite from packages/opencode under Bun 1.4.0 and Bun 1.3.14: 3339 tests, 0 fail with these changes (before: one deterministic failure under 1.4.0, one load-dependent PTY failure). tsgo typecheck clean. darwin-arm64 binaries built with both runtimes, smoke tests pass.
Checklist