question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSNativeCommandPreserveBytePipe hangs on piping to python scripts in Windows

See original GitHub issue

Prerequisites

Steps to reproduce

I’m trying out the preview feature PSNativeCommandPreserveBytePipe, and it seems to work in most cases but I’ve noticed it hangs when piping to Python scripts in certain situations. I have both Python 3.9.7 and 3.11.0 installed and the standard python launcher. I have some simple python scripts:

repr.py, which should print the escaped binary contents of stdin:

import sys
print(repr(sys.stdin.buffer.read()).lstrip('b'))

echo.py, which just prints it’s arguments:

import sys
print(' '.join(sys.argv[1:]))

I also have cat.exe and echo.exe binaries from Cygwin 64-bit as well as WSL2 (Ubuntu).

The following commands hang with PSNativeCommandPreserveBytePipe enabled, until hitting Ctrl+C:

.\echo.py hello | .\repr.py
echo.exe hello | .\repr.py
wsl echo hello | .\repr.py
'hello' | cat.exe | .\repr.py
'hello' | wsl cat | .\repr.py
[byte[]](1, 2, 3) | cat.exe | .\repr.py
[byte[]](1, 2, 3) | wsl cat | .\repr.py

However, if I prefix the repr.py script with python.exe or py.exe (the python launcher) they work as expected:

> .\echo.py hello | py .\repr.py
'hello\r\n'
> [byte[]](1, 2, 3) | cat.exe | python .\repr.py
'\x01\x02\x03'

Also piping strings or bytes directly from Powershell to the un-prefixed script works:

> 'hello' | .\repr.py
'hello\r\n'
> [byte[]](1, 2, 3) | .\repr.py
'\x01\x02\x03'

So it seems that this only effects native-to-native commands involving the Python launcher launched implicitly via file association as the target of the pipe. I haven’t tried using other scripting languages to see if it effects other implicitly launched binaries. I confirmed that this effects both Python 3.9 and 3.11.

Also assoc and ftype show the .py file association as:

> cmd /c assoc .py
.py=Python.File
> cmd /c ftype Python.File
Python.File="C:\WINDOWS\py.exe" "%L" %*

Expected behavior

The expected behavior for the first 5 hanging commands is to print `'hello\r\n'`, and the last 2 to print `'\x01\x02\x03'`.

Actual behavior

The commands hang and nothing happens until I hit Ctrl+C, which stops the command with no output.

Running the commands with PSNativeCommandPreserveBytePipe disabled does not hang and shows the legacy behavior (bytes are passed as line-separated strings).

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0-preview.4
PSEdition                      Core
GitCommitId                    7.4.0-preview.4
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
rmccampbellcommented, Jul 1, 2023

I believe with the preview feature all native-to-native pipelines are treated as binary data, so no distinction is needed. Between Powershell commands and native binaries it depends on the type, so a byte array e.g. [byte[]](1,2,3) is passed as raw binary input. (There are some commands like Get-Content that can return byte arrays with the -AsByteStream flag).

0reactions
SeeminglySciencecommented, Jul 5, 2023

I can repro, thanks for the report @rmccampbell! Lookin’ into it

Read more comments on GitHub >

github_iconTop Results From Across the Web

PSNativeCommandPreserveByte...
PSNativeCommandPreserveBytePipe hangs on piping to python scripts in Windows #24030. Sign in to view logs · Sign in to view logs ...
Read more >
Python Popen hangs, yet same command in Windows cmd ...
Write to a temporary file because pipe redirection seems broken with tempfile.NamedTemporaryFile(mode="w+") as tmp_out, tempfile.
Read more >
Issue 34780: [Windows] Hang on startup if stdin refers to a ...
This hangs because msvcrt calls PeekNamedPipe() if stdin refers to a pipe. Ironically, this fstat() call is completely useless on Windows ...
Read more >
Python hangs after some time, Continues when pressed ...
I ran this script from CMD using the following command: python test.py ... Right-click the title bar of your console window.
Read more >
Python subprocess (using Popen) hangs indefinitely and ...
"Subprocess.PIPE will hang indefinitely if stdout is more than 65000 characters." "The problem is in the communicate function. It silently fails ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found