Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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](https://github.com/xonsh/xonsh/blob/67d672783db6397bdec7ae44a9d9727b1e20a772/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 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Gil Forsyth <[email protected]>
- Loading branch information