Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] subprocess: close the handle when the process completes #81591

Closed
vstinner opened this issue Jun 26, 2019 · 6 comments
Closed

[Windows] subprocess: close the handle when the process completes #81591

vstinner opened this issue Jun 26, 2019 · 6 comments
Labels
3.12 bugs and security fixes OS-windows performance Performance or resource usage stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

BPO 37410
Nosy @vstinner
PRs
  • bpo-37410: subprocess closes the process handle when done #14391
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-06-26.11:53:14.562>
    labels = ['3.8', '3.7', 'library', '3.9', 'performance']
    title = '[Windows] subprocess: close the handle when the process completes'
    updated_at = <Date 2019-06-26.13:40:23.561>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2019-06-26.13:40:23.561>
    actor = 'vstinner'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-06-26.11:53:14.562>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37410
    keywords = ['patch']
    message_count = 5.0
    messages = ['346603', '346604', '346607', '346617', '346618']
    nosy_count = 1.0
    nosy_names = ['vstinner']
    pr_nums = ['14391']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue37410'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @vstinner
    Copy link
    Member Author

    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.

    @vstinner vstinner added 3.9 only security fixes stdlib Python modules in the Lib dir performance Performance or resource usage labels Jun 26, 2019
    @vstinner
    Copy link
    Member Author

    IMHO it's safe to backport PR 14391 to Python 3.7 and 3.8.

    @vstinner vstinner added 3.7 (EOL) end of life 3.8 only security fixes labels Jun 26, 2019
    @vstinner
    Copy link
    Member Author

    See also bpo-37380: subprocess.Popen._cleanup() "The handle is invalid" error when some old process is gone.

    @vstinner
    Copy link
    Member Author

    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:
                    return
                ...

    @vstinner
    Copy link
    Member Author

    terminate() was changed by:

    commit a0c9caa
    Author: Gregory P. Smith <greg@krypto.org>
    Date: Sun Nov 15 18:19:10 2015 -0800

    Fix issue bpo-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.
    

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel iritkatriel added OS-windows 3.12 bugs and security fixes and removed 3.9 only security fixes 3.8 only security fixes 3.7 (EOL) end of life labels Sep 12, 2022
    @vstinner
    Copy link
    Member Author

    vstinner commented Nov 3, 2022

    Sadly, I don't have the bandwidth to work on this issue, I just close it.

    @vstinner vstinner closed this as completed Nov 3, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.12 bugs and security fixes OS-windows performance Performance or resource usage stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants