xonsh/tests
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
..
aliases refactor(aliases): source foreign shell funcs without interactive mode (#5344) 2024-04-18 19:51:39 +05:30
bin style: isort imports 2022-01-31 11:16:51 -05:00
completers chore: use stable 3.12 for CI jobs (#5307) 2024-05-16 15:43:57 +05:30
histories Fix tests 2020-09-23 21:15:37 +02:00
man1 improve completers (#4648) 2022-01-27 21:22:36 +05:30
procs Read stop signals from the process and update the process state. (#5361) 2024-05-22 11:45:39 -04:00
prompt Py 312 pre-liminary support (#5156) 2023-07-04 22:18:37 +05:30
scripts xonsh script.xsh should not fail over 2017-01-11 21:59:42 +08:00
test_lib chore: update tests 2022-03-24 19:49:30 +05:30
xoreutils Py 312 pre-liminary support (#5156) 2023-07-04 22:18:37 +05:30
xpack fix: do not append empty/comment-only input to history (#4822) 2022-05-26 17:50:08 +05:30
.somedotfile dotfile matching 2018-09-12 18:12:02 -04:00
bashrc.sh added tests for foreign shells 2015-10-10 19:52:53 -04:00
batch.bat Add test for sourcing bat file on windows 2016-04-18 00:41:11 +02:00
conftest.py Fix vox env dir (#4736) 2022-03-27 17:12:52 +05:30
run_pwd.xsh more fixes 2016-09-09 00:58:32 -04:00
sample.xsh fixed up dashing 2018-11-08 13:16:44 -05:00
test_aliases.py feat: add superhelp and additional context via new FuncAlias (#5366) 2024-05-13 09:11:58 -04:00
test_ansi_colors.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_ast.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_base_shell.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_builtins.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_cli_utils.py Fix argparser (#5421) 2024-05-16 16:31:05 +05:30
test_color_tools.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_commands_cache.py commands_cache: ignore mtime check on non-darwin os (#4988) 2022-12-05 21:04:20 +05:30
test_completer.py Use substring for env completion and better way to sort list (#5388) 2024-05-03 10:30:14 +02:00
test_completion_context.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_contexts.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_dirstack.py fix(dirstack): use XSH.env for $HOME check, not os.path (#5211) 2023-09-25 14:53:15 +06:00
test_dirstack_unc.py refactor: update line length to default setting 2024-04-16 11:23:35 -04:00
test_environ.py Added `env.detype_all()` to get all available variables that is possible to detype. (#5431) 2024-05-22 07:53:11 +05:30
test_events.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_execer.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_foreign_shells.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_history_dummy.py Add XONSH_HISTORY_IGNORE_REGEX support (#4953) 2022-10-05 00:16:42 +05:30
test_history_json.py History: Saving history in case of any type of exiting the shell. (#5418) 2024-05-16 11:01:38 +05:30
test_history_sqlite.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_imphooks.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_integrations.py Read stop signals from the process and update the process state. (#5361) 2024-05-22 11:45:39 -04:00
test_jobs.py Fix job control for callable aliases (#4901) 2022-08-03 11:37:26 +05:30
test_jsonutils.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_lazyasd.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_lazyjson.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_lexer.py Fix parsing of redirect tokens (#5322) 2024-04-05 13:21:54 +05:30
test_main.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_man.py style: sort imports 2022-03-24 19:49:30 +05:30
test_news.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_parser.py feat: allow square brackets in command arguments (#5326) 2024-04-09 22:33:43 +05:30
test_pipelines.py feat: add subproc output format, autostrip singleline output (#5377) 2024-05-02 11:48:25 -04:00
test_platform.py style: isort imports 2022-01-31 11:16:51 -05:00
test_pretty.py fix: item order of pretty-printed dictionaries (#4786) 2022-04-30 08:25:52 +05:30
test_ptk_completer.py cmd cache refactor - optimize cache usage (#4954) 2022-11-14 23:52:10 +05:30
test_ptk_highlight.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_ptk_history.py refactor: remove xonsh.ptk2 module (#4570) 2021-11-30 08:22:31 -05:00
test_ptk_multiline.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_ptk_shell.py [pre-commit.ci] pre-commit autoupdate (#5148) 2023-05-30 17:10:53 +06:00
test_pyghooks.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_readline_shell.py Add test for readline shell (#4447) 2021-09-02 22:15:20 +03:00
test_shell.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_tools.py feat: add superhelp and additional context via new FuncAlias (#5366) 2024-05-13 09:11:58 -04:00
test_tracer.py pre-commit hook for pyupgrade (#4583) 2021-12-06 14:42:26 -05:00
test_virtualenv_activator.py fix: update virtualenv activator (#5419) 2024-05-16 14:29:48 +05:30
test_wizard.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00
test_xonfig.py refactor: update line length to default setting 2024-04-16 11:23:35 -04:00
test_xonsh.xsh Remove globals (#4280) 2021-05-20 13:14:26 +03:00
test_xontribs.py [pre-commit.ci] pre-commit autoupdate (#5271) 2024-01-30 12:23:50 +01:00