Title: [Windows] subprocess: close the handle when the process completes
Type: resource usage Stage: patch review
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords: patch

Created on 2019-06-26 11:53 by vstinner, last changed 2019-06-26 13:40 by vstinner.

Pull Requests
URL Status Linked Edit
PR 14391 closed vstinner, 2019-06-26 11:57
Messages (5)
msg346603 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-06-26 11:53
subprocess.Popen uses _winapi.CreateProcess() to spawn a child process. It stores the process handle using a Handle class. Popen never explicitly releases the handle, it rely on Handle.__del__ when the Popen object is detroyed (when Popen._handle attribute is cleared).

As discussed in bpo-37380, we could call explicitly CloseHandle(handle) as soon as the child process completes, to release resources in the Windows kernel.

Attached PR implements this fix.
msg346604 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-06-26 11:58
IMHO it's safe to backport PR 14391 to Python 3.7 and 3.8.
msg346607 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-06-26 12:08
See also bpo-37380: subprocess.Popen._cleanup() "The handle is invalid" error when some old process is gone.
msg346617 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-06-26 13:38
We may backport the change to Python 2.7 as well, but in this case terminate() must be changed to test returncode:

        def terminate(self):
            """Terminates the process."""
            # Don't terminate a process that we know has already died.
            if self.returncode is not None:
msg346618 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-06-26 13:40
terminate() was changed by:

commit a0c9caad66f01328155177180df1c46fe7c62e57
Author: Gregory P. Smith <>
Date:   Sun Nov 15 18:19:10 2015 -0800

    Fix issue #6973: When we know a subprocess.Popen process has died, do
    not allow the send_signal(), terminate(), or kill() methods to do
    anything as they could potentially signal a different process.
Date User Action Args
2019-06-26 13:40:23vstinnersetmessages: + msg346618
2019-06-26 13:38:43vstinnersetmessages: + msg346617
2019-06-26 12:08:13vstinnersetmessages: + msg346607
2019-06-26 11:58:18vstinnersetmessages: + msg346604
versions: + Python 3.7, Python 3.8
2019-06-26 11:57:54vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request14205
2019-06-26 11:53:14vstinnercreate