From e891449e0ff3fe5f7a29ffee8995241ba0555ef6 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 5 Feb 2026 19:14:06 -0600 Subject: [PATCH] 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 --- .../coordinator/coordinator-client.service.ts | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/apps/orchestrator/src/coordinator/coordinator-client.service.ts b/apps/orchestrator/src/coordinator/coordinator-client.service.ts index e04790d..974220c 100644 --- a/apps/orchestrator/src/coordinator/coordinator-client.service.ts +++ b/apps/orchestrator/src/coordinator/coordinator-client.service.ts @@ -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 { - 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); } }