fix: restore terminal foreground control to support interactive commands#1329
Open
mkaaad wants to merge 3 commits into
Open
fix: restore terminal foreground control to support interactive commands#1329mkaaad wants to merge 3 commits into
mkaaad wants to merge 3 commits into
Conversation
…up" (mvdan#1244) This reverts commit e74afc1.
Add setProcessForeground and restoreForeground functions to manage terminal foreground process group. This ensures signals like SIGINT are delivered to the command's process group when executing with timeout. Also add comments explaining the purpose of these calls.
Owner
|
Could you clarify if this is indeed what shells like Bash do already? |
Owner
Remove trailing whitespace from prepareCommand stub.
Author
|
Based on the Bash source code ( When spawning a child process group, Bash transfers terminal ownership to the foreground process group: (jobs.c:3905-3918) /* Save the tty settings before we start the job in the foreground. */
if (foreground)
{
#if defined (READLINE)
/* Don't fetch the terminal attributes if we're doing this from a key
binding or programmable completion. */
if (RL_ISSTATE(RL_STATE_COMPLETING|RL_STATE_DISPATCHING|RL_STATE_TERMPREPPED) == 0)
#endif
get_tty_state ();
save_stty = shell_tty_info;
jobs[job]->flags &= ~J_ASYNC; /* no longer async */
/* Give the terminal to this job. */
if (IS_JOBCONTROL (job))
give_terminal_to (jobs[job]->pgrp, 0);When the foreground child process group exits, Bash restores terminal control back to the shell's own process group. (jobs.c:3250-3253) if ((flags & JWAIT_NOTERM) == 0 && running_in_background == 0 &&
(job == NO_JOB || IS_ASYNC (job) == 0 || IS_FOREGROUND (job)) &&
(subshell_environment & (SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0)
give_terminal_to (shell_pgrp, 0);Reference source: https://cgit.git.savannah.gnu.org/cgit/bash.git/tree/jobs.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Previously, only the process group was modified, but terminal foreground ownership was not transferred to child processes.
This broke interactive programs such as
bash,vim, and REPL environments:Solution
Add explicit foreground process group control via
TIOCSPGRPioctl:Key Changes
setProcessForeground/restoreForegroundhelper functionsdeferAbout ignore
SIGTTOUWhen a background process calls the
TIOCSPGRPioctl to modify the terminal foreground group:SIGTTOUto the calling process by defaultSIGTTOUis stop the process, which will freeze the programSIGTTOUavoids suspension during foreground switching