[New-bugs-announce] [issue43784] [Windows] interpreter hangs indefinitely on subprocess.communicate during __del__ at script exit

Kevin M report at bugs.python.org
Fri Apr 9 01:25:49 EDT 2021


New submission from Kevin M <sylikc at gmail.com>:

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()

----------
components: Library (Lib), Windows
files: win_subprocess_hang.py
messages: 390587
nosy: paul.moore, steve.dower, sylikc, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: [Windows] interpreter hangs indefinitely on subprocess.communicate during __del__ at script exit
versions: Python 3.9
Added file: https://bugs.python.org/file49947/win_subprocess_hang.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43784>
_______________________________________


More information about the New-bugs-announce mailing list