Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: isolate per-client SSE write failures in broadcastSse (#8915 review)

[._.]/ Adam Eivyf9a4104ba87b
diff --git a/server/lib/sseUtils.js b/server/lib/sseUtils.js
index 0f4f079244..39a9ffad51 100644
--- a/server/lib/sseUtils.js
+++ b/server/lib/sseUtils.js
@@ -39,7 +39,17 @@ export const broadcastSse = (job, payload, { retain = true } = {}) => {
// run is actually doing.
if (retain) job.lastPayload = payload;
const msg = `data: ${JSON.stringify(payload)}\n\n`;
- for (const c of job.clients) c.write(msg);
+ // Isolate each client's write — a dead client's `res.write` throwing (a
+ // closed/broken pipe) must not abort the loop and starve every later
+ // client in `job.clients` of a frame their connection is still open for
+ // (#8915).
+ for (const c of job.clients) {
+ try {
+ c.write(msg);
+ } catch (err) {
+ console.error(`❌ SSE client write failed, dropping this subscriber's frame: ${err?.message || err}`);
+ }
+ }
};
// Dispatches a job's terminal SSE frame and its companion lifecycle event
diff --git a/server/lib/sseUtils.test.js b/server/lib/sseUtils.test.js
index 16bc1a292c..545db994d2 100644
--- a/server/lib/sseUtils.test.js
+++ b/server/lib/sseUtils.test.js
@@ -63,6 +63,23 @@ describe('broadcastSse', () => {
broadcastSse(job, { ok: true });
expect(job.lastPayload).toEqual({ ok: true });
});
+
+ // Regression (#8915 review follow-up): a single dead client's res.write
+ // throwing used to abort the `for` loop outright, so every LATER client in
+ // job.clients never received the frame at all — not just the throwing one.
+ it('isolates one client throwing on write so later clients still receive the frame', () => {
+ const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
+ const dead = { write: vi.fn(() => { throw new Error('write after end'); }) };
+ const alive = makeRes();
+ const job = { clients: [dead, alive] };
+ const payload = { type: 'complete' };
+
+ expect(() => broadcastSse(job, payload)).not.toThrow();
+
+ expect(alive.write).toHaveBeenCalledWith(`data: ${JSON.stringify(payload)}\n\n`);
+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE client write failed'));
+ errSpy.mockRestore();
+ });
});
describe('attachSseClient', () => {
@@ -360,7 +377,7 @@ describe('createJobFailureFinalizer', () => {
expect(job.status).toBe('error');
expect(events.emit).toHaveBeenCalledWith('failed', { generationId: 'j1', error: 'boom' });
- expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE terminal broadcast failed'));
+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE client write failed'));
errSpy.mockRestore();
});
diff --git a/server/services/imageGen/grok.test.js b/server/services/imageGen/grok.test.js
index 8c07c63a5b..b96c69e058 100644
--- a/server/services/imageGen/grok.test.js
+++ b/server/services/imageGen/grok.test.js
@@ -416,7 +416,7 @@ describe('grok provider — directed-path harvest', () => {
await vi.waitFor(() => expect(completedListener).toHaveBeenCalledTimes(1), { timeout: 5000, interval: 50 });
expect(failedListener).not.toHaveBeenCalled();
- expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE terminal broadcast failed'));
+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE client write failed'));
errSpy.mockRestore();
}, 10000);
});
diff --git a/server/services/imageGen/local.test.js b/server/services/imageGen/local.test.js
index a48f6f7a63..15d65e48de 100644
--- a/server/services/imageGen/local.test.js
+++ b/server/services/imageGen/local.test.js
@@ -1163,7 +1163,7 @@ describe('local render terminal dispatch — SSE/lifecycle decoupling (#8915)',
// diagnosis — even though the SSE broadcast to this client threw.
const failure = await failed;
expect(failure.error).toContain('invalid value for --steps');
- expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE terminal broadcast failed'));
+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE client write failed'));
errSpy.mockRestore();
});
@@ -1193,7 +1193,7 @@ describe('local render terminal dispatch — SSE/lifecycle decoupling (#8915)',
// Gallery indexing / peer-sync (driven by 'completed') still fires even
// though the SSE 'complete' frame threw delivering to this client.
await expect(completed).resolves.toMatchObject({ mode: 'local' });
- expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE terminal broadcast failed'));
+ expect(errSpy).toHaveBeenCalledWith(expect.stringContaining('SSE client write failed'));
errSpy.mockRestore();
});
});