Skip to content

no indication when process exits with a signal #5960

Closed

Description

@toffaletti

Describe the bug

I have a process that crashes with SIGTRAP on macOS. Running the executable under nushell gives no indication the process exited because of a fatal signal.

Other shells have more to say when this occurs.
fish: terminated by signal SIGTRAP (Trace or breakpoint trap)
bash: Trace/BPT trap: 5
zsh: zsh: trace trap

How to reproduce

  1. Run a command that exits with SIGTRAP (such as a binary that is not properly code signed on macOS)

Expected behavior

I expected nushell to give some indication the process exited with a signal.

Screenshots

No response

Configuration

key value
version 0.65.0
branch
commit_hash
build_os macos-x86_64
build_target x86_64-apple-darwin
rust_version rustc 1.61.0 (fe5b13d68 2022-05-18)
rust_channel stable-x86_64-apple-darwin
cargo_version cargo 1.61.0 (a028ae42f 2022-04-29)
pkg_version 0.65.0
build_time 2022-07-05 14:34:47 -07:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. added
    category:enhancement
    on Jul 5, 2022
  2. rgwood commented on Jul 6, 2022

    @rgwood
    Contributor

    Thanks for the detailed issue - it's good to know how other shells handle this.

    If anyone wants to work on this, I think it would be relatively straightforward to add some code around here:

    if status.core_dumped() {

    I personally don't have a good understanding of which fatal signals we should log (maybe all of them?).

  3. toffaletti commented on Jul 7, 2022

    @toffaletti
    ContributorAuthor
  4. rgwood commented on Jul 7, 2022

    @rgwood
    Contributor

    I was hoping to implement this today, but it's more complex than I thought - the code I linked to before doesn't quite work as expected, and I think we need to overhaul how we pass exit codes/statuses around.

  5. toffaletti commented on Jul 7, 2022

    @toffaletti
    ContributorAuthor

    I took a shot at it as well and noticed the same thing and more:

    1. The issue you mention in Return error when external command core dumped #5908 which is essentially that the error reporting only happens if the exit status is received 100ms after the thread in spawned.
    2. I tried to add an exit status ListStream property to PipelineData::ExternalStream similar to how exit_code is implemented so in
      stack.add_env_var("LAST_EXIT_CODE".into(), v);
      nu could report the exit status or signal in an environment var as well. That would give more flexibility on how to handle it, but also adds some complexity.
    3. I wasn't confident I'd be doing the right thing for Windows, which has an exit status but no signals. std::os::unix::process::ExitStatusExt provides from_raw and into_raw to be able to shove the raw exit status into nu::protocol::value::Value and use ListStream. However, std::os::windows::process::ExitStatusExt does not provide into_raw only from_raw. Maybe calling code on Windows is good enough.
  6. rgwood commented on Jul 8, 2022

    @rgwood
    Contributor

    Nice, thanks. I've reverted #5908.

    I tried to add an exit status ListStream property to PipelineData::ExternalStream similar to how exit_code is implemented

    I think this is on the right track but I'm not 100% certain what the type of a new exit_status property should be. I guess we have 2 options:

    1. Keep using ListStream, coerce exit status to a Value::Int using into_raw on Unix and code() on Windows
    2. Pass around an actual ExitStatus struct (or some lazy wrapper around it)

    I'll bring this up in the Nu Discord (#design-discussion channel), maybe someone else will have a strong opinion.

  7. rgwood commented on Jul 8, 2022

    @rgwood
    Contributor

    (not saying you should wait for an answer btw - if you have an opinion about which of those options to choose and the time to put together a PR, feel free)

  8. rgwood commented on Jul 8, 2022

    @rgwood
    Contributor

    A suggestion from @kubouch in Discord:

    We could make it a record and store the record into the $env.LAST_EXIT_CODE instead of just the integer. To get the integer code, you'd call $env.LAST_EXIT_CODE.code but you could query other info, like $env.LAST_EXIT_CODE.signal or whatever. (We should probably rename it to LAST_EXIT_STATUS at that point.)

  9. toffaletti commented on Jul 8, 2022

    @toffaletti
    ContributorAuthor

    One issue I'm running into is that there are multiple places in the code where the method to execute externals is called and then exit_code is taken off the ListStream and pushed into the LAST_EXIT_CODE environment var. They are spread across multiple crates as well, some in nu-cli/src/util.rs (these seem to handle single commands) and in crates/nu-engine/src/eval.rs (this seems to handle multi-command pipelines). There is also a case in nu-cli/src/eval_file.rs that uses exit_code in print_table_or_error that I'm not sure if I need to care about. This would be much easier if the handling of exit code was consolidated into a single place, and perhaps that refactor should happen first if it makes sense what I'm saying.

  10. flying-sheep commented on Jul 13, 2022

    @flying-sheep
    Contributor

    A good programming language will throw an error by default if it encounters one and only swallow them if explicitly told (in our case by a do -i { ... }). $env.LAST_EXIT_CODE is a misfeature, external commands exiting with an exit code != 0 should have a nushell error derived from them.

    One of the reason I considered newshell was that I thought it it didn’t have horrible bullshit like “check some global variable after you did a thing to see if the thing succeeded”

  11. kubouch commented on Jul 13, 2022

    @kubouch
    Contributor

    Non-zero exit code, unfortunately, is not a guarantee a program encountered an error. For example ImageMagick's compare will return 1 on success, 2 on error. There is no reliable way to detect errors in an external program because it ultimately depends on the developer of the program how they decide what each error code means.

    We used to have a config option that would throw an error on a non-error exit code, but I think it was removed. I used it for a while but in practice, it was too annoying to have a program throw errors even though it completed successfully.

  12. flying-sheep commented on Jul 13, 2022

    @flying-sheep
    Contributor

    Pretend I said EXIT_SUCCESS, not 0 (the two are equal on POSIXy systems though). ImageMagick’s compare will return with EXIT_SUCCESS when the images are similar, with EXIT_FAILURE (1 on POSIX) if they’re different and 2 on error.

    That’s modelled after diff so people can do if [diff/compare a b]; then echo 'identical/similar'; else echo 'different; fi'

    I think the ideal way to design interaction with potentially weird systems:

    1. It should be easiest and most straighforward to interact with things that behave normally (here: using EXIT_SUCCESS for success and EXIT_FAILURE for failure)
    2. Give users a way to identify and work around things that work weirdly design (here: Using complete, users can wrap a badly designed external command like this:)
    def compare [
      ...options  # too bad we can’t express this yet
      image: string
      reconstruct: string
      difference: string = 'null'
    ] {
      let ret = (/usr/bin/compare ([$options, $image, $reconstruct, $difference] | flatten) | complete)
      if $ret.exit_code in [0, 1] {
        $ret.exit_code == 0
      } else {
        error make { msg: $ret.stderr }
      }
    }
  13. added
    A:external-commands
    A:signal-cancel
    and removed on Aug 9, 2023
  14. added a commit that references this issue on Sep 7, 2024
    3d008e2
  15. added this to the v0.98.0 milestone on Sep 7, 2024
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

    A:external-commandsA:signal-cancelcategory:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions