-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I'm trying to use one of Python's os.exec* functions, specifically os.execvp().
On Linux, one can use this to replace the current process with a new one.
On Windows in PowerShell 7.4.6, it kind of works, but has some strange behaviour.
Minimal example
Here is a minimal Python example which attempts to replace the current process with a continuous ping command:
Linux/ZSH:
import os
os.execvp("ping", ['/usr/bin/ping', '1.1.1.1'])Behaviour: The current Python process is completely replaced by the ping process. When you press Ctrl+C, you are returned to the shell.
Windows/PowerShell:
import os
os.execvp("ping", ['C:\\Windows\\System32\\ping.EXE', '-t', '1.1.1.1'])Behaviour:
- Two lines of output from
ping. - Mysteriously, the PowerShell prompt is printed on the third line.
pingkeeps printing lines after this.- If you press
Ctrl+C, PowerShell prints the prompt, butpingis not stopped, and keeps printing output. - If you rapidly repeat
Ctrl+C, eventually the message will get through, andpingwill stop.
More complicated (but useful) example
The real reason I'm using execvp is to create an SSH wrapper script for az ssh vm:
https://github.com/swsphn/az-ssh-wrapper
I'm using something like the following (which works perfectly in Linux!):
import os
# Insert your own Azure VM IP address
os.execvp('az', ['"C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\wbin\\az.CMD"', 'ssh', 'vm', '--ip', '1.2.3.4'])Behaviour: This starts out the same way as the ping example above:
- PowerShell prints a prompt (unexpected!)
- The output from SSH appears.
- We successfully see the shell prompt on the VM.
- If I type any character, the input triggers my local PowerShell history completion, which causes chaos on my screen.
- If I press the
Ctrlkey, it triggers the following PowerShell exception:An error has occurred that was not properly handled. Additional information is shown below. The PowerShell process will exit. Unhandled exception. System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read. at System.ConsolePal.ReadKey(Boolean intercept) at Microsoft.PowerShell.Internal.VirtualTerminal.ReadKey() at Microsoft.PowerShell.PSConsoleReadLine.ReadOneOrMoreKeys() at Microsoft.PowerShell.PSConsoleReadLine.ReadKeyThreadProc() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location --- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) - After the above exception triggers, PowerShell is dead, but I still have a console connected to the remote shell on my VM, so I can use it as expected.
- When I exit the remote shell on the VM, I am returned to a dead PowerShell and the following message:
[process exited with code 3762504530 (0xe0434352)] You can now close this terminal with Ctrl+D, or press Enter to restart.
Alternative example which doesn't require an Azure VM
The more complicated example above appears to reproduce with WSL as well. For example:
import os
os.execvp('wsl', ['C:\\Windows\\System32\\wsl.EXE'])
Expected behavior
I expect to be able to replace the current process with another process without PowerShell glitching or crashing.
Actual behavior
PowerShell seems to be trying to control the terminal at the same time that the replacement process is trying to control it, which causes glitches and crashes.
Error details
See above.
Environment data
Name Value
---- -----
PSVersion 7.4.6
PSEdition Core
GitCommitId 7.4.6
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0