Skip to content

fix(server): release SSE streams when a Bun client disconnects - #51835

Closed
Veld101 wants to merge 1 commit into
anomalyco:devfrom
Veld101:sse-bun-disconnect
Closed

Veld101 wants to merge 1 commit into
anomalyco:devfrom
Veld101:sse-bun-disconnect

Conversation

@Veld101

@Veld101 Veld101 commented Sep 28, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #36311

Supersedes #50375 (that one targets dev; the same missing bridge is present on v2).

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

On Bun 1.3.14, node:http does not emit ServerResponse "close" when an SSE client disconnects (oven-sh/bun#14697). @effect/platform-node waits for that event to interrupt the request fiber, so when a TUI drops its /api/event SSE connection the server-side event stream is never released. It keeps writing heartbeats into a dead socket, pinning one CPU core at 100% and making the server unresponsive.

packages/server/src/process.ts is the v2 equivalent of the dev call site the issue describes. This adds the bridge the issue suggests: on Bun only, destroy the response from the request "aborted" event so Bun emits the missing "close" and Effect can interrupt the stream and release its subscription.

The bridge is a small named function (bridgeSseDisconnect) next to bind so its two guards — Bun only, and only for a response that has not already ended — are directly testable.

How did you verify your code works?

  • bun test test/process.test.ts from packages/server — 4 pass. Two new cases drive a fake "request" emission and then "aborted": an unfinished response is destroyed, an already-ended response is left alone. The existing live server tests still pass.
  • bun typecheck in packages/server — passed.
  • bun run lint — 0 warnings / 0 errors.

The original report notes there is no short deterministic trigger for the permanent spin, so this is covered at the event-bridge level rather than by reproducing the spin end to end.

Note: bun run check still cannot complete in my Windows checkout because packages/app/src/custom-elements.d.ts is a git symlink (mode 120000) materialized as a text file (core.symlinks=false), which fails @opencode/app#typecheck with TS1128. That is unrelated to this change and passes on a normal checkout.

Screenshots / recordings

Not applicable; no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@Veld101
Veld101 force-pushed the sse-bun-disconnect branch from 24679ef to 8794989 Compare October 12, 2026 14:30
@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2.0: standalone SSE disconnect can wedge Bun server at 100% CPU

1 participant