Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/tracing/idleSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
// Timer that tracks maxSpanTime for child spans
let _childSpanTimeoutID: ReturnType<typeof setTimeout> | undefined;

// Timer that tracks finalTimeout
let _finalTimeoutID: ReturnType<typeof setTimeout> | undefined;

// The reason why the span was finished
let _finishReason: IdleSpanFinishReason = FINISH_REASON_EXTERNAL_FINISH;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/lib/tracing/idleSpan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
Loading