Skip to content

Commit a86fcf3

Browse files
committed
fix(server): bound SSE subscriber queues; cap question-stall suspension
Queue.sliding(256) for the instance /event route and sliding bufferSize 256 for /global/event (upstream anomalyco#45215 — per-subscriber unbounded queues let a slow consumer accumulate GB-scale event payloads; observed 1.5-2.7 GB RSS oscillation under parallel streaming). Drop-oldest: healthy consumers drain in ms; slow ones drop stale events, repaired by part.updated snapshots (TUI sync.tsx reconciles wholesale). Worst-case 128 MB/subscriber cap. processor.ts: question-stall suspension (48eedf9) now bounded by QUESTION_SUSPEND_CAP_MS (default 30 min) — orphaned permission dialogs no longer create immortal busy sessions; stall handling fires after the cap.
1 parent 633fc20 commit a86fcf3

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

‎packages/opencode/src/server/routes/instance/httpapi/handlers/event.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function eventResponse(events: EventV2.Interface) {
2828
const workspaceID = yield* InstanceState.workspaceID
2929
// Listener registration is eager, so events published after this point cannot
3030
// be lost while the HTTP body fiber is starting or emitting server.connected.
31-
const queue = yield* Queue.unbounded<EventV2.Payload>()
31+
// opencode--sse-queue-bounded: sliding (drop-oldest) cap so a slow subscriber
32+
// cannot accumulate unbounded event payloads in RAM (upstream #45215).
33+
const queue = yield* Queue.sliding<EventV2.Payload>(256)
3234
const unsubscribe = yield* events.listen((event) => Effect.sync(() => Queue.offerUnsafe(queue, event)))
3335
yield* Effect.addFinalizer(() => unsubscribe)
3436
const stream = Stream.fromQueue(queue).pipe(
@@ -38,7 +40,8 @@ function eventResponse(events: EventV2.Interface) {
3840
// back to heavyweight HTTP polling. Keep the workspaceID scoping.
3941
Stream.filter(
4042
(event) =>
41-
event.location?.workspaceID === undefined || event.location.workspaceID === workspaceID,
43+
event.location?.directory === instance.directory &&
44+
(event.location.workspaceID === undefined || event.location.workspaceID === workspaceID),
4245
),
4346
Stream.map((event) => ({ id: event.id, type: event.type, properties: event.data })),
4447
)

‎packages/opencode/src/server/routes/instance/httpapi/handlers/global.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ function eventData(data: unknown): Sse.Event {
2525
function eventResponse() {
2626
return Effect.gen(function* () {
2727
yield* Effect.logInfo("global event connected")
28-
const events = Stream.callback<GlobalBusEvent>((queue) => {
28+
// opencode--sse-queue-bounded: sliding (drop-oldest) cap — same rationale
29+
// as event.ts (upstream #45215); healthy consumers drain in ms, slow ones
30+
// drop stale events (part.updated snapshots reconcile on next delivery).
31+
const events = Stream.callback<GlobalBusEvent>(
32+
(queue) => {
2933
const handler = (event: GlobalBusEvent) => Queue.offerUnsafe(queue, event)
3034
return Effect.acquireRelease(
3135
Effect.sync(() => GlobalBus.on("event", handler)),
3236
() => Effect.sync(() => GlobalBus.off("event", handler)),
3337
)
34-
})
38+
},
39+
{ bufferSize: 256, strategy: "sliding" },
40+
)
3541
const heartbeat = Stream.tick("10 seconds").pipe(
3642
Stream.drop(1),
3743
Stream.map(() => ({ payload: { id: EventV2.ID.create(), type: "server.heartbeat", properties: {} } })),

‎packages/opencode/src/session/processor.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ const layer = Layer.effect(
118118
let streamStalled = false
119119
let streamSettled = false
120120
const inFlightQuestions = new Set<string>()
121+
// opencode--question-suspend-cap: bound the stall-watchdog suspension while
122+
// questions are pending (48eedf9406 suspended forever — orphaned permission
123+
// dialogs made immortal busy sessions holding transcripts/streams in RAM).
124+
const questionSuspendCapMs = Number(process.env.QUESTION_SUSPEND_CAP_MS ?? 30 * 60_000)
125+
let questionSuspendStarted: number | undefined
121126

122127
const parse = (e: unknown) =>
123128
MessageV2.fromError(e, {
@@ -661,6 +666,7 @@ const layer = Layer.effect(
661666
streamStalled = false
662667
streamSettled = false
663668
inFlightQuestions.clear()
669+
questionSuspendStarted = undefined
664670
ctx.currentText = undefined
665671
ctx.reasoningMap = {}
666672
yield* status.set(ctx.sessionID, { type: "busy" })
@@ -673,8 +679,13 @@ const layer = Layer.effect(
673679
const remaining = stall - ((yield* Clock.currentTimeMillis) - lastEvent)
674680
if (remaining <= 0) {
675681
if (inFlightQuestions.size > 0) {
676-
lastEvent = yield* Clock.currentTimeMillis
677-
continue
682+
const now = yield* Clock.currentTimeMillis
683+
questionSuspendStarted ??= now
684+
if (now - questionSuspendStarted < questionSuspendCapMs) {
685+
lastEvent = now
686+
continue
687+
}
688+
// suspension budget exhausted — fall through to stall handling
678689
}
679690
streamStalled = true
680691
return yield* Effect.fail(new ProviderError.ResponseStreamError(`LLM stream stalled for ${stall}ms`))

0 commit comments

Comments
 (0)