fix(bus): acquire PubSub subscription eagerly to close /event race (#27959)

This commit is contained in:
Kit Langton
2026-05-18 11:38:05 -04:00
committed by GitHub
parent 5bfd7fd16c
commit cb35493242
9 changed files with 659 additions and 43 deletions

View File

@@ -20,10 +20,11 @@ function eventData(data: unknown): Sse.Event {
function eventResponse(bus: Bus.Interface) {
return Effect.gen(function* () {
const context = yield* Effect.context()
const events = bus.subscribeAll().pipe(
Stream.provideContext(context),
// Subscribe eagerly: the bus subscription is acquired in the request scope
// at this yield, so any publish from now on is queued for the body-pump
// fiber to drain — closing the race where Stream.concat(server.connected,
// lazy-subscribe) used to drop publishes in the prefix-consume window.
const events = (yield* bus.subscribeAll()).pipe(
Stream.takeUntil((event) => event.type === Bus.InstanceDisposed.type),
)
const heartbeat = Stream.tick("10 seconds").pipe(