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

+11 -2
View File
@@ -6,8 +6,8 @@ const http = require('http');
const { randomUUID } = require('crypto');
require('dotenv').config();
// Prevent HTTP parser errors (e.g. from keep-alive connections to monitored
// services that send data after Connection: close) from crashing the process.
// Prevent HTTP parser errors (e.g. "Data after Connection: close") and other
// transient async errors from crashing the process.
process.on('uncaughtException', (err) => {
if (err.code === 'HPE_INVALID_CONSTANT' || (err.message && err.message.includes('Parse Error'))) {
console.error('HTTP parse error (swallowed):', err.message);
@@ -17,6 +17,15 @@ process.on('uncaughtException', (err) => {
process.exit(1);
});
process.on('unhandledRejection', (reason) => {
const msg = reason instanceof Error ? reason.message : String(reason);
if (msg.includes('Parse Error')) {
console.error('HTTP parse error (swallowed):', msg);
return;
}
console.error('Unhandled rejection:', reason);
});
const db = require('./database');
const scheduler = require('./scheduler');
const monitorService = require('./monitorService');