python / python/cpython

Worker management race conditions in `ThreadPoolExecutor` and `ProcessPoolExecutor`

Offen
#117,344 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

tests
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

We have tests that ThreadPoolExecutor and ProcessPoolExecutor reuse workers. The test wait for the previous task to complete before submitting the next task.

https://github.com/python/cpython/blob/29829b58a8328a7c2ccacaa74c1d7d120a5e5ca5/Lib/test/test_concurrent_futures/test_thread_pool.py#L44-L50

The problem is that the tasks are potentially completed before the thread or process is marked as idle. Calling Future.result() doesn't guarantee that there is an idle worker. This happens much more frequently with the GIL disabled, but it can happen with the GIL enabled too if you have an unlucky GIL switch.

https://github.com/python/cpython/blob/29829b58a8328a7c2ccacaa74c1d7d120a5e5ca5/Lib/concurrent/futures/thread.py#L81-L95

For example, you can reliably trigger the problem with the GIL enabled by adding a short time.sleep(0.01) immediately after the work_item.run() call in:
https://github.com/python/cpython/blob/29829b58a8328a7c2ccacaa74c1d7d120a5e5ca5/Lib/concurrent/futures/thread.py#L92

It's not clear to me if this is just a problem with the unit tests or an actual bug with the executor implementation.

Linked PRs
  • gh-117355

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den zitierten Wiederverwendungstests in Lib/test/test_concurrent_futures/test_thread_pool.py und dem Code für den Worker-Lebenszyklus in Lib/concurrent/futures/thread.py; untersuche auch die entsprechenden ProcessPoolExecutor-Pfade. Ermittle, ob die Race Condition eine stärkere Testsynchronisierung oder eine Änderung am Executor erfordert, führe anschließend die concurrent-futures-Testsuite aus und stelle sicher, dass die Wiederverwendung von Workern sowohl bei der Ausführung mit aktiviertem GIL als auch bei der Ausführung ohne GIL zuverlässig funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend, distributed-systems, testing-qa
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.