Goal
Prevent memory leakage and telemetry drift inside the gateway observability metrics.
Problem
In src/middleware/metrics.ts, the gauge tracking active HTTP requests is incremented on entry and decremented inside res.on('finish'). If a client aborts the connection prematurely, the 'finish' event never fires—only the 'close' event fires. This causes the gauge to accumulate dead counts, drifting upward forever.
Task
- Modify
src/middleware/metrics.ts to listen to both 'finish' and 'close' response socket events.
- Use a flag or check if
res.writableEnded is true to ensure activeRequestsGauge.dec() is called exactly once per lifecycle to prevent double-decrementing.
Goal
Prevent memory leakage and telemetry drift inside the gateway observability metrics.
Problem
In
src/middleware/metrics.ts, the gauge tracking active HTTP requests is incremented on entry and decremented insideres.on('finish'). If a client aborts the connection prematurely, the'finish'event never fires—only the'close'event fires. This causes the gauge to accumulate dead counts, drifting upward forever.Task
src/middleware/metrics.tsto listen to both'finish'and'close'response socket events.res.writableEndedis true to ensureactiveRequestsGauge.dec()is called exactly once per lifecycle to prevent double-decrementing.