Skip to content

subprocess.py throw "The handle is invalid" error on duplicating the STD_INPUT_HANDLE #60662

Closed

Description

@kartlee05
mannequin
BPO 16458
Nosy @gpshead, @tjguk
Superseder
  • bpo-3905: subprocess failing in GUI applications on Windows
  • Files
  • subprocess.zip: Test files to reproduce the issue and patch
  • 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 = <Date 2015-12-05.19:44:48.218>
    created_at = <Date 2012-11-12.02:49:27.405>
    labels = ['type-bug', 'OS-windows']
    title = 'subprocess.py throw "The handle is invalid" error on duplicating the STD_INPUT_HANDLE'
    updated_at = <Date 2015-12-05.19:44:48.217>
    user = 'https://bugs.python.org/kartlee05'

    bugs.python.org fields:

    activity = <Date 2015-12-05.19:44:48.217>
    actor = 'eryksun'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-12-05.19:44:48.218>
    closer = 'eryksun'
    components = ['Windows']
    creation = <Date 2012-11-12.02:49:27.405>
    creator = 'kartlee05'
    dependencies = []
    files = ['27966']
    hgrepos = []
    issue_num = 16458
    keywords = []
    message_count = 9.0
    messages = ['175416', '175522', '175526', '175571', '175572', '175573', '175587', '175637', '222956']
    nosy_count = 5.0
    nosy_names = ['gregory.p.smith', 'tim.golden', 'BreamoreBoy', 'kartlee05', 'hercs']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '3905'
    type = 'behavior'
    url = 'https://bugs.python.org/issue16458'
    versions = ['Python 2.7']

    Activity

    1. kartlee05 commented on Nov 12, 2012

      kartlee05mannequin
      MannequinAuthor

      Please download subprocess.zip and extract the archive. Run -

      perl test.pl

      you will notice 'invalid handle' error on duplicating STD_INPUT_HANDLE in python. This is observed in Windows XP SP3. Vista SP2 and higher platforms doesn't show this failure when I checked.

      "test.pl" spawn a python process "test.py" using CREATE_NEW_PROCESS_GROUP flag. Before it does, it close STDIN and create a listening socket. Perl under the hood, convert the socket handle to file descriptor using open_osfhandle(..) call. When it does this, the C runtime set the standard input handle to this descriptor for console app. [ See msvcr* osfinfo.c code - SetStdHandle( STD_INPUT_HANDLE, (HANDLE)value ) ]. As a result, when you get the STD_INPUT_HANDLE, you will get the new one. Duplicating this handle, hence throw the invalid handle error in Windows XP SP3.

      When I looked at MSDN for DuplicateHandle(..), it is mentioned not to duplicate socket handle using this call. So I suspect something going wrong in XP platform. To fix this issue, I have come up with a patch - python-2143_patch. Please take a look and let me know your comments.

      -Karthik

    2. tjguk commented on Nov 13, 2012

      @tjguk
      Member

      I'm sorry, but I genuinely can't see what you're trying to say here. If you believe that there's a bug in Python's standard library, can you show a reproducible *Python* testcase for it, please (using the pywin32 modules if that helps) and state clearly what you think is wrong.

    3. kartlee05 commented on Nov 13, 2012

      kartlee05mannequin
      MannequinAuthor

      Hi Tim,

      Thanks for your reply.

      I added test case using perl and python since it was easy to reproduce using perl socket module and show the issue happening with python's subprocess.py. There is definitely an action required in subprocess.py to catch 'invalid handle error' and my attached patch handle this situation.

      I can come up with a test case like perl does to create a listening socket and map the socket handle to a file descriptor. You will see this issue *only* when the open_osfhandle(..) is used to map the handle to a file descriptor.

      -Karthik

    4. tjguk commented on Nov 14, 2012

      @tjguk
      Member

      On 13/11/2012 20:57, Karthk Rajagopalan wrote:

      I added test case using perl and python since it was easy to
      reproduce using perl socket module and show the issue happening with
      python's subprocess.py. There is definitely an action required in
      subprocess.py to catch 'invalid handle error' and my attached patch
      handle this situation.

      So, because subprocess.call includes a call to DuplicateHandle(stdin),
      that call will fail if stdin is currently a handle to a socket.
      Is that right?

      Your patch fails to apply cleanly to the hg tip, because Gregory P.
      Smith has been making changes in this very piece of code over the last
      few days. I'll add him as interested to the call as someone who can make
      a call as to the suitability of your patch.

      I can come up with a test case like perl does to create a listening
      socket and map the socket handle to a file descriptor. You will see
      this issue *only* when the open_osfhandle(..) is used to map the
      handle to a file descriptor.

      If this is to go ahead, we will need a test case which can run as part
      of the Python stdlib tests, and perl is not part of that stdlib!

    5. kartlee05 commented on Nov 14, 2012

      kartlee05mannequin
      MannequinAuthor

      Hi Tim,

      Thanks for your reply.

      Yes, DuplicateHandle(..) seem to fail if the handle is to a socket under XP SP3.

      Can you point me to the guidelines about submitting patch so I can merge my change in main branch and upload it?

      We build python from source and doesn't use pywin32 as of now. So I downloaded them yesterday to come up with a test case in python which you can use in your test bed.

      -Karthik

    6. tjguk commented on Nov 14, 2012

      @tjguk
      Member
    7. gpshead commented on Nov 14, 2012

      @gpshead
      Member

      Sorry I can't help on the windows side of things. I don't have access to any windows systems.

    8. tjguk commented on Nov 15, 2012

      @tjguk
      Member

      Karthk, if you can run up an up-to-date patch and a test I'm willing to review and commit it, otherwise this one will lie quiet.

    9. BreamoreBoy commented on Jul 13, 2014

      BreamoreBoymannequin
      Mannequin

      Is XP under Python support now? I'm sure I've read that it isn't but can't find a reference.

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      OS-windowstype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions