Skip to content

Powershell fails every prompt in pixi shell after cancelling (ctrl+c) #514

Closed

Description

@Wumpf

Checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pixi, using pixi --version.

Reproducible example

(see description)

Issue description

  • open powershell
  • navigate to folder with a pixi.toml file
  • pixi shell
  • sleep(1000) (or any other long running command
  • ctrl+c to cancel while it is still running
  • press enter, observe how error code remains -1073741510. In my case the oh-my-posh shell plugin also gets.. issues:
    image

Expected behavior

continue operating as normal

Activity

  1. changed the title [-]Powershell fails every command in pixi shell after cancelling (ctrl+c) one[/-] [+]Powershell fails every command in pixi shell after cancelling (ctrl+c)[/+] on Nov 28, 2023
  2. changed the title [-]Powershell fails every command in pixi shell after cancelling (ctrl+c)[/-] [+]Powershell fails every prompt in pixi shell after cancelling (ctrl+c)[/+] on Nov 28, 2023
  3. ruben-arts commented on Nov 28, 2023

    @ruben-arts
    Contributor

    I can recreate it! Will take a look at this later.

  4. ruben-arts commented on Dec 11, 2023

    @ruben-arts
    Contributor

    Added the help wanted label as we don't know where this is coming from.

  5. traversaro commented on Apr 14, 2024

    @traversaro
    Contributor

    I am experiencing this also on Command Prompt. Interestingly, it seems that the pixi environment is actually active when the prompt does not have the (envname) modifier, while the (envname) modifier is present, the environment is not active, see :

    (pixitest) C:\src\pixitest>set | findstr "PIXI"
    PIXI_CACHE_DIR=D:\cache\rattler
    PIXI_ENVIRONMENT_NAME=default
    PIXI_ENVIRONMENT_PLATFORMS=win-64
    PIXI_EXE=C:\Users\straversaro\AppData\Local\pixi\bin\pixi.exe
    PIXI_PROJECT_MANIFEST=C:\src\pixitest\pixi.toml
    PIXI_PROJECT_NAME=pixitest
    PIXI_PROJECT_ROOT=C:\src\pixitest
    PIXI_PROJECT_VERSION=0.1.0
    PIXI_PROMPT=(pixitest)
    
    (pixitest) C:\src\pixitest>
    
    (pixitest) C:\src\pixitest>
    (pixitest) C:\src\pixitest>
    (pixitest) C:\src\pixitest>
    (pixitest) C:\src\pixitest>^C
    C:\src\pixitest>
    (pixitest) C:\src\pixitest>
    C:\src\pixitest>
    (pixitest) C:\src\pixitest>set | findstr "PIXI"
    PIXI_CACHE_DIR=D:\cache\rattler
    
    C:\src\pixitest>set | findstr "PIXI"
    PIXI_CACHE_DIR=D:\cache\rattler
    PIXI_ENVIRONMENT_NAME=default
    PIXI_ENVIRONMENT_PLATFORMS=win-64
    PIXI_EXE=C:\Users\straversaro\AppData\Local\pixi\bin\pixi.exe
    PIXI_PROJECT_MANIFEST=C:\src\pixitest\pixi.toml
    PIXI_PROJECT_NAME=pixitest
    PIXI_PROJECT_ROOT=C:\src\pixitest
    PIXI_PROJECT_VERSION=0.1.0
    PIXI_PROMPT=(pixitest)
    
    (pixitest) C:\src\pixitest>set | findstr "PIXI"
    PIXI_CACHE_DIR=D:\cache\rattler
    
    C:\src\pixitest>set | findstr "PIXI"
    PIXI_CACHE_DIR=D:\cache\rattler
    PIXI_ENVIRONMENT_NAME=default
    PIXI_ENVIRONMENT_PLATFORMS=win-64
    PIXI_EXE=C:\Users\straversaro\AppData\Local\pixi\bin\pixi.exe
    PIXI_PROJECT_MANIFEST=C:\src\pixitest\pixi.toml
    PIXI_PROJECT_NAME=pixitest
    PIXI_PROJECT_ROOT=C:\src\pixitest
    PIXI_PROJECT_VERSION=0.1.0
    PIXI_PROMPT=(pixitest)
    
    (pixitest) C:\src\pixitest>
    

    In this case, pixi is version 0.18.0, and C:\src\pixitest is an empty pixi environment.

  6. traversaro commented on Apr 29, 2024

    @traversaro
    Contributor

    This is particularly annoying as it appears to happen when you use Ctrl+C to close a running program, like:

    pixi shell
    runsomeprogram :: You press Ctrl+C to exit
    :: now the shell is corrupted

    I guess most users on Windows are probably using pixi run , so they are not encountering this. I think I will eventually try to tackle this problem, as it is quite annoying. Anyone has some pointers to relevant part of the codes it may be worth start looking into?

  7. ruben-arts commented on Apr 29, 2024

    @ruben-arts
    Contributor

    He @traversaro, please enlighten us!

    The initialization of the powershell can be found here:

    pixi/src/cli/shell.rs

    Lines 37 to 70 in 58ab590

    fn start_powershell(
    pwsh: PowerShell,
    env: &HashMap<String, String>,
    prompt: String,
    ) -> miette::Result<Option<i32>> {
    // create a tempfile for activation
    let mut temp_file = tempfile::Builder::new()
    .suffix(".ps1")
    .tempfile()
    .into_diagnostic()?;
    let mut shell_script = ShellScript::new(pwsh.clone(), Platform::current());
    for (key, value) in env {
    shell_script.set_env_var(key, value).into_diagnostic()?;
    }
    temp_file
    .write_all(shell_script.contents().into_diagnostic()?.as_bytes())
    .into_diagnostic()?;
    // Write custom prompt to the env file
    temp_file.write(prompt.as_bytes()).into_diagnostic()?;
    // close the file handle, but keep the path (needed for Windows)
    let temp_path = temp_file.into_temp_path();
    let mut command = std::process::Command::new(pwsh.executable());
    command.arg("-NoLogo");
    command.arg("-NoExit");
    command.arg("-File");
    command.arg(&temp_path);
    let mut process = command.spawn().into_diagnostic()?;
    Ok(process.wait().into_diagnostic()?.code())
    }

    This is the prompt:

    pixi/src/prompt.rs

    Lines 37 to 43 in 58ab590

    pub fn get_powershell_prompt(env_name: &str) -> String {
    format!(
    "$old_prompt = $function:prompt\n\
    function prompt {{\"({}) $($old_prompt.Invoke())\"}}",
    env_name
    )
    }

    This is all the code around activation: https://github.com/prefix-dev/pixi/blob/main/src/activation.rs

    Would be happy to help if you feel like you found the culprit!

  8. Petzep commented on May 1, 2024

    @Petzep

    I have the same issue.
    When the terminal is corrupted, it behaves as if it is running both the pixi-shell and the powershell on the foreground.
    The input from the keyboard is sent to the pixi-shell and powershell on alternating keystrokes.
    This can be demonstrated by getting the process ID of powershell and the pixi-shell, then corrupting the pixi-shell and checking the pids of both processes.

    Steps:

    • Start a powershell terminal
    • Get the powershell pid: echo $pid
    • Start the pixi shell: pixi shell
    • Get the pixi-shell pid: echo $pid
    • Corrupt the terminal as the OP suggested: sleep(1000) Enter and within 1 seconds Ctrl + C
    • type echo $pid but type every keystroke twice
    • Observe: Every time a key is pressed the cursor alternates between PS> and (pixi-env)PS>
    • Press Enter twice (one for each process)
    • Observe: One PID is the powershell, one is the pixi-shell

    Are there any Ctrl + C hooks that could cause this behavior in the pixi-shell?

  9. maphew commented on May 30, 2024

    @maphew

    great sleuthing @Petzep, thanks! It's great to have a comprehenisble explanation, hopefully that reveals a path to a fix.

    Only new bit I can add to the conversation is that this is not console software dependent. It happens for me in CMD and Powershell, and doesn't matter if run within default Windows, Windows Terminal or ConEmu.

  10. traversaro commented on Aug 22, 2024

    @traversaro
    Contributor

    At least on my environments, the activation of pixi on windows take ~1/2 seconds, that makes it complicated to use pixi run as a replacement to pixi shell for terminal-intensive workflows. As I would really like to use pixi in some projects in which there is an extensive use of Windows, I am not currently exploring the workaround of using conda to activate the pixi environment:

    conda activate .pixi/envs/default
    

    That is far from perfect (you are basically ignoring the activation table of pixi.toml), but at least works with Ctrl+C .

    Another alternative is to use https://github.com/synapticarbors/pixi2ces to create a conda lock file and create a different conda environment, but to be honest I do not love it as it as the same downside of the previous method, with the additional overhead of having a different conda environment, that can quickly diverse from the pixi one unless you pay attention.

  11. ruben-arts commented on Aug 22, 2024

    @ruben-arts
    Contributor

    Does `eval "$(pixi shell-hook -s bash)" work?

  12. traversaro commented on Aug 22, 2024

    @traversaro
    Contributor

    Does `eval "$(pixi shell-hook -s bash)" work?

    Probably this is more related to #417 ?

    Anyhow, I just tried and it hangs for ~1/2 seconds, and then it exists but it does not seem to me that the environemnt is actually activated.

  13. traversaro commented on Aug 22, 2024

    @traversaro
    Contributor

    Anyhow, I just tried and it hangs for ~1/2 seconds, and then it exists but it does not seem to me that the environemnt is actually activated.

    Actually the environment is activated, but the PATH variable does not contain the pixi-related parts.

  14. ruben-arts commented on Aug 22, 2024

    @ruben-arts
    Contributor

    Ah because of that, issue in #417 with the path...

  15. arthur-st commented on Oct 12, 2024

    @arthur-st

    Running into this as well, both in the command prompt and in PowerShell. The simplest repo is like this:

    1. pixi shell
    2. python
    3. press Ctrl+C
    4. press Enter a couple of times

    CMD will show an incorrectly exited Python interpreter, whereas PowerShell will just lock up after a few Enter presses (on a beefy machine).

  16. added a commit that references this issue on Oct 14, 2024
    f6b9d11
  17. traversaro commented on Jan 29, 2025

    @traversaro
    Contributor

    @Luca0101 noticed again this problem in 0.40.2, we are investigating the problem and if we are able to reproduce we will open a new issue.

  18. traversaro commented on Jan 30, 2025

    @traversaro
    Contributor

    @Luca0101 noticed again this problem in 0.40.2, we are investigating the problem and if we are able to reproduce we will open a new issue.

    We isolated the problem, it appears to be a regression of 0.40.1 w.r.t. to 0.40.0, we reported it in #3037 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedMaintainers would like community input

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions