Skip to content

Commit 9b79a76

Browse files
JohnMcLearclaude
andcommitted
fix: heartbeat _getActiveHandles optional chain bug (Qodo #2)
Qodo correctly flagged `_getActiveHandles?.().length` as a latent TypeError: `?.()` guards the call but the call's `undefined` return on a missing method still hits `.length`, which throws. Since the heartbeat fires on a setInterval inside the mocha bootstrap, a Node build without the underscore-prefixed internals would take down the whole backend test run. Capture the array first, then read `.length` only when it actually exists. -1 stays as the "API missing" sentinel. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent aa79a34 commit 9b79a76

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/tests/backend/diagnostics.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,15 @@ diag('diagnostics loaded');
7171
// tightly we can bracket the kill timestamp.
7272
const heartbeat = setInterval(() => {
7373
const mem = process.memoryUsage();
74-
const handles = (process as any)._getActiveHandles?.().length ?? -1;
75-
const requests = (process as any)._getActiveRequests?.().length ?? -1;
74+
// _getActiveHandles / _getActiveRequests are undocumented Node internals.
75+
// The earlier shape `_getActiveHandles?.().length ?? -1` was a bug: `?.()`
76+
// only guards the call, so a missing method returns `undefined` and then
77+
// `.length` throws TypeError — which would take down the whole test run.
78+
// Capture the array first, then read .length only when it actually exists.
79+
const handlesArr = (process as any)._getActiveHandles?.();
80+
const handles = handlesArr ? handlesArr.length : -1;
81+
const requestsArr = (process as any)._getActiveRequests?.();
82+
const requests = requestsArr ? requestsArr.length : -1;
7683
diag(`hb running="${currentTest}" lastFinished="${lastFinishedTest}" ` +
7784
`rss=${Math.round(mem.rss / 1024 / 1024)}M ` +
7885
`heap=${Math.round(mem.heapUsed / 1024 / 1024)}M ` +

0 commit comments

Comments
 (0)