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

electron, pyinstaller, python

Solution

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)

Problem

I have an Electron app `xxx.exe` which is spawning an executable created from PyInstaller `yyy.exe`. In, `yyy.exe`, I am finally trying to launch a `git` cmd via `subprocess.check_output()`. Sadly, the call to `check_output()` throws `[WinError 6] The handle is invalid`. If `yyy.exe` is directly launched on the command line, everything is running fine. The issue is only happening on Windows. My assumption is that there are some checks on `stdin` that trigger the exception because running through an Electron app doesn't provide any Stdin. Any hints would be appreciated! Thanks in advance!

Original source