Fix crash loop: disable keep-alive agents and add unhandledRejection guard

The previous fix (agent .on('error')) doesn't reach individual socket
objects — socket-level parse errors still escaped as uncaught exceptions.

Root fix: disable keepAlive on the HTTP/HTTPS monitoring agents. Without
connection reuse there is no stale connection to trigger "Parse Error:
Data after Connection: close". Monitoring intervals are 60s+ so the lack
of connection pooling has no meaningful performance impact.

Belt-and-suspenders: add process.on('unhandledRejection') alongside the
existing uncaughtException guard to catch any async paths that leak.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013AQYLFrscmMnzMiXAAANoa
This commit is contained in:
Claude committed 2026-08-07 00:02:01 +00:00
1 parent b13a8c343f
commit dcfd9e9b65
2 files changed
+19 -10

No files matched your search

+8 -8
View File
@@ -31,14 +31,14 @@ function parseHostPort(urlString, fallbackPort) {
class MonitorService {
constructor() {
// Keep-alive agents for pooled HTTP/S connections.
// Socket-level 'error' listener prevents parse errors (e.g. "Data after
// Connection: close") from bubbling up as uncaught exceptions when a
// monitored server sends a malformed response on a reused connection.
this.httpAgent = new http.Agent({ keepAlive: true, maxSockets: 50 });
this.httpAgent.on('error', () => {});
this.httpsAgent = new https.Agent({ keepAlive: true, maxSockets: 50 });
this.httpsAgent.on('error', () => {});
// Disable keep-alive to avoid "Parse Error: Data after Connection: close"
// crashes. Keep-alive connection reuse causes the HTTP parser to throw
// when a monitored server sends extra data after Connection: close — the
// socket-level error becomes an uncaught exception that kills the process.
// Monitoring checks are infrequent enough that connection pooling provides
// no meaningful benefit.
this.httpAgent = new http.Agent({ keepAlive: false });
this.httpsAgent = new https.Agent({ keepAlive: false });
// Circuit breaker for HTTP checks (5 failures before opening, 60s reset)
this.httpCircuitBreaker = new CircuitBreaker(5, 60000, this.isTransientError);