Skip to content

fix(server): preserve directory events across reloads - #42632

Open
kernel-oops wants to merge 1 commit into
anomalyco:devfrom
kernel-oops:fix/directory-sse-replacement
Open

fix(server): preserve directory events across reloads#42632
kernel-oops wants to merge 1 commit into
anomalyco:devfrom
kernel-oops:fix/directory-sse-replacement

Conversation

@kernel-oops

@kernel-oopskernel-oops commented Aug 14, 2026

Copy link
Copy Markdown

Issue for this PR

Closes#36495

Type of change

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

What does this PR do?

A directory-scoped /event stream currently ends when its instance is disposed, even though the subscription is keyed by the stable directory and the replacement instance emits to the same process-wide bus. This keeps the stream open across disposal/recreation while still forwarding server.instance.disposed as an informational event.

The bus subscriptions now live for the actual response-body lifetime and are removed when the client socket closes, avoiding a listener leak.

How did you verify your code works?

Added regression tests showing that one connection receives the disposal event and later events from the replacement instance, and that a real Node/Bun socket disconnect returns the global listener count to baseline. The focused tests pass (5 tests), as does the OpenCode typecheck.

Screenshots / recordings

N/A — server SSE behavior only.

Checklist

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

@github-actionsgithub-actionsBot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Aug 14, 2026
@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:

One potentially related PR found:

#39349: fix(app): recover from a silently dead event stream
#39349

This PR may be related because it also addresses event stream issues in the context of the app consuming server events. Your current PR (42632) fixes the server side by keeping directory event streams alive across reloads, while #39349 appears to handle client-side recovery when event streams become unresponsive. They complement each other in improving event stream reliability.

@github-actionsgithub-actionsBot removed the needs:compliance This means the issue will auto-close after 2 hours. label Aug 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@Enough1122

Copy link
Copy Markdown

AI code review — automated review for reference; please use your judgment.

Scope: reworks the SSE /event handler so both subscriptions (event listener + GlobalBus disposed-listener) are acquired inside the response body's scope before the first byte, adds socket-close detection for Node-backed connections, and restructures the stream merge so client disconnects release resources promptly across instance reloads.

  • The core fix is real: previously the disposed-stream registration raced the body fiber startup, and there was no socket-level close detection for the Node server path, so listeners could leak after abrupt client disconnects during reloads. Acquiring everything under Stream.scoped before server.connected closes both holes.
  • The dual-path disconnect handling (Node socket.once("close") vs scope release for in-process Web Requests) is necessarily ugly and honestly commented — good. One nit: socket?.destroyed checked once at setup handles already-dead sockets, but a socket that dies between that check and once("close") registration is covered since registration precedes the check... actually the order is once then destroyed, which is the safe order. 👍
  • Stream topology (merge with haltStrategy: "left"/"right" + take(1) on disconnected) is correct but dense — the new httpapi-event test coverage is what makes this reviewable; make sure it includes an abrupt-disconnect case, not just graceful completion.
  • Minor: event.payload.id ?? eventID() fabricates ids for disposed events lacking them — fine, but synthetic ids mean clients can't dedupe against the durable log; harmless today.
  • Heartbeat interval unchanged (10s); no behavior change there.

Sign up for freeto 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.

GET /event?directory= stream is killed by PATCH /config (instance dispose), starving directory-scoped subscribers

2 participants

@kernel-oops@Enough1122