Send SIGHUP instead of SIGKILL to clean up jobs (#4811)

Allows `nohup` command to work properly.
This commit is contained in:
Peter Ye 2022-05-20 13:39:24 -04:00 committed by GitHub
parent 400d2d6c56
commit 11b2d6d086
Failed to generate hash of commit
2 changed files with 34 additions and 6 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* SIGHUP (instead of SIGKILL) is sent to unfinished jobs when exiting the shell. This allows the `nohup` command to work properly.
**Security:**
* <news item>

View file

@ -76,6 +76,8 @@ if ON_WINDOWS:
stderr=subprocess.STDOUT,
)
_hup = _kill # there is no equivalent of SIGHUP on Windows
def ignore_sigtstp():
pass
@ -114,6 +116,9 @@ else:
def _kill(job):
_send_signal(job, signal.SIGKILL)
def _hup(job):
_send_signal(job, signal.SIGHUP)
def ignore_sigtstp():
signal.signal(signal.SIGTSTP, signal.SIG_IGN)
@ -313,7 +318,7 @@ def add_job(info):
def clean_jobs():
"""Clean up jobs for exiting shell
In non-interactive mode, kill all jobs.
In non-interactive mode, send SIGHUP to all jobs.
In interactive mode, check for suspended or background jobs, print a
warning if any exist, and return False. Otherwise, return True.
@ -335,7 +340,7 @@ def clean_jobs():
# part of the last command and is now being called again
# immediately. Kill jobs and exit without reminder about
# unfinished jobs in this case.
kill_all_jobs()
hup_all_jobs()
else:
if len(XSH.all_jobs) > 1:
msg = "there are unfinished jobs"
@ -357,18 +362,18 @@ def clean_jobs():
jobs_clean = False
_last_exit_time = time.time()
else:
kill_all_jobs()
hup_all_jobs()
return jobs_clean
def kill_all_jobs():
def hup_all_jobs():
"""
Send SIGKILL to all child processes (called when exiting xonsh).
Send SIGHUP to all child processes (called when exiting xonsh).
"""
_clear_dead_jobs()
for job in XSH.all_jobs.values():
_kill(job)
_hup(job)
def jobs(args, stdin=None, stdout=sys.stdout, stderr=None):