This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: subprocess failing in GUI applications on Windows
Type: behavior Stage: test needed
Components: Library (Lib), Windows Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Weird, amaury.forgeotdarc, benjamin.peterson, borisompol, chn, denversc, eryksun, kevinwatters, paul.moore, steve.dower, terry.reedy, tim.golden, tlesher, trentm, twhitema, zach.ware
Priority: normal Keywords:

Created on 2008-09-18 23:07 by twhitema, last changed 2022-04-11 14:56 by admin.

Messages (27)
msg73408 - (view) Author: Todd Whiteman (twhitema) Date: 2008-09-18 23:07
I'm getting a 'The handle is invalid' error when using subprocess.Popen
in Python 2.5 (and 2.6). If any of the stdio handles are somehow invalid
or not real io handles (fd is not 0, 1 or 2), and you are not telling
subprocess to PIPE all of the handles, then subprocess throws the
following exception.

The easiest way to reproduce this is using run PythonWin from a Windows
Command Prompt:
and then use the following 2 subprocess code lines below:

import subprocess
p = subprocess.Popen(["python", "-c", "print 32"], stdin=None,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python26\lib\", line 588, in init_
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Python26\lib\", line 703, in _get_handles
    p2cread = self._make_inheritable(p2cread)
  File "C:\Python26\lib\", line 748, in _make_inheritable
WindowsError: [Error 6] The handle is invalid

Specifying PIPE for all subprocess.Popen handles is the only way I've
found to get around this problem:

p = subprocess.Popen(["python", "-c", "print 32"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
('32\r\n', '')
msg73409 - (view) Author: Todd Whiteman (twhitema) Date: 2008-09-18 23:08
This seems to be somewhat related to issue1124861:
msg73420 - (view) Author: Trent Mick (trentm) * (Python committer) Date: 2008-09-19 05:19
The failure is in the DuplicateHandle call that subprocess makes on, in
this case, the stdin handle (as returned by
`GetStdHandle(STD_INPUT_HANDLE)`) call earlier.

Two cases here:

1. When this is run in a subsystem:windows process (like PythonWin or
IDLE) that is launched from Windows Explorer (e.g. from a Start Menu
shortcut or Desktop shortcut) then `GetStdHandle(STD_INPUT_HANDLE)`
returns None. 

> If an application does not have associated standard handles,
> such as a service running on an interactive desktop, and has
> not redirected them, the return value is NULL.

In this case you *don't* get the error that Todd described, because the
code path taken in then use CreatePipe for the `p2cread`
variable on which `DuplicateHandle` is called.

2. However, when the subsystem:windows process is launched from the
cmd.exe shell the `GetStdHandle` call returns a value -- in Todd's and
my testing, the value 3.

The code path in then calls `DuplicateHandle` on this in
`Popen._make_inheritable`. This fails with traceback Todd posted.

My *guess* at what the problem is stems from this comment in the MSDN
docs on console handles:

> A process can use the DuplicateHandle function to create a 
> duplicate console handle that has different access or 
> inheritability from the original handle. Note, however,
> that a process can create a duplicate console handle only
> for its own use. This differs from other handle types (such
> as file, pipe, or mutex objects), for which DuplicateHandle
> can create a duplicate that is valid for a different process.

My guess is that the stdin handle (3) is inherited from the shell
(cmd.exe) and attempting to `DuplicateHandle` on it violates the clause
that you can on dupe a console handle of your own. I'm not sure of that

Anyone else have more light to shed on this?

If this is the case I'm not sure what a possible or good solution could
be for subprocess.
msg73427 - (view) Author: jmf (jmfauth) Date: 2008-09-19 14:41
I do not really know if this is related to this, but I suspect yes.
On my w2k+sp4 box, swiss french, Python 3.0rc1, IDLE does not start or
more precisely it starts by opening the following message box:

Subprocess Startup Error
IDLE's subprocess didn't make connection.  Either IDLE can't start a
subprocess or personal firewall software is blocking the connection.

It is certainly neither a firewall issue, nor a tkinter issue (tkinter 
applications are working fine.)

No problem with the 3.0b2 realease, 3.0b3: not tested.
msg73428 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-09-19 15:27
jmfauth: please try to run idle from a command prompt:

> cd <path_to_python3.0rc1>
> python Lib/idlelib/

Do you see interesting output there?
msg73433 - (view) Author: jmf (jmfauth) Date: 2008-09-19 17:28
Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

C:\>cd python30

C:\Python30>python Lib/idlelib/
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python30\lib\idlelib\", line 76, in main
AttributeError: 'Thread' object has no attribute 'set_daemon'


Sorry if I can help here, things like socket and subprocess are not my
cup of tea.
msg73460 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-09-20 08:15
Benjamin, I think you're responsible.
msg73461 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-09-20 08:15
There are also instances of set_daemon left in socketserver and
multiprocessing/dummy. How is that possible?
msg73463 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-09-20 12:53
The idle problem has already been fixed, and I got the socket server one
in r66520.
msg73468 - (view) Author: jmf (jmfauth) Date: 2008-09-20 18:12
Just for information and from an end user perspective.

I have tried to replace the from the original 3.0rc1
distribution by the as proposed by Benjamin Peterson

Script difference (line 568):

        if self.daemon_threads:
            t.daemon = True


        if self.daemon_threads:

Unfortunately, no luck, I'm still getting exactly the same error
messages, the msg box and the raised AttributeError:

AttributeError: 'Thread' object has no attribute 'set_daemon'
msg73496 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-09-21 07:34
Jean-Michel, you'll need to apply the idlelib/ patch; replace



    sockthread.daemon = True

around line 75.
msg73514 - (view) Author: Todd Whiteman (twhitema) Date: 2008-09-21 16:47
Uh, the Idle bug reported by Jean-Michel is a completely different bug
(please see the first three messages of this report).

Please re-open this bug, as the subprocess issue I have reported is
still outstanding.
msg73517 - (view) Author: jmf (jmfauth) Date: 2008-09-21 18:11
> Georg Brandl

Thanks, patch applied on w2k+sp4 box, swiss french, Python 3.0rc1.
IDLE is working fine.
msg74804 - (view) Author: (Weird) Date: 2008-10-15 14:40
Georg Brandl thank you, i was having the same problem, but now is fixed
msg133002 - (view) Author: Denver Coneybeare (denversc) * Date: 2011-04-05 03:04
I just re-tested this in cpython trunk at changeset and the issue does not appear to be reproducible.

I first launched IDLE by running "python lib\idlelib\".  Then I entered the following:

Python 3.3a0 (default, Apr  2 2011, 21:55:40) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.getwindowsversion()
sys.getwindowsversion(major=5, minor=1, build=2600, platform=2, service_pack='Service Pack 3')
>>> import subprocess
>>> PIPE = subprocess.PIPE
>>> p = subprocess.Popen(["python", "-c", "print(32)"], stdin=None, stdout=PIPE, stderr=PIPE)
>>> p
<subprocess.Popen object at 0x00F85610>
>>> p.returncode

Popen() did not raise any exceptions.  I also tried in Python 2.7 and the Popen called succeeded there as well.  So my inability to reproduce the issue does not necessarily mean that the issue is fixed, but rather that it is likely dependent on the version of Windows on which Python is running.

That being said, the linked issue issue1124861 logs what appears to be the same issue and it was fixed in Python 2.6.  So maybe I'm just not old enough to have encountered this issue :)  Anyways, this issue probably can be closed as either a duplicate or fixed incidentally.
msg133052 - (view) Author: Todd Whiteman (twhitema) Date: 2011-04-05 16:49
I still see this problem (in Python 2.7.1 and Python 3.1.2).

When testing using idle, you'll need to launch using "pythonw.exe", i.e.:

pythonw.exe lib\idlelib\
msg133106 - (view) Author: Denver Coneybeare (denversc) * Date: 2011-04-06 03:03
Ahh okay.  I've reproduced it in trunk at changeset 053bc5ca199b.

As suggested, I ran: PCBuild\pythonw.exe lib\idlelib\

Python 3.3a0 (default, Apr  2 2011, 21:55:40) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import subprocess
>>> subprocess.Popen(["python", "-c", "print(32)"], stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    subprocess.Popen(["python", "-c", "print(32)"], stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "C:\dev\cpython\cpython\lib\", line 732, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\dev\cpython\cpython\lib\", line 903, in _get_handles
    p2cread = self._make_inheritable(p2cread)
  File "C:\dev\cpython\cpython\lib\", line 946, in _make_inheritable
WindowsError: [Error 6] The handle is invalid

So, the issue is definitely not fixed.
msg153234 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-02-12 23:14
In 3.2.2, starting IDLE from a Command Prompt, I get the same error as Denver. Starting IDLE 'normally', from the icon, there is no error. So even though Command Prompt detaches pythonw in some way and gives me a new prompt to do something else, there is something different about the pythonw process that trips up subprocess.

Changing ["python", "-c", "print(32)"] to ["notepad"] gives the same result. Running in the interactive interpreted eliminated the error. So does changing stdin=None to stdin=subprocess.PIPE eliminates the error and brings up Notepad, which seems to operate normally. (I am guessing that it ignores stdin, etc.).

I see that that the latter was known in the original post and plausibly diagnosed by Trent Mick, but never fixed because it was not obvious how to do so.

Can the WindowsError be caught in subproccess, and if it is this error (6), follow the alternate patch that is used for NULL returns? Could that cause worse problems that the current situation?

If this is unfixable due to Windows behavior, perhaps we should say something in the doc.
msg220206 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-10 22:47
I cannot reproduce this problem with 3.4.1 or 3.5.a0 on Windows 7.
msg220218 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-06-11 00:15
Mark, you did not specify which of the variations you tried ;-) I reproduce as I said in my last message.

C:\Programs\Python34>pythonw -m idlelib

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [
>>> import subprocess
>>> p = subprocess.Popen(["python", "-c", "print(32)"], stdin=None,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Traceback (most recent call last):
OSError: [WinError 6] The handle is invalid

Change startup command from pythonw to python, no error, and it runs.
>>> p.communicate()
(b'32\r\n', b'')
msg220219 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-11 00:51
*facepalm* looks like I've run it with the wrong exe, sorry about the noise :-(
msg245640 - (view) Author: Boris (borisompol) Date: 2015-06-22 17:13
Probably the same issue: everything works when called from command line, but when called via a desktop shortcut I get 
OSError: [WinError 6] The handle is invalid

Changing pythonw to python solved the problem. ...this could be expected behavior. If anyone is interested I can attach my test files.
msg338056 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-03-16 07:02
I reran test with IDLE started 4 ways:
I am closing this as out of date because the problem seems to have been fixed elsewise.

3.8.0a2+ 32 bit debug python, built today, opened from python icon, then idle started with 'import idlelib.idle'
3.8.0a2 64 bit installed, directly from IDLE icon
3.7.2 python -m idlelib
3.7.2 pythonw -m idlelib

#  and it ran correctly all 4 times.
>>> import subprocess
>>> p = subprocess.Popen(["python", "-c", "print(32)"], stdin=None,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> p.communicate()
(b'32\r\n', b'')

Anyone with the same or similar failure on current Python (now 3.7, 3.8) should open a new issue and give careful details -- release or build, system, startup procedure, and a reproducing example only using stdlib modules (no PythonWin).
msg338066 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-03-16 08:26
> I am closing this as out of date because the problem seems to have been 
> fixed elsewise.

This is a problem only in Windows 7, which we should have addressed years ago. It's common enough that we shouldn't leave it as just an unresolved third-party issue. I discussed the problem and suggested a workaround in issue 25492, which I closed as a duplicate of this issue four years ago.
msg338070 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-03-16 09:07
Since Win 10 was released a year after Jun 2014, I must have still been on Win 7 when I saw the failure. I believe there have some patches to filenos and subprocess since, but I don't know the relation between filenos and file handles.  A current test on a Win 7 machine similar to what I did  would still be good.  Perhaps it would have been better to reopen #25492, but I am adding the current Windows people nosy here.
msg338075 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-03-16 09:40
> A current test on a Win 7 machine similar to what I did  
> would still be good.  

Python 3.8 is the last version to support Windows 7 (i.e. NT 6.1). It should get bug fixes through Spring 2021, so we have a couple more years to fix this.

We should constrain the fix to just older versions of Windows (prior to 6.2) and the classic console file handles (i.e. the lower 2 bits are set such as 3, 7, 11) that they use. Otherwise a bad standard handle should fail the call.
msg388098 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-03-04 15:59
For whatever the reason and Windows version, it's still the case that _get_handles() should work around any bad standard handles in the current process. In bpo-25492, I suggested checking os.get_handle_inheritable(). That's too permissive. It should require a valid file handle, checked via _winapi.GetFileType(). For example:

    if stdin is None:
        p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)
        if p2cread is not None:
            except OSError:
                p2cread = None
Date User Action Args
2022-04-11 14:56:39adminsetgithub: 48155
2021-03-04 16:02:03eryksunlinkissue29829 superseder
2021-03-04 15:59:11eryksunsetmessages: + msg388098
components: + Library (Lib)
versions: + Python 3.9, Python 3.10, - Python 2.7, Python 3.7
2019-03-16 09:40:26eryksunsetmessages: + msg338075
2019-03-16 09:07:30terry.reedysetstatus: closed -> open

nosy: + paul.moore, tim.golden, zach.ware, steve.dower, - georg.brandl, jmfauth
messages: + msg338070

stage: test needed
2019-03-16 08:26:26eryksunsetversions: + Python 3.7, Python 3.8, - Python 3.3, Python 3.4, Python 3.5
resolution: out of date ->
messages: + msg338066

type: behavior
stage: resolved -> (no value)
2019-03-16 07:02:37terry.reedysetstatus: open -> closed
resolution: out of date
messages: + msg338056

stage: needs patch -> resolved
2019-03-16 04:47:31eryksunsetnosy: + eryksun
2019-03-15 21:58:37BreamoreBoysetnosy: - BreamoreBoy
2015-12-05 19:44:48eryksunlinkissue16458 superseder
2015-12-05 19:43:17eryksunlinkissue25492 superseder
2015-06-22 17:13:33borisompolsetnosy: + borisompol

messages: + msg245640
versions: + Python 3.3
2014-06-11 00:51:36BreamoreBoysetmessages: + msg220219
2014-06-11 00:15:29terry.reedysetstage: test needed -> needs patch
messages: + msg220218
versions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
2014-06-10 22:47:28BreamoreBoysetnosy: + BreamoreBoy
messages: + msg220206
2012-02-13 13:35:37tleshersetnosy: + tlesher
2012-02-12 23:14:31terry.reedysetversions: + Python 2.7, Python 3.2, Python 3.3, - Python 3.0
nosy: + terry.reedy

messages: + msg153234

stage: test needed
2011-12-20 18:07:45chnsetnosy: + chn
2011-04-06 03:03:40denverscsetmessages: + msg133106
2011-04-05 16:49:49twhitemasetmessages: + msg133052
2011-04-05 03:04:23denverscsetnosy: + denversc
messages: + msg133002
2008-10-15 14:40:57Weirdsetnosy: + Weird
messages: + msg74804
2008-09-21 18:11:02jmfauthsetmessages: + msg73517
2008-09-21 17:00:31georg.brandlsetstatus: closed -> open
resolution: fixed -> (no value)
2008-09-21 16:47:54twhitemasetmessages: + msg73514
2008-09-21 07:34:50georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg73496
versions: + Python 3.0, - Python 2.6, Python 2.5
2008-09-20 23:41:56kevinwatterssetnosy: + kevinwatters
2008-09-20 18:12:47jmfauthsetmessages: + msg73468
2008-09-20 12:53:40benjamin.petersonsetassignee: benjamin.peterson ->
messages: + msg73463
2008-09-20 08:15:55georg.brandlsetmessages: + msg73461
2008-09-20 08:15:27georg.brandlsetassignee: benjamin.peterson
messages: + msg73460
nosy: + benjamin.peterson, georg.brandl
2008-09-19 17:28:51jmfauthsetmessages: + msg73433
2008-09-19 15:27:08amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg73428
2008-09-19 14:41:29jmfauthsetnosy: + jmfauth
messages: + msg73427
2008-09-19 05:19:21trentmsetmessages: + msg73420
2008-09-18 23:08:08twhitemasetmessages: + msg73409
2008-09-18 23:07:01twhitemacreate