Skip to content

fix(fi-instrumentation-otel): keep context helpers attached in async functions - #647

Merged
nik13 merged 1 commit into
devfrom
fix/fi-instrumentation-async-helpers-on-dev
Nov 10, 2026
Merged

nik13 merged 1 commit into
devfrom
fix/fi-instrumentation-async-helpers-on-dev

Conversation

@nik13

@nik13 nik13 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

What was broken

  • The context helpers (using_session, using_user, using_attributes, using_metadata, using_tags, using_prompt_template) work as decorators, but on an async def function they attached and detached before the function body ran. Spans created inside async functions got no session or user, so they appeared in no session or the wrong one.

What changed

  • On coroutine functions the helper attaches inside the body and detaches when it finishes.
  • On async generators each generator keeps its own context across resumptions and restores the caller's context before every yield; asend, athrow, aclose, cancellation and out-of-order closing are handled.
  • Each call gets its own context token, so concurrent tasks and exceptions restore correctly. Synchronous decorators and with / async with are unchanged.

Verification

Paired run against the previous base on a clean, offline sandbox:

  • 41 helper cases pass, including interleaved generators, stacked decorators and cancellation.
  • Core suite: 2,952 passed before, 2,993 passed after, 0 failed; the dependent package suites run on this core also pass.

Compatibility

No dependency, version or Python range change. Only python/fi_instrumentation and its tests change.

…functions

using_session, using_user, using_attributes and the other context helpers
work as decorators. On an async function or async generator they now
attach inside the body for its whole execution, with one context token per
call and per generator resumption, so concurrent tasks, interleaved
generators and exceptions each restore the caller's context.
@nik13
nik13 merged commit 6385161 into dev Nov 10, 2026
166 checks passed
@nik13
nik13 deleted the fix/fi-instrumentation-async-helpers-on-dev branch November 10, 2026 13:05
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.

1 participant