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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: Beeper realtime socket no longer crashes the server on a rejected WebSocket upgrade (#12597)
[._.]/ Adam Eivy63250dd43d0a
diff --git a/server/services/beeperSocket.js b/server/services/beeperSocket.jsindex bf01faa09b..0c3d5dc869 100644--- a/server/services/beeperSocket.js+++ b/server/services/beeperSocket.js@@ -278,6 +278,11 @@ function dropSocket() {socket = null;if (!dying) return;dying.removeAllListeners?.();+ // `ws` terminate() on a still-CONNECTING socket aborts the handshake and emits+ // `error` on the next tick; with every listener stripped that is an unhandled+ // 'error' event, which exits the process. The socket is superseded, so the+ // late error carries nothing actionable.+ dying.on?.('error', () => {});// `terminate()` is the `ws` escape hatch for a socket that may be half-open —// `close()` waits for a close handshake the dead peer will never send.if (typeof dying.terminate === 'function') dying.terminate();diff --git a/server/services/beeperSocket.test.js b/server/services/beeperSocket.test.jsindex 93d29c1b39..1bb9a362bf 100644--- a/server/services/beeperSocket.test.js+++ b/server/services/beeperSocket.test.js@@ -21,6 +21,7 @@ class FakeSocket {this.options = options;this.sent = [];this.terminated = false;+ this.readyState = 0; // CONNECTING until `open`this.listeners = new Map();createdSockets.push(this);}@@ -33,7 +34,19 @@ class FakeSocket {removeAllListeners() { this.listeners.clear(); }send(payload) { this.sent.push(payload); }- terminate() { this.terminated = true; }+ // Mirrors `ws`: terminating a CONNECTING socket aborts the handshake and emits+ // `error` on the next tick — an unhandled 'error' event throws when nothing listens.+ terminate() {+ this.terminated = true;+ if (this.readyState !== 0) return;+ this.readyState = 2;+ process.nextTick(() => {+ if (!this.listeners.get('error')?.length) {+ throw new Error('Unhandled \'error\' event: WebSocket was closed before the connection was established');+ }+ this.fire('error', new Error('WebSocket was closed before the connection was established'));+ });+ }close() { this.terminated = true; }fire(event, ...args) {@@ -42,6 +55,7 @@ class FakeSocket {// Walk the documented handshake: open → ready → subscriptions.updated.handshake() {+ this.readyState = 1;this.fire('open');this.fire('message', JSON.stringify({ type: 'ready', version: 1, chatIDs: [], app: { state: false } }));this.fire('message', JSON.stringify({ type: 'subscriptions.updated', requestID: 'r1', chatIDs: ['*'] }));@@ -356,6 +370,43 @@ describe('beeperSocket — silence watchdog (injected time)', () => {latest().fire('close', 1006);expect(clock.delaysFor((timer) => timer.ms !== SILENCE_TIMEOUT_MS)).toEqual([500]);});++ // Terminating a CONNECTING socket makes `ws` emit `error` on the next tick;+ // an uncaught one exits the server and PM2 restart-loops it.+ describe('terminating a socket mid-handshake', () => {+ const uncaught = [];+ const onUncaught = (err) => { uncaught.push(err); };+ beforeEach(() => {+ uncaught.length = 0;+ process.on('uncaughtException', onUncaught);+ });+ afterEach(() => { process.off('uncaughtException', onUncaught); });+ const flushTick = () => new Promise((resolve) => setImmediate(resolve));++ it.each([401, 503])('does not crash the process on an HTTP %i upgrade rejection', async (statusCode) => {+ vi.spyOn(console, 'error').mockImplementation(() => {});+ vi.spyOn(console, 'log').mockImplementation(() => {});+ await start();+ const first = latest();++ first.fire('unexpected-response', {}, { statusCode });+ await flushTick();++ expect(first.terminated).toBe(true);+ expect(uncaught).toEqual([]);+ });++ it('does not crash the process when the silence watchdog fires before the handshake completes', async () => {+ await start();+ const first = latest();++ expect(clock.runTimersWithDelay(SILENCE_TIMEOUT_MS)).toBe(1);+ await flushTick();++ expect(first.terminated).toBe(true);+ expect(uncaught).toEqual([]);+ });+ });});describe('beeperSocket — sweep triggers', () => {