Repository navigation
Reap active Git and GitHub subprocesses on SIGTERM #1290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,10 @@ | |
| import os | ||
| import signal | ||
| import subprocess | ||
| import threading | ||
| import time | ||
| from collections.abc import Mapping, Sequence | ||
| from contextlib import suppress | ||
| from contextlib import contextmanager, suppress | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
|
|
||
|
|
@@ -32,6 +33,55 @@ def __init__( | |
| self.duration_s = duration_s | ||
|
|
||
|
|
||
| class Terminated(BaseException): | ||
| """Unwind the active operation before exiting from SIGTERM.""" | ||
|
|
||
|
|
||
| def _request_unwind(_signum, _frame): | ||
| signal.signal(signal.SIGTERM, signal.SIG_IGN) | ||
| raise Terminated | ||
|
|
||
|
|
||
| @contextmanager | ||
| def unwind_on_sigterm(): | ||
| if (threading.current_thread() is not threading.main_thread() | ||
| or signal.getsignal(signal.SIGTERM) != signal.SIG_DFL): | ||
| yield | ||
| return | ||
| try: | ||
| try: | ||
| signal.signal(signal.SIGTERM, _request_unwind) | ||
| yield | ||
| finally: | ||
| signal.signal(signal.SIGTERM, signal.SIG_DFL) | ||
| except Terminated: | ||
| signal.signal(signal.SIGTERM, signal.SIG_DFL) | ||
| signal.raise_signal(signal.SIGTERM) | ||
| raise | ||
|
|
||
|
|
||
| @contextmanager | ||
| def defer_sigterm(): | ||
| """Finish a private directory operation before unwinding.""" | ||
| if (threading.current_thread() is not threading.main_thread() | ||
| or signal.getsignal(signal.SIGTERM) is not _request_unwind): | ||
| yield | ||
| return | ||
| pending = False | ||
|
|
||
| def remember_signal(_signum, _frame): | ||
| nonlocal pending | ||
| pending = True | ||
|
|
||
| signal.signal(signal.SIGTERM, remember_signal) | ||
| try: | ||
| yield | ||
| finally: | ||
| signal.signal(signal.SIGTERM, _request_unwind) | ||
| if pending: | ||
| _request_unwind(signal.SIGTERM, None) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Completed: | ||
| exit_code: int | ||
|
|
@@ -102,8 +152,7 @@ def run( | |
| raise FileNotFoundError(f"working directory {cwd} does not exist") from exc | ||
| raise ToolMissing(f"{argv[0]}: executable not found on PATH") from exc | ||
| except subprocess.TimeoutExpired as exc: | ||
| out = exc.stdout.decode(errors="replace") if isinstance(exc.stdout, bytes) else exc.stdout | ||
| out = out or "" | ||
| out = exc.stdout.decode(errors="replace") if isinstance(exc.stdout, bytes) else (exc.stdout or "") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Unrelated TimeoutExpired refactor This change folds |
||
| err = exc.stderr.decode(errors="replace") if isinstance(exc.stderr, bytes) else exc.stderr | ||
| elapsed = time.monotonic() - started | ||
| raise ProcTimeout( | ||
|
|
||
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Unwind-time children inherit SIGTERM=SIG_IGN, now across the whole CLI
_request_unwindsets SIGTERM to SIG_IGN untilunwind_on_sigtermexits.Popen(restore_signals=True)only resets SIGPIPE, SIGXFZ and SIGXFSZ. So any subprocess started from afinally/cleanup path while Terminated is propagating keeps ignoring SIGTERM after exec. This already happened inside sandbox scopes. Becausecli.mainnow wraps every command, it can affect any cleanup that runs Git or gh. Impact is small: those children are short-lived and_proc.runSIGKILLs on exceptions. Smallest fix: block SIGTERM withsignal.pthread_sigmaskinstead of using SIG_IGN, or pass apreexec_fn/process_groupreset in_proc.run. At minimum, add a comment documenting it.