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