Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 411
Pascal Gula Avatar asked Apr 24 '17 16:04

Pascal Gula


1 Answers

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)
like image 102
Eryk Sun Avatar answered Sep 29 '22 03:09

Eryk Sun