Commit 94d6f82
committed
fix: ensure call_llm spans are always ended in multi-agent scenarios
Replace `tracer.start_as_current_span('call_llm')` context manager with
explicit span lifecycle management in `_call_llm_with_tracing()`.
In multi-agent setups using `transfer_to_agent`, the async generator
receives `GeneratorExit` after the sub-agent completes execution. At
that point, the OTel context manager's `finally` block calls
`context.detach(token)` which raises `ValueError` because the
contextvars token became stale during the async context switch. This
exception prevents `span.end()` from ever being reached, so the span
is never exported to trace backends.
The fix uses `tracer.start_span()` + manual `context.attach()`/
`context.detach()` with a `try/finally` that catches the `ValueError`
from `detach()` and always calls `span.end()`.
Fixes #47151 parent a61ccf3 commit 94d6f82
2 files changed
Lines changed: 23 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| 49 | + | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
| |||
1127 | 1130 | | |
1128 | 1131 | | |
1129 | 1132 | | |
1130 | | - | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
1131 | 1144 | | |
1132 | 1145 | | |
1133 | 1146 | | |
| |||
1187 | 1200 | | |
1188 | 1201 | | |
1189 | 1202 | | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
1190 | 1209 | | |
1191 | 1210 | | |
1192 | 1211 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
| |||
0 commit comments