Skip to content

Nushell hangs CPU in debug mode #9859

Closed

Description

@sophiajt

Describe the bug

I'm seeing Nushell hang occasionally in macOS as I'm developing. I managed to catch it and attach a debugger to see where it was:

  * frame #0: 0x0000000190a4cacc libsystem_kernel.dylib`read + 8
    frame #1: 0x0000000102423170 nu`crossterm::event::sys::unix::file_descriptor::FileDesc::read::hfbd23575769f9401 + 28
    frame #2: 0x00000001024216b4 nu`_$LT$crossterm..event..source..unix..mio..UnixInternalEventSource$u20$as$u20$crossterm..event..source..EventSource$GT$::try_read::hbbc0fab5c8f663e4 + 320
    frame #3: 0x00000001024226f8 nu`crossterm::event::read::InternalEventReader::poll::h36c3e8abbd07d8a9 + 360
    frame #4: 0x000000010241ea0c nu`crossterm::event::poll_internal::h0048b8dda3aba103 + 172
    frame #5: 0x000000010242307c nu`crossterm::cursor::sys::unix::read_position_raw::hd8205425dcf4d786 + 140
    frame #6: 0x0000000102d99548 nu`reedline::painting::painter::Painter::initialize_prompt_position::h536603ca530d65c6 + 84
    frame #7: 0x0000000102d8a024 nu`reedline::engine::Reedline::read_line::hf3ee01bf075925f5 + 80
    frame #8: 0x00000001024f09f8 nu`nu_cli::repl::evaluate_repl::h0038740f8bd590bd + 9328
    frame #9: 0x00000001024ad2d0 nu`nu::run::run_repl::h9453fd09d0b40fa3 + 384

How to reproduce

  1. Start in zsh as root shell
  2. Run a release version of nushell
  3. From inside nushell, run cargo run in the nushell repo
  4. Immediately, exit

Expected behavior

There shouldn't be any hang on exit.

Screenshots

No response

Configuration

