Python Running as Windows Service: Oserror: [Winerror 6] the Handle Is Invalid

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

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)

pyinstaller error: OSError: [WinError 6] The handle is invalid

This error apparently is thrown, because of this:

Line 1117 in subprocess.py is: p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)

The service processes do not have a STDIN associated with them (TBC).

This problem 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)

Reference: OSError: (WinError 6) The handle is Invalid


In your problem:

subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True, stdin=subprocess.DEVNULL).stdout.decode()

WinError 6: The handle is invalid from python check_output spawn in Electron app

On Windows, subprocess.Popen tries to duplicate non-zero standard handles and fails if they're invalid. You can redirect stdin and stderr to NUL. For example:

import os

try:
from subprocess import DEVNULL
except ImportError:
DEVNULL = os.open(os.devnull, os.O_RDWR)

output = subprocess.check_output(cmd, stdin=DEVNULL, stderr=DEVNULL)

WinError 6 - The handle is invalid with executable but code works on debug mode

The problem is with psa1_path variable

C:/Users/${USERNAME}\projects\demo\cmd\powershell.ps1 -m -v 'CC 2018' -wait windowstyle hidden

This variable has parameters. And the subprocess.Popen use it as a string, then you have to set shell=True so Popen will use this string as a complete shell cmd.

p = subprocess.Popen(['C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe',
'-ExecutionPolicy',
'Unrestricted',
psa1_path],
stdout=f, shell=True, stdin=subprocess.DEVNULL)

Don't forget to add stdin arg because it's also throw the [WinError 6] The handle is invalid

So why the code works with debug code and not with executable :

It's mainly because PyCharm does additionnal configuration and setup behind the scenes when it comes to running a program.

Because when you’re going from IDE to runtime, you need additional hooks to get things going. pyinstaller doesn’t do the subprocess the same way as pyCharm for the shell part



Related Topics



Leave a reply



Submit