A subprocess.Popen object contains many resources: pipes, a child process (its pid, and an handle on Windows), etc.
IMHO it's not safe to rely on the destructor to release all resources. I would prefer to release resources explicitly. For example, use proc.wait() or "with proc:".
Attached patch emits a ResourceWarning in Popen destructor if the status of the child process was not read yet.
The patch changes also _execute_child() to set the returncode on error, if the child process raised a Python exception. It avoids to emit a ResourceWarning on this case.
The patch fixes also unit tests to release explicitly resources. self.addCleanup(p.stdout.close) is not enough: use "with proc:" instead.
TODO: fix also the Windows implementation of _execute_child(). |