New submission from Kevin M <syl...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue43784>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to