Commit dd5488f
chore(internal): make PeriodicThread.awake() non-blocking on stopped threads
Two failure modes are fixed:
1. Hang on awake() after a completed stop()/join(). The previous
handshake used a one-shot `_served` Event: the worker set it once on
exit and then went away. If awake() ran afterwards it cleared
`_served`, signalled the request, and waited for an `_served->set()`
that would never come. Because the wait ran with the GIL released,
not even a Python-level signal handler could interrupt it; only
SIGKILL recovered the process.
2. Deadlock between an in-flight awake() and a periodic callback that
calls stop() on its own thread (the Timer._periodic pattern in
ddtrace/internal/periodic.py). An earlier fix attempt had stop()
acquire `_awake_mutex`, but awake() held that same mutex while
waiting on `_served`: the worker-thread stop() then blocked on the
mutex before the callback could return and the worker could signal
completion — both threads waited forever.
Replace the `_served` Event handshake with a `std::condition_variable`
(`_awake_cond`) plus `_awake_waiting` / `_awake_served` flags. awake()
now waits via `cv::wait`, which releases `_awake_mutex` while blocked,
so stop() can synchronize with request publication without deadlocking
a worker-thread stop() call.
awake() also checks `_stopping && !_skip_shutdown` under the mutex and
raises RuntimeError in the permanently-stopped case. `_skip_shutdown`
keeps the legitimate `_before_fork()/awake()/_after_fork()` flow
working: the restarted worker consumes the queued AWAKE request.
The worker only notifies the awake waiter on paths where the request
can actually be considered served (AWAKE consumed, or permanent
stop/error/finalization exit). A fork-stop deliberately does not
notify: the pending AWAKE survives for the worker restarted by
`_after_fork()` to serve.
Internal API; no release note.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 56e77c7 commit dd5488f
2 files changed
Lines changed: 159 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
388 | 388 | | |
389 | 389 | | |
390 | 390 | | |
391 | | - | |
392 | 391 | | |
393 | 392 | | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
394 | 400 | | |
395 | 401 | | |
396 | 402 | | |
| |||
461 | 467 | | |
462 | 468 | | |
463 | 469 | | |
464 | | - | |
465 | 470 | | |
466 | 471 | | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
467 | 475 | | |
468 | 476 | | |
469 | 477 | | |
| |||
496 | 504 | | |
497 | 505 | | |
498 | 506 | | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
499 | 520 | | |
500 | 521 | | |
501 | 522 | | |
| |||
577 | 598 | | |
578 | 599 | | |
579 | 600 | | |
| 601 | + | |
580 | 602 | | |
581 | 603 | | |
582 | 604 | | |
583 | 605 | | |
| 606 | + | |
584 | 607 | | |
585 | 608 | | |
586 | 609 | | |
| |||
592 | 615 | | |
593 | 616 | | |
594 | 617 | | |
595 | | - | |
596 | | - | |
| 618 | + | |
| 619 | + | |
597 | 620 | | |
598 | 621 | | |
599 | 622 | | |
600 | 623 | | |
601 | 624 | | |
602 | 625 | | |
603 | | - | |
| 626 | + | |
| 627 | + | |
604 | 628 | | |
605 | 629 | | |
606 | 630 | | |
| |||
616 | 640 | | |
617 | 641 | | |
618 | 642 | | |
619 | | - | |
620 | | - | |
| 643 | + | |
| 644 | + | |
621 | 645 | | |
622 | 646 | | |
623 | | - | |
624 | | - | |
625 | | - | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
626 | 652 | | |
627 | 653 | | |
628 | 654 | | |
| |||
698 | 724 | | |
699 | 725 | | |
700 | 726 | | |
| 727 | + | |
701 | 728 | | |
702 | 729 | | |
703 | | - | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
704 | 747 | | |
705 | | - | |
706 | | - | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
707 | 753 | | |
708 | | - | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
709 | 757 | | |
710 | 758 | | |
711 | 759 | | |
| |||
720 | 768 | | |
721 | 769 | | |
722 | 770 | | |
723 | | - | |
724 | | - | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
725 | 783 | | |
726 | 784 | | |
727 | 785 | | |
| |||
827 | 885 | | |
828 | 886 | | |
829 | 887 | | |
830 | | - | |
831 | 888 | | |
832 | 889 | | |
833 | 890 | | |
| |||
934 | 991 | | |
935 | 992 | | |
936 | 993 | | |
937 | | - | |
938 | 994 | | |
939 | 995 | | |
| 996 | + | |
940 | 997 | | |
941 | 998 | | |
942 | 999 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
51 | 125 | | |
52 | 126 | | |
53 | 127 | | |
| |||
351 | 425 | | |
352 | 426 | | |
353 | 427 | | |
354 | | - | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
355 | 433 | | |
356 | 434 | | |
357 | 435 | | |
| |||
471 | 549 | | |
472 | 550 | | |
473 | 551 | | |
474 | | - | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
475 | 557 | | |
476 | 558 | | |
477 | 559 | | |
| |||
0 commit comments