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); } }