28

I have a Python script, which is running as a Windows Service. The script forks another process with:

with subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:

which causes the following error:

OSError: [WinError 6] The handle is invalid
   File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 911, in __init__
   File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 1117, in _get_handles

2 Answers 2

44

Line 1117 in subprocess.py is:

p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)

which made me suspect that service processes do not have a STDIN associated with them (TBC)

This troublesome code can be avoided by supplying a file or null device as the stdin argument to popen.

In Python 3.x, you can simply pass stdin=subprocess.DEVNULL. E.g.

subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)

In Python 2.x, you need to get a filehandler to null, then pass that to popen:

devnull = open(os.devnull, 'wb')
subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)
13
  • 1
    Service executables (e.g. pythonservice.exe) are run detached (i.e. not attached to an instance of conhost.exe), in which case GetStdHandle should return NULL, not INVALID_HANDLE_VALUE.
    – Eryk Sun
    Oct 18, 2016 at 20:25
  • 2
    A similar error is common when running via pythonw.exe. Prior to Windows 8, a pythonw.exe process has console handle values in its standard handles, but they're invalid since there's no attached console. subprocess raises an error when it tries to call DuplicateHandle on the invalid handle.
    – Eryk Sun
    Oct 18, 2016 at 20:28
  • 5
    Ok, I think I have the edgiest of edge cases here, so I'm not sure anyone else will find it - My script has been compiled into a standalone .exe using PyInstaller. The script isn't the actual service - a C#/.net app is the service which forks the standalone .exe. The service has a standalone mode, which, when executed as a desktop user, works as expected. Oct 18, 2016 at 20:49
  • 2
    similar error comes in Python 3.6 for subprocess.run( cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE) when running inside nested (subprocess.Popen, Windows Application, python via DLL). adding stdin = subprocess.DEVNULL fixed it.
    – hardmooth
    Feb 27, 2018 at 7:16
  • 1
    @SmitJohnth looks like it has a count option. Set it to 1 and loop. Aug 2, 2021 at 17:28
3

Add stdin=subprocess.PIPE like:

with subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.