fix(CQ-ORCH-4): Fix AbortController timeout cleanup using try-finally

Move clearTimeout() to finally blocks in both checkQuality() and
isHealthy() methods to ensure timer cleanup even when errors occur.
This prevents timer leaks on failed requests.

Refs #339

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jason Woltje
2026-02-05 19:14:06 -06:00
parent b952c24f21
commit e891449e0f

View File

@@ -93,12 +93,12 @@ export class CoordinatorClientService {
let lastError: Error | undefined;
for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => {
controller.abort();
}, this.timeout);
const controller = new AbortController();
const timeoutId = setTimeout(() => {
controller.abort();
}, this.timeout);
try {
const response = await fetch(url, {
method: "POST",
headers: this.buildHeaders(),
@@ -106,8 +106,6 @@ export class CoordinatorClientService {
signal: controller.signal,
});
clearTimeout(timeoutId);
// Retry on 503 (Service Unavailable)
if (response.status === 503) {
this.logger.warn(
@@ -168,6 +166,8 @@ export class CoordinatorClientService {
} else {
throw lastError;
}
} finally {
clearTimeout(timeoutId);
}
}
@@ -179,26 +179,26 @@ export class CoordinatorClientService {
* @returns true if coordinator is healthy, false otherwise
*/
async isHealthy(): Promise<boolean> {
try {
const url = `${this.coordinatorUrl}/health`;
const controller = new AbortController();
const timeoutId = setTimeout(() => {
controller.abort();
}, 5000);
const url = `${this.coordinatorUrl}/health`;
const controller = new AbortController();
const timeoutId = setTimeout(() => {
controller.abort();
}, 5000);
try {
const response = await fetch(url, {
headers: this.buildHeaders(),
signal: controller.signal,
});
clearTimeout(timeoutId);
return response.ok;
} catch (error) {
this.logger.warn(
`Coordinator health check failed: ${error instanceof Error ? error.message : String(error)}`
);
return false;
} finally {
clearTimeout(timeoutId);
}
}