Skip to content

Commit 256cb4c

Browse files
fix(tracer): capture context after first yield for improved ASGI compatibility
1 parent cb66d97 commit 256cb4c

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

src/openlayer/lib/tracing/tracer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,13 @@ def __next__(self):
518518
func_kwargs=func_kwargs,
519519
context_kwarg=context_kwarg,
520520
)
521-
# Capture context after trace is initialized for ASGI compatibility
522-
# This ensures we can finalize in the correct context even if
523-
# later __next__ calls happen in different worker threads
524-
self._captured_context = contextvars.copy_context()
525521
self._trace_initialized = True
526522

527523
try:
528524
chunk = next(self._original_gen)
529525
self._output_chunks.append(chunk)
526+
if self._captured_context is None:
527+
self._captured_context = contextvars.copy_context()
530528
return chunk
531529
except StopIteration:
532530
# Finalize trace when generator is exhausted

0 commit comments

Comments
 (0)