xonsh/news
Andy Kipp 0f25a5a348
Read stop signals from the process and update the process state. (#5361)
Reading stop signals from the process and update the process state.

### The issue

Technically. In a couple of places that critical for processing signals
we have `os.waitpid()`. The function behavior is pretty unobvious and
one of things is processing return code after catching the signal. We
had no good signal processing around this and this PR fixes this. See
also `proc_untraced_waitpid` function description.

From user perspective. For example we have process that is waiting for
user input from terminal e.g. `python -c "input()"` or `fzf`. If this
process will be in captured pipeline e.g. `!(echo 1 | fzf | head)` it
will be suspended by OS and the pipeline will be in the endless loop
with future crashing and corrupting std at the end. This PR fixes this.

### The solution

Technically. The key function is `proc_untraced_waitpid` - it catches
the stop signals and updates the process state.

From user perspective. First of all we expect that users will use
captured object `!()` only for capturable processes. Because of it our
goal here is to just make the behavior in this case stable.
In this PR we detect that process in the pipeline is suspended and we
need to finish the command pipeline carefully:
* Show the message about suspended process.
* Keep suspended process in `jobs`. The same behavior we can see in
bash. This is good because we don't know what process suspended and why.
May be experienced user will want to continue it manually.
* Finish the CommandPipeline with returncode=None and suspended=True.

### Before

```xsh
!(fzf) # or !(python -c "input()")
# Hanging / Exceptions / OSError / No way to end the command.
# After exception:
$(echo 1)
# OSError / IO error
```

### After

```xsh
!(fzf) # or `!(ls | fzf | head)` or `!(python -c "input()")`
# Process ['fzf'] with pid 60000 suspended with signal 22 SIGTTOU and stay in `jobs`.
# This happends when process start waiting for input but there is no terminal attached in captured mode.
# CommandPipeline(returncode=None, suspended=True, ...)

$(echo 1)
# Success.
```
Closes #4752 #4577

### Notes

* There is pretty edge case situation when the process was terminated so
fast that we can't catch pid alive and check signal
([src](67d672783d/xonsh/jobs.py (L71-L80))).
I leave it as is for now.

### Mentions

#2159

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Gil Forsyth <gforsyth@users.noreply.github.com>
2024-05-22 11:45:39 -04:00
..
appimage_py311.rst Downgrade AppImage to 3.11 (#5364) 2024-04-25 11:46:36 +02:00
downgrade_ptk.rst Downgrade prompt-toolkit to >=3.0.29,<3.0.40 (#5403) 2024-05-09 09:13:03 -04:00
env_completion.rst Use substring for env completion and better way to sort list (#5388) 2024-05-03 10:30:14 +02:00
env_detype_all.rst Added `env.detype_all()` to get all available variables that is possible to detype. (#5431) 2024-05-22 07:53:11 +05:30
fix_interactive_suspended_subproc.rst Read stop signals from the process and update the process state. (#5361) 2024-05-22 11:45:39 -04:00
fix_noaccess.rst Make xonsh tolerant to inaccessible paths: history backend, script cache (#5430) 2024-05-20 21:14:14 +02:00
fix_print_exception.rst Fixed showing exception message (#5394) 2024-05-06 19:48:43 +05:30
fix_process_traceback.rst Fixed empty stacktrace for CalledProcessError (#5391) 2024-05-04 11:50:32 +02:00
funcalias.rst feat: add superhelp and additional context via new FuncAlias (#5366) 2024-05-13 09:11:58 -04:00
history_save_unload.rst History: Saving history in case of any type of exiting the shell. (#5418) 2024-05-16 11:01:38 +05:30
jobs_catch_no_process.rst jobs: catching ChildProcessError (#5365) 2024-04-26 10:34:31 +02:00
jobs_repr.rst jobs: default representation changed to dict (#5363) 2024-04-26 10:26:24 +02:00
jupytext.rst Add Jupytext to news (#5402) 2024-05-10 11:03:44 +02:00
last_cp.rst Now last executed CommandPipeline is available in `__xonsh__.last` (#5422) 2024-05-22 09:25:35 -04:00
main_d.rst Fixed `xonsh -DVAR=VAL` behavior: initiate env variables before shell initialization. (#5396) 2024-05-06 17:39:10 +05:30
mini_refactor.rst CommandPipeline: clean repr + a bit of code cleaning (#5369) 2024-05-03 00:09:31 +05:30
no_env.rst feat: add --no-env flag to avoid inheriting parent-shell env (#5370) 2024-04-29 12:29:22 -04:00
popen_name.rst Add thread details to the exception (#5360) 2024-04-24 11:15:40 +02:00
prompt_superuser.rst Show `root and @#` in prompt if user is superuser. (#5409) 2024-05-22 09:27:03 -04:00
shell_type.rst Shortcut for shell-type (#5367) 2024-04-25 20:10:18 +02:00
sig_exit_fix.rst fix(signals): fix processing exit signals and exit exception (#5399) 2024-05-13 09:31:55 -04:00
sigint_save_hist.rst Saving history on SIGINT (#5425) 2024-05-22 10:20:08 -04:00
subproc_output_format.rst feat: add subproc output format, autostrip singleline output (#5377) 2024-05-02 11:48:25 -04:00
TEMPLATE.rst single instance of string.Formatter 2018-10-20 12:22:59 -06:00
trace_subproc.rst wip 2024-04-26 13:37:19 +05:30
typerr.rst Fix 5379, 5429 (#5432) 2024-05-22 08:27:44 +05:30
waitpid_returncode.rst Fixed populating the return code for interrupted process. (#5380) 2024-05-02 22:10:53 +02:00
xonfig_env.rst Xonfig: show sensitive env variables that could affect the shell behavior. (#5374) 2024-05-02 22:46:22 +05:30
zulip.rst Xonsh Zulip Community started (#5372) 2024-04-29 10:01:02 -04:00