Skip to content

Commit c555085

Browse files
authored
test(agent): add timeout and error logging to checkAgentStatus (#7724)
The checkAgentStatus function in the test agent helper makes an HTTP request to the test agent with no timeout. If the TCP connection is established but no HTTP response is received, the promise hangs indefinitely. This is a likely cause of flaky test timeouts with no indication of the root cause (e.g. the AI Guard Windows CI job). Add a 2s timeout with a descriptive warning so hangs are caught early and the cause is visible in CI logs. Also log unexpected errors (other than ECONNREFUSED, which is the normal "no test agent" case) to aid future debugging.
1 parent c820334 commit c555085

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

packages/dd-trace/test/plugins/agent.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,29 @@ function handleTraceRequest (req, res, sendToTestAgent) {
227227
}
228228

229229
function checkAgentStatus () {
230-
const agentUrl = process.env.DD_TRACE_AGENT_URL || 'http://127.0.0.1:9126'
231-
232230
return new Promise((resolve) => {
233-
const request = http.request(`${agentUrl}/info`, { method: 'GET' }, response => {
231+
const agentUrl = process.env.DD_TRACE_AGENT_URL || 'http://127.0.0.1:9126'
232+
const timeoutMs = 2000
233+
234+
const request = http.request(`${agentUrl}/info`, { method: 'GET', timeout: timeoutMs }, response => {
234235
resolve(response.statusCode === 200)
235236
})
236237

237-
request.on('error', (_error_) => {
238+
request.on('timeout', () => {
239+
// eslint-disable-next-line no-console
240+
console.warn(
241+
`checkAgentStatus: Timed out after ${timeoutMs}ms trying to reach test agent at ${agentUrl}. ` +
242+
'Proceeding without test agent. If this happens frequently, investigate what is listening on that port.'
243+
)
244+
request.destroy()
245+
resolve(false)
246+
})
247+
248+
request.on('error', (/** @type {NodeJS.ErrnoException} */ err) => {
249+
if (err.code !== 'ECONNREFUSED') {
250+
// eslint-disable-next-line no-console
251+
console.warn(`checkAgentStatus: Unexpected error reaching test agent at ${agentUrl}`, err)
252+
}
238253
resolve(false)
239254
})
240255

0 commit comments

Comments
 (0)