-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Revert "ref(core): Wrap scopes in WeakRef
when storing scopes on spans"
#17731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
+4
−290
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 4 additions & 46 deletions
packages/core/src/tracing/utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,29 @@ | ||
import type { Scope } from '../scope'; | ||
import type { Span } from '../types-hoist/span'; | ||
import { addNonEnumerableProperty } from '../utils/object'; | ||
import { GLOBAL_OBJ } from '../utils/worldwide'; | ||
|
||
const SCOPE_ON_START_SPAN_FIELD = '_sentryScope'; | ||
const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope'; | ||
|
||
type ScopeWeakRef = { deref(): Scope | undefined } | Scope; | ||
|
||
type SpanWithScopes = Span & { | ||
[SCOPE_ON_START_SPAN_FIELD]?: Scope; | ||
[ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: ScopeWeakRef; | ||
[ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: Scope; | ||
}; | ||
|
||
/** Wrap a scope with a WeakRef if available, falling back to a direct scope. */ | ||
function wrapScopeWithWeakRef(scope: Scope): ScopeWeakRef { | ||
try { | ||
// @ts-expect-error - WeakRef is not available in all environments | ||
const WeakRefClass = GLOBAL_OBJ.WeakRef; | ||
if (typeof WeakRefClass === 'function') { | ||
return new WeakRefClass(scope); | ||
} | ||
} catch { | ||
// WeakRef not available or failed to create | ||
// We'll fall back to a direct scope | ||
} | ||
|
||
return scope; | ||
} | ||
|
||
/** Try to unwrap a scope from a potential WeakRef wrapper. */ | ||
function unwrapScopeFromWeakRef(scopeRef: ScopeWeakRef | undefined): Scope | undefined { | ||
if (!scopeRef) { | ||
return undefined; | ||
} | ||
|
||
if (typeof scopeRef === 'object' && 'deref' in scopeRef && typeof scopeRef.deref === 'function') { | ||
try { | ||
return scopeRef.deref(); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
// Fallback to a direct scope | ||
return scopeRef as Scope; | ||
} | ||
|
||
/** Store the scope & isolation scope for a span, which can the be used when it is finished. */ | ||
export function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, isolationScope: Scope): void { | ||
if (span) { | ||
addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, wrapScopeWithWeakRef(isolationScope)); | ||
// We don't wrap the scope with a WeakRef here because webkit aggressively garbage collects | ||
// and scopes are not held in memory for long periods of time. | ||
addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope); | ||
addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope); | ||
} | ||
} | ||
|
||
/** | ||
* Grabs the scope and isolation scope off a span that were active when the span was started. | ||
* If WeakRef was used and scopes have been garbage collected, returns undefined for those scopes. | ||
*/ | ||
export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationScope?: Scope } { | ||
const spanWithScopes = span as SpanWithScopes; | ||
|
||
return { | ||
scope: spanWithScopes[SCOPE_ON_START_SPAN_FIELD], | ||
isolationScope: unwrapScopeFromWeakRef(spanWithScopes[ISOLATION_SCOPE_ON_START_SPAN_FIELD]), | ||
scope: (span as SpanWithScopes)[SCOPE_ON_START_SPAN_FIELD], | ||
isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD], | ||
}; | ||
} |
244 changes: 0 additions & 244 deletions
packages/core/test/lib/tracing/utils.test.ts
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.