Proxy crashes with unhandled read ECONNRESET when a backend restarts after serving a keep-alive request
Version: portless 0.15.6 (also present in 0.10.3–0.15.5), Node 24.21.0, Windows 11. Happens in both HTTPS and HTTP mode.
What happens
After a request has been proxied to an app, restarting that app (e.g. air rebuilding a Go server) kills the whole proxy:
node:events:505
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:216:20)
Emitted 'error' event on Socket instance at:
at emitErrorNT (node:internal/streams/destroy:170:8)
errno: -4077, code: 'ECONNRESET', syscall: 'read'Restarting the app while nothing has been proxied does not crash it. This looks like the same symptom as #111 on a different socket; #127 fixed the TLS wrapper socket.
Cause
In the request handler, proxyReqHeaders is a copy of req.headers, so the client's Connection: keep-alive is forwarded to the backend. The backend request uses createConnection: () => createLoopbackConnection(route.port) with no agent. When a keep-alive response ends, Node's responseKeepAlive removes the request's socket error listener and emits free; with no agent to take the socket it stays open with no error listener at all. When the backend then resets it, the error event is unhandled and the process exits.
Hop-by-hop headers are already stripped from responses (HOP_BY_HOP_HEADERS), but not from the forwarded request.
Minimal repro (plain Node, no portless):
import http from "node:http";
import net from "node:net";
const variant = process.argv[2]; // "keepalive" | "close" | "none"
const sockets = new Set();
const backend = http.createServer((req, res) => res.end("ok"));
backend.on("connection", (s) => sockets.add(s));
backend.listen(0, () => {
const port = backend.address().port;
const req = http.request({
createConnection: () => net.connect({ host: "127.0.0.1", port }),
path: "/",
headers: variant === "none" ? {} : { connection: variant === "keepalive" ? "keep-alive" : "close" },
}, (res) => {
res.resume();
res.on("end", () => setTimeout(() => {
for (const s of sockets) s.resetAndDestroy(); // what a backend restart does
setTimeout(() => { console.log("still alive"); process.exit(0); }, 300);
}, 100));
});
req.on("error", (e) => console.log("request error handled:", e.code));
req.end();
});node repro.mjs keepalive→ crashes withUnhandled 'error' event … read ECONNRESET(exit 1)node repro.mjs close→ still alivenode repro.mjs none→ still alive
Suggested fix
Strip hop-by-hop headers (and any header named in Connection) from the forwarded request in the normal request path, as RFC 9110 §7.6.1 requires of a proxy. For example, before http.request(...):
for (const name of String(proxyReqHeaders.connection || "").split(",")) {
delete proxyReqHeaders[name.trim().toLowerCase()];
}
for (const h of HOP_BY_HOP_HEADERS) {
delete proxyReqHeaders[h];
}The WebSocket upgrade paths set connection: Upgrade deliberately and already attach error listeners, so they are unaffected. Attaching a no-op error listener to the socket returned by createLoopbackConnection would also stop the crash, but forwarding keep-alive to a socket that is never reused has no benefit.
Source: vercel-labs/portless