Skip to content

Commit 990ec15

Browse files
committed
fix: delete host entry before awaiting close to prevent stale-client race
If getOrCreateClient ran after refCount hit 0 but before clients.delete(host), it would receive a closing TelemetryClient. Deleting synchronously first means any concurrent caller gets a fresh instance instead. Co-authored-by: samikshya-chand_data
1 parent d009957 commit 990ec15

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

lib/telemetry/TelemetryClientProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ class TelemetryClientProvider {
9292
holder.refCount -= 1;
9393
logger.log(LogLevel.debug, `TelemetryClient reference count for ${host}: ${holder.refCount}`);
9494

95-
// Close and remove client when reference count reaches zero
95+
// Close and remove client when reference count reaches zero.
96+
// Delete from map before awaiting close so a concurrent getOrCreateClient
97+
// creates a fresh client rather than receiving this closing one.
9698
if (holder.refCount <= 0) {
99+
this.clients.delete(host);
97100
try {
98101
await holder.client.close();
99-
this.clients.delete(host);
100102
logger.log(LogLevel.debug, `Closed and removed TelemetryClient for host: ${host}`);
101103
} catch (error: any) {
102104
// Swallow all exceptions per requirement

0 commit comments

Comments
 (0)