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: refuse websocket upgrades during the shutdown request drain (#8350)

[._.]/ Adam Eivy60f8dce765be
diff --git a/server/lib/httpDrain.js b/server/lib/httpDrain.js
index 1d3cbcd70c..ee3c1474d1 100644
--- a/server/lib/httpDrain.js
+++ b/server/lib/httpDrain.js
@@ -1,15 +1,19 @@
-// Graceful request drain for the HTTP listeners at shutdown (#8323).
+// Graceful request drain for the HTTP listeners at shutdown (#8323, #8350).
//
// `createHttpDrain(servers)` starts tracking every ordinary request the given
-// servers accept (upgrades never emit 'request', so WebSockets are not tracked —
-// their owners close them). `begin(windowMs)` is the synchronous "stop intake"
-// step the signal handler calls before it first awaits:
+// servers accept. `begin(windowMs)` is the synchronous "stop intake" step the
+// signal handler calls before it first awaits:
//
// - each listener stops accepting connections (`server.close()`, which also
// drops idle keep-alive sockets);
// - every request listener is swapped for a 503 + `Connection: close` refusal,
// so a request that arrives on a still-open keep-alive socket is never
// handed to the app — `server.close()` alone keeps serving those;
+// - every upgrade listener (engine.io, remote-desktop, Eidoverse) is swapped
+// the same way, so a WebSocket `Upgrade:` request on a still-open
+// keep-alive socket gets a raw 503 instead of reaching those handlers.
+// Sockets already upgraded before the drain began never emit 'upgrade'
+// again — their owners close them;
// - in-flight responses are told not to keep their socket alive, and an open
// SSE stream (which never ends on its own) is dropped outright.
//
@@ -32,6 +36,13 @@ const refuseDuringDrain = (req, res) => {
res.end(JSON.stringify({ error: 'Server is shutting down', code: 'SHUTTING_DOWN' }));
};
+// Upgrade requests never reach Express — there is no `res` to write through,
+// only the raw socket handed to every 'upgrade' listener.
+const refuseUpgradeDuringDrain = (req, socket) => {
+ if (socket.writable) socket.write('HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\n');
+ socket.destroy();
+};
+
export const createHttpDrain = (servers) => {
const listeners = servers.filter(Boolean);
const inFlight = new Set();
@@ -72,6 +83,8 @@ export const createHttpDrain = (servers) => {
for (const server of listeners) {
server.removeAllListeners('request');
server.on('request', refuseDuringDrain);
+ server.removeAllListeners('upgrade');
+ server.on('upgrade', refuseUpgradeDuringDrain);
// Already-closed (never listened) is fine — the refusal gate above is
// what matters for sockets that are still open.
server.close(() => {});
diff --git a/server/lib/httpDrain.test.js b/server/lib/httpDrain.test.js
index d7bba71ffd..f9959578b6 100644
--- a/server/lib/httpDrain.test.js
+++ b/server/lib/httpDrain.test.js
@@ -48,6 +48,27 @@ const deferred = () => {
return { promise, resolve };
};
+// A raw connection accepted before the drain begins, whose upgrade request is
+// only sent once `send()` is called — the same shape as `openPartialRequest`
+// above but for the 'upgrade' event rather than 'request'. `server.close()`
+// stops accepting NEW sockets, so the connection has to be open first for this
+// to exercise "a still-open keep-alive socket", not a refused new one.
+const openUpgradeRequest = async (port, path) => {
+ const socket = net.connect(port, '127.0.0.1');
+ await new Promise((resolve) => socket.once('connect', resolve));
+ let raw = '';
+ socket.on('data', (chunk) => { raw += chunk; });
+ const closed = new Promise((resolve) => socket.once('close', () => resolve(raw)));
+ return {
+ send: () => {
+ socket.write(
+ `GET ${path} HTTP/1.1\r\nHost: localhost\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n`,
+ );
+ return closed;
+ },
+ };
+};
+
describe('createHttpDrain', () => {
it('lets an accepted request finish while both listeners refuse new requests', async () => {
const handled = [];
@@ -113,4 +134,22 @@ describe('createHttpDrain', () => {
server.closeAllConnections();
expect(await stuck).toEqual({ error: 'ECONNRESET' });
});
+
+ it('refuses a WebSocket upgrade on a still-open socket once the drain begins', async () => {
+ const upgradeCalls = [];
+ const { server, port } = await listen((req, res) => res.end('ok'));
+ server.on('upgrade', (req, socket) => {
+ upgradeCalls.push(req.url);
+ socket.write('HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n');
+ });
+ const drain = createHttpDrain([server]);
+
+ const pending = await openUpgradeRequest(port, '/socket.io/');
+ drain.begin(5000);
+ const refused = await pending.send();
+
+ expect(refused).toMatch(/^HTTP\/1\.1 503 /);
+ expect(refused).toMatch(/connection: close/i);
+ expect(upgradeCalls).toEqual([]);
+ });
});