This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Complement image: propagate SIGTERM to all workers #13914
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Complement image: propagate SIGTERM to all workers. |
This file contains 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 |
---|---|---|
|
@@ -51,11 +51,18 @@ | |
import importlib | ||
import itertools | ||
import multiprocessing | ||
import os | ||
import signal | ||
import sys | ||
from typing import Any, Callable, List | ||
from types import FrameType | ||
from typing import Any, Callable, List, Optional | ||
|
||
from twisted.internet.main import installReactor | ||
|
||
# a list of the original signal handlers, before we installed our custom ones. | ||
# We restore these in our child processes. | ||
_original_signal_handlers: dict[int, Any] = {} | ||
|
||
|
||
class ProxiedReactor: | ||
""" | ||
|
@@ -105,6 +112,11 @@ def _worker_entrypoint( | |
|
||
sys.argv = args | ||
|
||
# reset the custom signal handlers that we installed, so that the children start | ||
# from a clean slate. | ||
for sig, handler in _original_signal_handlers.items(): | ||
signal.signal(sig, handler) | ||
|
||
from twisted.internet.epollreactor import EPollReactor | ||
|
||
proxy_reactor._install_real_reactor(EPollReactor()) | ||
|
@@ -167,13 +179,29 @@ def main() -> None: | |
update_proc.join() | ||
print("===== PREPARED DATABASE =====", file=sys.stderr) | ||
|
||
processes: List[multiprocessing.Process] = [] | ||
|
||
# Install signal handlers to propagate signals to all our children, so that they | ||
# shut down cleanly. This also inhibits our own exit, but that's good: we want to | ||
# wait until the children have exited. | ||
def handle_signal(signum: int, frame: Optional[FrameType]) -> None: | ||
print( | ||
f"complement_fork_starter: Caught signal {signum}. Stopping children.", | ||
file=sys.stderr, | ||
) | ||
for p in processes: | ||
if p.pid: | ||
os.kill(p.pid, signum) | ||
|
||
for sig in (signal.SIGINT, signal.SIGTERM): | ||
_original_signal_handlers[sig] = signal.signal(sig, handle_signal) | ||
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. Crucially, this works because: (I wish it was called 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.
... which has the same somewhat unintuitive semantics, yes. I guess it made sense 45 years ago. |
||
|
||
# At this point, we've imported all the main entrypoints for all the workers. | ||
# Now we basically just fork() out to create the workers we need. | ||
# Because we're using fork(), all the workers get a clone of this launcher's | ||
# memory space and don't need to repeat the work of loading the code! | ||
# Instead of using fork() directly, we use the multiprocessing library, | ||
# which uses fork() on Unix platforms. | ||
processes = [] | ||
for (func, worker_args) in zip(worker_functions, args_by_worker): | ||
process = multiprocessing.Process( | ||
target=_worker_entrypoint, args=(func, proxy_reactor, worker_args) | ||
|
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.
I don't see why our own exit is blocked waiting for our children to exit. Does
os.kill()
block until the child handles the signal? Or does we already have this for free thanks toprocess.join()
further below?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.
two halves to this:
process.join()
stuff means we will exit once the children have exited.os.kill
doesn't block.