Message390587
I've noticed an issue (or user error) in which Python a call that otherwise usually works in the __del__ step of a class will freeze when the Python interpreter is exiting.
I've attached sample code that I've ran against Python 3.9.1 on Windows 10.
The code below runs a process and communicates via the pipe.
class SubprocTest(object):
def run(self):
print("run")
proc_args = ["cmd.exe"]
self._process = subprocess.Popen(proc_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def __del__(self):
print("__del__")
if self._process is not None:
self.terminate()
def terminate(self):
print("terminate")
self._process.communicate(input=b"exit\n", timeout=1)
print("kill")
self._process.kill()
self._process = None
if __name__ == "__main__":
s = SubprocTest()
s.run()
del s
print("s done")
t = SubprocTest()
t.run()
print("t done")
Current output:
run
__del__
terminate
kill
s done
run
t done
__del__
terminate
<<<<<< hangs indefinitely here, even though timeout=1
Expected output:
run
__del__
terminate
kill
s done
run
t done
__del__
terminate
kill
In normal circumstances, when you del the object and force a run of __del__(), the process ends properly and the terminate() method completes.
When the Python interpreter exits, Python calls the __del__() method of the class. In this case, the terminate() never completes and the script freezes indefinitely on the communicate() |
|
Date |
User |
Action |
Args |
2021-04-09 05:25:49 | sylikc | set | recipients:
+ sylikc, paul.moore, tim.golden, zach.ware, steve.dower |
2021-04-09 05:25:49 | sylikc | set | messageid: <1617945949.93.0.237236734747.issue43784@roundup.psfhosted.org> |
2021-04-09 05:25:49 | sylikc | link | issue43784 messages |
2021-04-09 05:25:49 | sylikc | create | |
|