Skip to content

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

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

Veld101 wants to merge 1 commit into
anomalyco:v2from
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:

This branch has not been deployed

No deployments
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.

1 participant