key value
version 0.83.1
branch main
commit_hash 8403fff
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.69.0 (84c898d65 2023-04-16)
rust_channel 1.69.0-aarch64-apple-darwin
cargo_version cargo 1.69.0 (6e9a83356 2023-04-12)
build_time 2023-07-29 05:50:17 +12:00
build_rust_channel debug
allocator standard
features default, sqlite, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. added
    status:needs-triage
    on Jul 29, 2023
  2. WindSoilder commented on Jul 30, 2023

    @WindSoilder
    Contributor

    I get the same issue on MacOS as well after I exit nushell

  3. sophiajt commented on Jul 30, 2023

    @sophiajt
    ContributorAuthor

    @WindSoilder - is then when you do cargo run or when you install nushell and run nu?

  4. WindSoilder commented on Jul 31, 2023

    @WindSoilder
    Contributor

    when I do cargo run. I can't reproduce it on released nu binary.
    I suddendly found that I can reproduce it in debug mode everytime.

  5. sophiajt commented on Aug 1, 2023

    @sophiajt
    ContributorAuthor

    bisect found it was caused by #9693

  6. sophiajt commented on Aug 1, 2023

    @sophiajt
    ContributorAuthor

    Repro:

    • Start in zsh as root shell
    • Run a release version of nushell
    • From inside nushell, run cargo run in the nushell repo
    • Immediately, exit
  7. fdncred commented on Aug 1, 2023

    @fdncred
    Contributor

    I repro'd it too with your steps on MacOS.

  8. changed the title [-]Nushell occasionally hangs (in debug mode?)[/-] [+]Nushell hangs CPU in debug mode[/+] on Aug 1, 2023
  9. added a commit that references this issue on Aug 1, 2023
    d25df9c
  10. added this to the v0.84.0 milestone on Aug 2, 2023
  11. IanManske commented on Aug 2, 2023

    @IanManske
    Member

    Sorry about that! Does this happen in WSL as well? I am unable to reproduce this on Linux.

    To make sure I'm running the repro steps properly:

    Steps
    • start a new terminal with zsh as the initial shell (also tried with root permissions)
    • cd to nushell repo (on main before revert 778a00e)
    • run release build: cargo run -r -- -n or system nu v0.83.0: nu -n
    • now inside nushell, run: cargo run -- -n
    • at this point I have the following process tree:
      55565 kitty (kitty zsh)
      • 55587 zsh
        • 55624 nu (target/release/nu -n)
          • 55636 nu (target/debug/nu -n)
    • now inside the debug nushell, run: exit (no hang?)
    • now back in the release build nushell, run: exit
    • now back in zsh

    My suspicion is that in foreground.rs we are not reaching line 178 for some reason. (This puts nushell back in control of the terminal.) Unfortunately I do not have a device running macOS to test, but I've made a branch with some debug prints if anyone is able to provide output.

    On my machine I get the following:

    DEBUG: My pid/pgid is 79624 and 79624 is controlling the terminal
    > cargo run -- -n
    DEBUG: setting foreground from child, pid=79652 pgrp=0
    DEBUG: setting foreground from parent, pid=79652 pgrp=0
    DEBUG: My pid/pgid is 79652 and 79652 is controlling the terminal
    > exit
    DEBUG: dropping foreground child...
    DEBUG: dropping last foreground child...
    DEBUG: 79652 controls the terminal, restoring control to 79624
    > exit
    
  12. fdncred commented on Aug 2, 2023

    @fdncred
    Contributor

    I'll try to repro in WSL.

  13. fdncred commented on Aug 2, 2023

    @fdncred
    Contributor

    I cannot repro it in WSL. I tried a few different ways unsuccessfully. I tried with kgx, wezterm, and Windows Terminal. Now it makes me wonder if the problem is more macos-centric.

    I'm trying cargo test --workspace now in WSL, which is what @sholderbach had problems with, iirc.

    UPDATE: No issues with cargo test --workspace in WSL. All this testing was on 778a00e

  14. IanManske commented on Aug 2, 2023

    @IanManske
    Member

    Thanks for all the testing!

    Yeah, there may be macOS specific behavior that I'm unaware of that's causing this. I'd have to do more research, but in the meantime, if someone could see if the debug prints that I mentioned before match on macOS, that would be much appreciated! Specifically, I am interested to see if the tcsetpgrp on line 178 is being run, and, if not, which if statement is preventing it from being run.

  15. fdncred commented on Aug 2, 2023

    @fdncred
    Contributor

    Hey @IanManske, I tried your branch. This is what I get with cargo r.

    ❯ cargo r
        Finished dev [unoptimized + debuginfo] target(s) in 0.28s
         Running `target/debug/nu`
    DEBUG: My pid/pgid is 55863 and 55863 is controlling the terminal
         __  ,
     .--()°'.' Welcome to Nushell,
    '|, . ,'   based on the nu language,
     !_-(_\    where all data is structured!
    
    Please join our Discord community at https://discord.gg/NtAbbGn
    Our GitHub repository is at https://github.com/nushell/nushell
    Our Documentation is located at https://nushell.sh
    Tweet us at @nu_shell
    Learn how to remove this at: https://nushell.sh/book/configuration.html#remove-welcome-message
    
    It's been this long since Nushell's first commit:
    4yrs 2months 23days 6hrs 13mins 3secs 354ms 412µs
    
    Startup Time: 534ms 155µs 833ns
    
    DEBUG: dropping foreground child...
    DEBUG: dropping last foreground child...
     assets  │  crates │  scripts │  target │  wix        │  Cargo.toml         │  codecov.yml     │  Cross.toml │  README.md           │  toolkit.nu
     benches │  docker │  src     │  tests  │  Cargo.lock │  CODE_OF_CONDUCT.md │  CONTRIBUTING.md │  LICENSE    │  rust-toolchain.toml │
    
    toolkit module detected...
    activating toolkit module with `use toolkit.nu`
    ~/s/nushell-ian   debug-hang  ≣                                                                                                      0823  06:12:15 PM
    ❯ exit <-- this just hangs
    
  16. fdncred commented on Aug 2, 2023

    @fdncred
    Contributor

    And here it is with cargo run -- -n

    ❯ cargo run -- -n
        Finished dev [unoptimized + debuginfo] target(s) in 0.19s
         Running `target/debug/nu -n`
    DEBUG: My pid/pgid is 56038 and 56038 is controlling the terminal
         __  ,
     .--()°'.' Welcome to Nushell,
    '|, . ,'   based on the nu language,
     !_-(_\    where all data is structured!
    
    Please join our Discord community at https://discord.gg/NtAbbGn
    Our GitHub repository is at https://github.com/nushell/nushell
    Our Documentation is located at https://nushell.sh
    Tweet us at @nu_shell
    Learn how to remove this at: https://nushell.sh/book/configuration.html#remove-welcome-message
    
    It's been this long since Nushell's first commit:
    4yrs 2months 23days 6hrs 15mins 36secs 235ms 729µs
    
    Startup Time: 160ms 817µs 625ns
    ~/src/nushell-ian > exit <-- hangs
    
  17. fdncred commented on Aug 2, 2023

    @fdncred
    Contributor

    oops, both those tests were on MacOS. Maybe you wanted them ran on WSL since that whole block is disabled on macos with #[cfg(all(target_family = "unix", not(target_os = "macos")))]

  18. IanManske commented on Aug 2, 2023

    @IanManske
    Member

    Oh, I did not see that! Thanks for pointing that out.

    Then that is the problem. If we are not supporting job control on MacOs (for now?) then acquire_terminal in terminal.rs needs to match and have the same cfg guard (or something like this).

  19. fdncred commented on Aug 2, 2023

    @fdncred
    Contributor

    Why wouldn't we support it in MacOS? I figured it would work similarly as Linux.

  20. IanManske commented on Aug 2, 2023

    @IanManske
    Member

    I don't know, I would have assumed so as well. The relevant comment says // Note: we exclude macos because the techniques below seem to have issues in macos 13 currently. which is a little vague.

    I guess I'll have to setup a macOS virtual machine to do some testing. I'll open an issue or PR with the findings/fixes once I'm done.

  21. fdncred commented on Aug 3, 2023

    @fdncred
    Contributor

    ok, sounds good. thanks for your help! we appreciate it.

  22. added a commit that references this issue on Aug 3, 2023
    9a0720f
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

    status:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions