-
Notifications
You must be signed in to change notification settings - Fork 23
fix(otel): release unreleased scope on operation re-entry #654
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,8 +174,24 @@ def _attempt_key(info: UserFunctionStartInfo | UserFunctionEndInfo) -> str: | |
| # Context scope helpers | ||
| # ------------------------------------------------------------------ | ||
| def _attach_context(self, key: str, new_context: Context) -> None: | ||
| """Attach a context and remember its token under ``key``.""" | ||
| """Attach a context and remember its token under ``key``. | ||
|
|
||
| A token already stored under ``key`` means the previous scope for that | ||
| operation was never released: its user function did not reach | ||
| ``on_user_function_end``, because it suspended and a timed in-process | ||
| resume re-entered the same operation. Release it before attaching, so | ||
| the new token does not bury one that can never be detached -- otherwise | ||
| releasing the new scope would restore the abandoned span and leave it | ||
| current for later work on this thread. | ||
| """ | ||
| with self._lock: | ||
| if key in self._context_tokens: | ||
| logger.debug( | ||
| "Releasing an unreleased context scope for %s before " | ||
| "re-attaching; its user function did not report an end.", | ||
| key, | ||
| ) | ||
| self._detach_context(key) | ||
|
wangyb-A marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Codex AI review [P2] Preserve LIFO order when replacing a scope. If this key belongs to an outer context while an inner scope remains current, detaching only the outer token resets the context stack out of order. Re-entering the inner context then captures the abandoned outer span, so resumed outer code records data on a span that is never exported. On same-thread re-entry, unwind all newer tokens for that thread in reverse attachment order through this key before attaching the replacement. Apply the same fix in |
||
| self._context_tokens[key] = ( | ||
| threading.get_ident(), | ||
| otel_context.attach(new_context), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.