Commit 157575a
authored
### Rationale for this change
`LaunchWorkersUnlocked` appends an entry to `state_->workers_` before constructing the thread
that owns it, and only the worker itself erases that entry. If the `std::thread` constructor
fails, the entry stays behind with nothing left to remove it, so `Shutdown` waits forever on
`workers_.empty()`. The destructor takes the same path. The failure also escaped `SpawnReal`
after `tasks_queued_or_running_` had been incremented, so `WaitForIdle` never returned either,
and once stale entries filled `workers_` to capacity the pool stopped launching workers while
`Spawn` still returned OK for tasks nothing would run.
### What changes are included in this PR?
`LaunchWorkersUnlocked` returns a `Status`. A failed thread construction erases the entry it had
reserved and returns an error, which `SpawnReal` and `SetCapacity` propagate. The task counter is
incremented after the launch rather than before, so a failed launch cannot leak a count.
### Are these changes tested?
`TestThreadPool.FailedWorkerLaunch` lowers `RLIMIT_NPROC` to 1, spawns a task, restores the soft
limit, and then checks the pool reports no workers and no tasks and still shuts down. It skips on
macOS, where `RLIMIT_NPROC` counts processes rather than threads, and skips anywhere else the
lowered limit does not stop thread creation, such as under root.
### Are there any user-facing changes?
Yes. `Spawn`, `Submit` and `SetCapacity` used to let a `std::system_error` escape when the OS
refused a new thread. They return an error `Status` now. `ThreadPool::Make` is unaffected, since
worker threads are only started on demand and a new pool starts none.
* GitHub Issue: #48137
Authored-by: Advit Arora <advitarora2@gmail.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent 987e231 commit 157575a
3 files changed
Lines changed: 49 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
580 | 581 | | |
581 | 582 | | |
582 | 583 | | |
583 | | - | |
| 584 | + | |
584 | 585 | | |
585 | 586 | | |
586 | 587 | | |
| |||
692 | 693 | | |
693 | 694 | | |
694 | 695 | | |
695 | | - | |
| 696 | + | |
696 | 697 | | |
697 | 698 | | |
698 | 699 | | |
699 | 700 | | |
700 | 701 | | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
705 | 711 | | |
| 712 | + | |
706 | 713 | | |
707 | 714 | | |
708 | 715 | | |
| |||
729 | 736 | | |
730 | 737 | | |
731 | 738 | | |
732 | | - | |
733 | | - | |
| 739 | + | |
734 | 740 | | |
735 | 741 | | |
736 | | - | |
| 742 | + | |
737 | 743 | | |
| 744 | + | |
738 | 745 | | |
739 | 746 | | |
740 | 747 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
496 | 496 | | |
497 | 497 | | |
498 | 498 | | |
| 499 | + | |
499 | 500 | | |
500 | 501 | | |
501 | 502 | | |
| |||
507 | 508 | | |
508 | 509 | | |
509 | 510 | | |
510 | | - | |
| 511 | + | |
511 | 512 | | |
512 | 513 | | |
513 | 514 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
832 | 833 | | |
833 | 834 | | |
834 | 835 | | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
835 | 866 | | |
836 | 867 | | |
837 | 868 | | |
| |||
0 commit comments