You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
no indication when process exits with a signal #5960
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
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.
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.
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.
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.
(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)
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.)
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.
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”
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.
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:
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)
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:)
defcompare [
...options# too bad we can’t express this yetimage: stringreconstruct: stringdifference: string = 'null'
] {
letret= (/usr/bin/compare ([$options, $image, $reconstruct, $difference] |flatten) |complete)
if$ret.exit_codein [0, 1] {
$ret.exit_code==0
} else {
error make { msg:$ret.stderr }
}
}
Activity
rgwood commentedon Jul 6, 2022
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:
nushell/crates/nu-command/src/system/run_external.rs
Line 321 in 5d00ece
I personally don't have a good understanding of which fatal signals we should log (maybe all of them?).
toffaletti commentedon Jul 7, 2022
This might be helpful: fish-shell/fish-shell@f5be301
Here's the fish shell code that does the printing: https://github.com/fish-shell/fish-shell/blob/80fe0a7fcb42ab81903038c0d25c1f2e40aa1ed8/share/functions/fish_job_summary.fish
That fish code is executed from here: https://github.com/fish-shell/fish-shell/blob/b73091b27bff809d9a0c8e61081b8c814999b84c/src/proc.cpp#L565
https://github.com/fish-shell/fish-shell/blob/11cfa85a2a8b2f727fc97c96f7a20cc3a7204e3c/src/proc.h#L121
Summary from quick skim of the fish code:
The status returned from wait/waitpid is passed to the macro
WIFSIGNALEDto test if the process exited from a signal. The macroWEXITSTATUSis then used to retrieve the signal. Finally, this table is used to turn the signal int value into descriptive strings: https://github.com/fish-shell/fish-shell/blob/cf2ca56e3440e398e299dd9fbf8e2aa58823915b/src/signal.cpp#L36rgwood commentedon Jul 7, 2022
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.
toffaletti commentedon Jul 7, 2022
I took a shot at it as well and noticed the same thing and more:
ListStreamproperty toPipelineData::ExternalStreamsimilar to how exit_code is implemented so innushell/crates/nu-engine/src/eval.rs
Line 733 in 4e90b47
from_rawandinto_rawto be able to shove the raw exit status intonu::protocol::value::Valueand useListStream. However, std::os::windows::process::ExitStatusExt does not provideinto_rawonlyfrom_raw. Maybe callingcodeon Windows is good enough.report error when process immediately terminates with a signal
rgwood commentedon Jul 8, 2022
Nice, thanks. I've reverted #5908.
I think this is on the right track but I'm not 100% certain what the type of a new
exit_statusproperty should be. I guess we have 2 options:ListStream, coerce exit status to aValue::Intusinginto_rawon Unix andcode()on WindowsExitStatusstruct (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.
rgwood commentedon Jul 8, 2022
(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)
rgwood commentedon Jul 8, 2022
A suggestion from @kubouch in Discord:
toffaletti commentedon Jul 8, 2022
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 incrates/nu-engine/src/eval.rs(this seems to handle multi-command pipelines). There is also a case innu-cli/src/eval_file.rsthat uses exit_code inprint_table_or_errorthat 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.flying-sheep commentedon Jul 13, 2022
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_CODEis 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”
kubouch commentedon Jul 13, 2022
Non-zero exit code, unfortunately, is not a guarantee a program encountered an error. For example ImageMagick's
comparewill return1on success,2on 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.
flying-sheep commentedon Jul 13, 2022
Pretend I said EXIT_SUCCESS, not 0 (the two are equal on POSIXy systems though). ImageMagick’s
comparewill 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
diffso people can doif [diff/compare a b]; then echo 'identical/similar'; else echo 'different; fi'I think the ideal way to design interaction with potentially weird systems:
complete, users can wrap a badly designed external command like this:)Error on non-zero exit statuses (nushell#13515)