Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: bound app event backlog

ogsamratf64e16c1e6ed
diff --git a/apps/server/src/engine-event-heartbeat.e2e.test.ts b/apps/server/src/engine-event-heartbeat.e2e.test.ts
index 1262a5d7..423987fb 100644
--- a/apps/server/src/engine-event-heartbeat.e2e.test.ts
+++ b/apps/server/src/engine-event-heartbeat.e2e.test.ts
@@ -2,6 +2,7 @@ import { afterEach, describe, expect, test } from "bun:test";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
+import { connect } from "node:net";
import { clearEnginePoolForConfig, computeEngineConfigFingerprint, type EngineSpawnTemplate } from "./engine-pool.js";
import type { ManagedOpencodeServer } from "./managed-opencode.js";
@@ -37,12 +38,15 @@ function setEnv(name: string, value: string): void {
process.env[name] = value;
}
-function startMockEngine() {
+function startMockEngine(eventBody?: () => ReadableStream<Uint8Array>) {
const server = Bun.serve({
hostname: "127.0.0.1",
port: 0,
fetch(request) {
const url = new URL(request.url);
+ if (url.pathname === "/event" && eventBody) {
+ return new Response(eventBody(), { headers: { "content-type": "text/event-stream" } });
+ }
if (url.pathname === "/event") {
// Held open and silent like a real engine between task events; ends
// when the proxy aborts it after its client disconnects.
@@ -171,4 +175,38 @@ describe("engine event stream heartbeat", () => {
expect(heartbeats).toBeGreaterThanOrEqual(2);
expect(buffered).not.toContain(": ping");
}, 20_000);
+
+ test("closes the stream of a client that stopped reading instead of queueing every event", async () => {
+ const root = await mkdtemp(join(tmpdir(), "omnirush-event-backlog-"));
+ roots.push(root);
+ await mkdir(join(root, ".opencode"), { recursive: true });
+ const frame = new TextEncoder().encode(`data: ${JSON.stringify({ type: "message.part.updated", properties: { text: "x".repeat(1024 * 1024) } })}\n\n`);
+ let sent = 0;
+ const engine = startMockEngine(() => new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (sent < 200) { sent += 1; controller.enqueue(frame); }
+ },
+ }));
+ const omnirush = await startPooledOmniRushServer({ root, engineUrl: engine.url });
+ const warnings: string[] = [];
+ const warn = console.warn;
+ console.warn = (...args: unknown[]) => { warnings.push(args.map(String).join(" ")); };
+ // A window that never reads (frozen): its socket is paused after the request.
+ const port = Number(new URL(omnirush.base).port);
+ const socket = connect(port, "127.0.0.1", () => {
+ socket.write(`GET /workspace/ws_heartbeat/opencode/event HTTP/1.1\r\nhost: x\r\nauthorization: Bearer ${omnirush.token}\r\naccept: text/event-stream\r\n\r\n`);
+ socket.pause();
+ });
+ socket.on("error", () => undefined);
+ try {
+ const deadline = Date.now() + 15_000;
+ while (Date.now() < deadline && !warnings.some((line) => line.includes("client stopped reading"))) await Bun.sleep(50);
+ expect(warnings.some((line) => line.includes("client stopped reading"))).toBe(true);
+ // Cut near the cap, not after the whole turn's events were queued.
+ expect(sent).toBeLessThan(200);
+ } finally {
+ console.warn = warn;
+ socket.destroy();
+ }
+ }, 30_000);
});
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index c02d9279..3795640f 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -2984,6 +2984,11 @@ function parseSsePayload(frame: string): unknown {
}
}
+// What the merged event stream may hold unread for its client. A window that
+// stops reading (frozen, suspended) otherwise keeps every engine event of a long
+// turn in this process until it runs out of memory.
+const ENGINE_EVENT_BACKLOG_BYTES = 64 * 1024 * 1024;
+
function mergedEventBody(input: {
pool: EnginePool;
streams: Array<{ connection: EnginePoolConnection; body: ReadableStream<Uint8Array> }>;
@@ -3011,6 +3016,19 @@ function mergedEventBody(input: {
for (const entry of readers) void entry.reader.cancel(input.lease.signal.reason).catch(() => undefined);
close();
};
+ // The client stopped reading: the queue is dropped (error, not close, which
+ // would keep it for the client) and the client reconnects by itself.
+ const overflow = () => {
+ if (closed || cancelled) return;
+ cancelled = true;
+ closed = true;
+ if (pingTimer) clearInterval(pingTimer);
+ input.lease.signal.removeEventListener("abort", abort);
+ input.lease.release();
+ for (const entry of readers) void entry.reader.cancel().catch(() => undefined);
+ console.warn("[omnirush-server] engine event stream closed: its client stopped reading");
+ controller.error(new Error("event stream backlog"));
+ };
const finish = () => {
active -= 1;
if (active > 0 || closed || cancelled) return;
@@ -3028,6 +3046,10 @@ function mergedEventBody(input: {
controller.enqueue(encoder.encode(`${frame}\n\n`));
}
}
+ if ((controller.desiredSize ?? 0) < -ENGINE_EVENT_BACKLOG_BYTES) {
+ overflow();
+ break;
+ }
if (parsed.overflow) {
// A frame that never terminates would buffer without bound;
// drop this connection and let the client reconnect.
@@ -3063,7 +3085,7 @@ function mergedEventBody(input: {
input.lease.release();
await Promise.all(readers.map((entry) => entry.reader.cancel(reason).catch(() => undefined)));
},
- });
+ }, new ByteLengthQueuingStrategy({ highWaterMark: 0 }));
}
// Read lazily so tests can shrink the deadline at runtime. Matches the 5s