fix(producer): force-close file server connections after render

The render process hung after completion because the file server's
HTTP keep-alive connections held the Node.js event loop open.
server.close() stops accepting new connections but doesn't destroy
existing ones. Now tracks all connections and destroys them on close.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Miguel Ángel
2026-03-31 20:47:23 +02:00
co-authored by Claude Opus 4.6
parent c127141e95
commit 67342bdd45
2 changed files with 19 additions and 5 deletions
-4
View File
@@ -253,10 +253,6 @@ Examples:
browserPath,
});
}
// Explicit exit — Puppeteer/Chrome CDP connections hold the event loop
// open even after all work is done. Without this, the process hangs.
process.exit(0);
},
});
+19 -1
View File
@@ -9,6 +9,7 @@
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import type { IncomingMessage } from "node:http";
import { readFileSync, existsSync, statSync } from "node:fs";
import { join, extname } from "node:path";
import { getVerifiedHyperframeRuntimeSource } from "./hyperframeRuntimeLoader.js";
@@ -323,14 +324,31 @@ export function createFileServer(options: FileServerOptions): Promise<FileServer
});
return new Promise((resolve) => {
// Track open connections so we can force-destroy them on close.
// Without this, server.close() waits for keep-alive connections to
// drain, holding the Node.js event loop open indefinitely.
const connections = new Set<IncomingMessage["socket"]>();
const server = serve({ fetch: app.fetch, port }, (info) => {
const actualPort = info.port;
const url = `http://localhost:${actualPort}`;
const httpServer = (server as unknown as { server?: { on: Function } }).server;
if (httpServer?.on) {
httpServer.on("connection", (socket: IncomingMessage["socket"]) => {
connections.add(socket);
socket.on("close", () => connections.delete(socket));
});
}
resolve({
url,
port: actualPort,
close: () => server.close(),
close: () => {
// Destroy all open connections, then close the server
for (const socket of connections) socket.destroy();
connections.clear();
server.close();
},
});
});
});