Skip to content

Conversation

@s1gr1d
Copy link
Member

@s1gr1d s1gr1d commented Nov 17, 2025

This PR was factored out of another PR to make reviewing easier. The other PR: #18189

Moved the spanStart and spanEnd listeners into an extra function (_setupTraceLifecycleListeners) to be able to only call it depending on the lifecycle (used in another PR).

Part of #17279

Comment on lines 62 to 68
this._client = client;
this._sessionSampled = sessionSampled;

client.on('spanStart', span => {
if (!this._sessionSampled) {
DEBUG_BUILD && debug.log('[Profiling] Session not sampled because of negative sampling decision.');
return;
}
if (span !== getRootSpan(span)) {
return;
}
// Only count sampled root spans
if (!span.isRecording()) {
DEBUG_BUILD && debug.log('[Profiling] Discarding profile because root span was not sampled.');
return;
}

// Matching root spans with profiles
getGlobalScope().setContext('profile', {
profiler_id: this._profilerId,
});

const spanId = span.spanContext().spanId;
if (!spanId) {
return;
}
if (this._activeRootSpanIds.has(spanId)) {
return;
}

this._activeRootSpanIds.add(spanId);
const rootSpanCount = this._activeRootSpanIds.size;

const timeout = setTimeout(() => {
this._onRootSpanTimeout(spanId);
}, MAX_ROOT_SPAN_PROFILE_MS);
this._rootSpanTimeouts.set(spanId, timeout);

if (rootSpanCount === 1) {
DEBUG_BUILD &&
debug.log(
`[Profiling] Root span with ID ${spanId} started. Will continue profiling for as long as there are active root spans (currently: ${rootSpanCount}).`,
);

this.start();
}
});

client.on('spanEnd', span => {
if (!this._sessionSampled) {
return;
}

const spanId = span.spanContext().spanId;
if (!spanId || !this._activeRootSpanIds.has(spanId)) {
return;
}

this._activeRootSpanIds.delete(spanId);
const rootSpanCount = this._activeRootSpanIds.size;

DEBUG_BUILD &&
debug.log(
`[Profiling] Root span with ID ${spanId} ended. Will continue profiling for as long as there are active root spans (currently: ${rootSpanCount}).`,
);
if (rootSpanCount === 0) {
this._collectCurrentChunk().catch(e => {
DEBUG_BUILD && debug.error('[Profiling] Failed to collect current profile chunk on `spanEnd`:', e);
});

this.stop();
}
});
this._setupTraceLifecycleListeners(client);
}

/**
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: notifyRootSpanActive() fails to register a safeguard timeout for already-active root spans, leading to indefinite profiling if spanEnd is not called.
Severity: CRITICAL | Confidence: 0.95

🔍 Detailed Analysis

The notifyRootSpanActive() method, used for already-active root spans during profiler initialization, adds the span ID to _activeRootSpanIds but fails to call _registerTraceRootSpan(). This omission means no safeguard timeout is registered for these spans. If such a pre-existing root span never fires its spanEnd event, the profiler will continue running indefinitely, wasting resources by continuously collecting and sending profile chunks every 60 seconds, contradicting the intended design of MAX_ROOT_SPAN_PROFILE_MS.

💡 Suggested Fix

Modify notifyRootSpanActive() to call _registerTraceRootSpan(spanId) instead of directly adding spanId to _activeRootSpanIds. This ensures that all active root spans, regardless of how they are initialized, have the necessary safeguard timeout registered.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/browser/src/profiling/UIProfiler.ts#L62-L68

Potential issue: The `notifyRootSpanActive()` method, used for already-active root spans
during profiler initialization, adds the span ID to `_activeRootSpanIds` but fails to
call `_registerTraceRootSpan()`. This omission means no safeguard timeout is registered
for these spans. If such a pre-existing root span never fires its `spanEnd` event, the
profiler will continue running indefinitely, wasting resources by continuously
collecting and sending profile chunks every 60 seconds, contradicting the intended
design of `MAX_ROOT_SPAN_PROFILE_MS`.

Did we get this right? 👍 / 👎 to inform future reviews.

Reference_id: 2726774

Copy link
Member Author

@s1gr1d s1gr1d Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done in another function - no problem here

It's kept track of it in _rootSpanTimeouts

DEBUG_BUILD && debug.log('[Profiling] Started profiling with profile ID:', this._profilerId);

// Expose profiler_id to match root spans with profiles
getGlobalScope().setContext('profile', { profiler_id: this._profilerId });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Profiler Context: Spans Adrift

Moving the global scope context setting from the spanStart listener to start() breaks profile-span association when the profiler fails to initialize. If the first root span fails to start the profiler, _resetProfilerInfo() clears the context and sets _isRunning to false. Subsequent root spans won't call start() (since rootSpanCount !== 1), leaving them without the profiler_id context needed to match spans with profiles.

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as intended, this is still attached at the correct time.

@github-actions
Copy link
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.64 kB - -
@sentry/browser - with treeshaking flags 23.14 kB - -
@sentry/browser (incl. Tracing) 41.29 kB - -
@sentry/browser (incl. Tracing, Profiling) 45.62 kB +0.1% +43 B 🔺
@sentry/browser (incl. Tracing, Replay) 79.75 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 69.45 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 84.44 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 96.66 kB - -
@sentry/browser (incl. Feedback) 41.29 kB - -
@sentry/browser (incl. sendFeedback) 29.3 kB - -
@sentry/browser (incl. FeedbackAsync) 34.22 kB - -
@sentry/react 26.33 kB - -
@sentry/react (incl. Tracing) 43.23 kB - -
@sentry/vue 29.12 kB - -
@sentry/vue (incl. Tracing) 43.09 kB - -
@sentry/svelte 24.66 kB - -
CDN Bundle 26.96 kB - -
CDN Bundle (incl. Tracing) 41.84 kB - -
CDN Bundle (incl. Tracing, Replay) 78.39 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 83.85 kB - -
CDN Bundle - uncompressed 78.94 kB - -
CDN Bundle (incl. Tracing) - uncompressed 124.09 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 240.12 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 252.88 kB - -
@sentry/nextjs (client) 45.38 kB - -
@sentry/sveltekit (client) 41.68 kB - -
@sentry/node-core 50.91 kB - -
@sentry/node 159.09 kB +0.07% +101 B 🔺
@sentry/node - without tracing 92.78 kB - -
@sentry/aws-serverless 106.53 kB - -

View base workflow run

@s1gr1d s1gr1d merged commit 5a5e091 into develop Nov 17, 2025
148 checks passed
@s1gr1d s1gr1d deleted the sig/profling-move-lifecycle-methods branch November 17, 2025 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants