Repository navigation
Commit df6f4ed
authored
Stop spurious
* Detach OpenTelemetry context only on the thread that attached it
The tracing interceptor's best-effort detach checked that the attached
Context was still current, so a generator finalized by GC on another
thread would skip the detach instead of using a token that is invalid
there. OpenTelemetry's threading instrumentation, which strands enables
whenever an Agent is created, copies the same Context object into new
threads, so that check passes on the wrong thread and detach logs
"Failed to detach context". Record the attaching thread with the token
and skip the detach anywhere else.
The regression test runs the existing safe-detach scenario with the
instrumentation active; it fails without this change.
* Cover OpenTelemetryInterceptor and compare the attaching Thread object
OpenTelemetryPlugin installs OpenTelemetryInterceptor, whose four
attach/detach sites had the same context-identity guard. Move the helpers
into _context.py and use them from both interceptors. Compare the attaching
threading.Thread object rather than its id, which the OS can reuse. The
regression test is now a matrix over interceptor and threading
instrumentation; the OpenTelemetryInterceptor case fails without this.
* Detach where the token is valid, not where the thread matches
Workflow activations run on a thread pool while the asyncio task keeps its
contextvars.Context, so a context attached in one activation is legitimately
detached in a later one on another thread. The thread check skipped those
detaches and left the inner context attached for outer interceptors.
Let contextvars decide instead: perform the reset that
opentelemetry.context.detach performs and treat its ValueError for a foreign
Context as nothing to detach. Covers the thread change with a unit test on
both interceptors and a worker test whose executor alternates activations
between two threads.
* Patch AttachedContext.detach through monkeypatch in the leak test
mypy rejects assigning to a method (method-assign); pytest's monkeypatch
does the same thing and restores it on teardown.
* Make the attach/detach helper a context manager
attached_context(context) replaces the seven attach/try/finally/detach
sites; the guarded detach moves to _detach, which the leak test counts.
* Count the interceptors' own detaches in the activity leak test
The test patched opentelemetry.context.attach/detach process-wide, which
also counts the threading instrumentation's attach/detach around every
thread's run(). A pool thread started before the patch and exiting during
the test adds a detach with no counted attach (9 attaches vs 10 detaches
on 3.10 macos-arm). Record the interceptors' own detach results instead.Failed to detach context errors from the OpenTelemetry interceptors (#1911)1 parent cf11a1d commit df6f4ed
5 files changed
Lines changed: 316 additions & 110 deletions
File tree
- temporalio/contrib/opentelemetry
- tests/contrib/opentelemetry
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
39 | 44 | | |
40 | 45 | | |
41 | 46 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
183 | 184 | | |
184 | 185 | | |
185 | 186 | | |
186 | | - | |
187 | | - | |
| 187 | + | |
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | 222 | | |
226 | 223 | | |
227 | 224 | | |
| |||
556 | 553 | | |
557 | 554 | | |
558 | 555 | | |
559 | | - | |
560 | | - | |
| 556 | + | |
561 | 557 | | |
562 | 558 | | |
563 | 559 | | |
| |||
567 | 563 | | |
568 | 564 | | |
569 | 565 | | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | 566 | | |
578 | 567 | | |
579 | 568 | | |
| |||
644 | 633 | | |
645 | 634 | | |
646 | 635 | | |
647 | | - | |
648 | | - | |
649 | | - | |
650 | | - | |
651 | | - | |
652 | | - | |
653 | | - | |
654 | | - | |
655 | | - | |
656 | | - | |
657 | | - | |
658 | | - | |
659 | | - | |
660 | | - | |
661 | | - | |
662 | | - | |
663 | | - | |
664 | | - | |
665 | | - | |
666 | | - | |
667 | | - | |
668 | | - | |
669 | | - | |
670 | | - | |
671 | | - | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
672 | 653 | | |
673 | 654 | | |
674 | 655 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
125 | 126 | | |
126 | 127 | | |
127 | 128 | | |
128 | | - | |
129 | | - | |
| 129 | + | |
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | 151 | | |
155 | 152 | | |
156 | 153 | | |
| |||
334 | 331 | | |
335 | 332 | | |
336 | 333 | | |
337 | | - | |
338 | | - | |
| 334 | + | |
339 | 335 | | |
340 | 336 | | |
341 | 337 | | |
| |||
349 | 345 | | |
350 | 346 | | |
351 | 347 | | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | 348 | | |
356 | 349 | | |
357 | 350 | | |
| |||
368 | 361 | | |
369 | 362 | | |
370 | 363 | | |
371 | | - | |
372 | | - | |
| 364 | + | |
373 | 365 | | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | 366 | | |
378 | 367 | | |
379 | 368 | | |
| |||
501 | 490 | | |
502 | 491 | | |
503 | 492 | | |
504 | | - | |
505 | | - | |
| 493 | + | |
506 | 494 | | |
507 | | - | |
508 | | - | |
509 | | - | |
510 | 495 | | |
511 | 496 | | |
512 | 497 | | |
| |||
0 commit comments