From 83cf272a497a76dab00e332d1453ccec9bb67079 Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Thu, 26 Nov 2026 17:30:23 -0500 Subject: [PATCH] fix(core): Clear pending idle span timers when the span ends --- packages/core/src/tracing/idleSpan.ts | 10 +++++- .../core/test/lib/tracing/idleSpan.test.ts | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/core/src/tracing/idleSpan.ts b/packages/core/src/tracing/idleSpan.ts index 334e191e69a7..9972082c0112 100644 --- a/packages/core/src/tracing/idleSpan.ts +++ b/packages/core/src/tracing/idleSpan.ts @@ -105,6 +105,9 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti // Timer that tracks maxSpanTime for child spans let _childSpanTimeoutID: ReturnType | undefined; + // Timer that tracks finalTimeout + let _finalTimeoutID: ReturnType | undefined; + // The reason why the span was finished let _finishReason: IdleSpanFinishReason = FINISH_REASON_EXTERNAL_FINISH; @@ -290,6 +293,11 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti _finished = true; activities.clear(); + // A pending timer keeps the span and its children reachable until it fires + _cancelIdleTimeout(); + _cancelChildSpanTimeout(); + clearTimeout(_finalTimeoutID); + _cleanupHooks.forEach(cleanup => cleanup()); _setSpanForScope(scope, previousActiveSpan); @@ -411,7 +419,7 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti _restartIdleTimeout(); } - setTimeout(() => { + _finalTimeoutID = setTimeout(() => { if (!_finished) { span.setStatus({ code: SPAN_STATUS_ERROR, message: 'deadline_exceeded' }); _finishReason = FINISH_REASON_FINAL_TIMEOUT; diff --git a/packages/core/test/lib/tracing/idleSpan.test.ts b/packages/core/test/lib/tracing/idleSpan.test.ts index d9a37209a1d6..5c6a27a29a2e 100644 --- a/packages/core/test/lib/tracing/idleSpan.test.ts +++ b/packages/core/test/lib/tracing/idleSpan.test.ts @@ -695,6 +695,37 @@ describe('startIdleSpan', () => { }); }); + describe('pending timers', () => { + it('leaves no timer behind when the span ends via idle timeout', () => { + const timersBefore = vi.getTimerCount(); + + const idleSpan = startIdleSpan({ name: 'idle span' }); + vi.advanceTimersByTime(TRACING_DEFAULTS.idleTimeout); + + expect(spanToJSON(idleSpan).end_timestamp).toBeDefined(); + expect(vi.getTimerCount()).toBe(timersBefore); + }); + + it('leaves no timer behind when the span is ended externally', () => { + const timersBefore = vi.getTimerCount(); + + const idleSpan = startIdleSpan({ name: 'idle span' }); + idleSpan.end(); + + expect(vi.getTimerCount()).toBe(timersBefore); + }); + + it('leaves no timer behind when the span is ended externally with an open child span', () => { + const timersBefore = vi.getTimerCount(); + + const idleSpan = startIdleSpan({ name: 'idle span' }); + startInactiveSpan({ name: 'inner' }); + idleSpan.end(); + + expect(vi.getTimerCount()).toBe(timersBefore); + }); + }); + describe('child span timeout', () => { it('finishes when a child span exceed timeout', () => { const idleSpan = startIdleSpan({ name: 'idle span' });