Skip to content
Advertisement

Python running as Windows Service: OSError: [WinError 6] The handle is invalid

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

JavaScript

which causes the following error:

JavaScript

Advertisement

Answer

Line 1117 in subprocess.py is:

JavaScript

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.

JavaScript

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

JavaScript
Advertisement