Commit d481370
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 662354a commit d481370
2 files changed
Lines changed: 22 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| 45 | + | |
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| |||
1169 | 1171 | | |
1170 | 1172 | | |
1171 | 1173 | | |
1172 | | - | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
1173 | 1185 | | |
1174 | 1186 | | |
1175 | 1187 | | |
| |||
1262 | 1274 | | |
1263 | 1275 | | |
1264 | 1276 | | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
1265 | 1283 | | |
1266 | 1284 | | |
1267 | 1285 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
85 | 88 | | |
86 | 89 | | |
87 | 90 | | |
| |||
0 commit comments