multiprocessing.Process cannot be closed if the process is reaped elsewhere
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 36k
- Ø Merge
- 1 T. 9 Std.
- Gemergte PRs (30 T.)
- 558
Beschreibung
Here's a small reproducer where I start a multiprocessing.Process - then reap it with os.waitpid(
import os
import multiprocessing
import socket
import sys
ctx = multiprocessing.get_context("spawn")
def target(sock):
with sock:
sock.send(b"\x00")
sock.recv(1)
def main():
a, b = socket.socketpair()
with a, b:
with b:
proc = ctx.Process(target=target, args=(b, ))
proc.start()
assert a.recv(1) == b"\x00"
a.send(b"\x00")
os.waitpid(proc.pid, 0)
if sys.version_info >= (3, 7):
proc.close()
elif proc.is_alive():
raise RuntimeError("proc={proc} is still running".format(proc=proc))
if __name__ == "__main__":
sys.exit(main())
which results in:
Traceback (most recent call last):
File "/home/graingert/projects/distributed/demo.py", line 31, in <module>
sys.exit(main())
File "/home/graingert/projects/distributed/demo.py", line 25, in main
proc.close()
File "/home/graingert/miniconda3/lib/python3.9/multiprocessing/process.py", line 181, in close
raise ValueError("Cannot close a process while it is still running. "
ValueError: Cannot close a process while it is still running. You should first call join() or terminate().
One issue is the call to self._popen.poll() suppresses the OSError of a reaped process:
https://github.com/python/cpython/blob/b6558d768f19584ad724be23030603280f9e6361/Lib/multiprocessing/popen_fork.py#L26-L31
I think this can be fixed by checking the state of the multiprocessing.Process.sentinel instead of using os.waitpid
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Reproducer im Issue und untersuche Lib/multiprocessing/popen_fork.py, insbesondere die Behandlung von _popen.poll(), zusammen mit multiprocessing.Process.close(). Untersuche, wie ein extern reaped Prozess durch seinen Sentinel dargestellt wird, und vergleiche dies mit der auf waitpid basierenden Zustandserkennung. Als erledigt gilt die Aufgabe, wenn der Reproducer den Child-Prozess extern reapen und anschließend Process schließen kann, ohne den fälschlicherweise auf „noch laufend“ bezogenen ValueError.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- operating-systems
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100