-
Notifications
You must be signed in to change notification settings - Fork 788
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
Async runner improvements #2056
Changes from 14 commits
9f3b802
1dbfdbf
fdc6fc2
52ede8a
5d93035
da7c650
db848af
9ec693c
82caf1d
f9f49c1
ec34a71
73dbde5
c623fd9
e305c68
fc9353a
b3ed0f4
b06af09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,26 +9,49 @@ | |
import threading | ||
from typing import Callable, Dict, Iterator, List, Optional, Tuple | ||
|
||
from .utils import check_process_exited | ||
|
||
def kill_process_and_descendants(pid, termination_timeout): | ||
|
||
def kill_processes_and_descendants(pids: List[str], termination_timeout: float): | ||
# TODO: there's a race condition that new descendants might | ||
# spawn b/w the invocations of 'pkill' and 'kill'. | ||
# Needs to be fixed in future. | ||
try: | ||
subprocess.check_call(["pkill", "-TERM", "-P", str(pid)]) | ||
subprocess.check_call(["kill", "-TERM", str(pid)]) | ||
subprocess.check_call(["pkill", "-TERM", "-P", *pids]) | ||
subprocess.check_call(["kill", "-TERM", *pids]) | ||
except subprocess.CalledProcessError: | ||
pass | ||
|
||
time.sleep(termination_timeout) | ||
|
||
try: | ||
subprocess.check_call(["pkill", "-KILL", "-P", str(pid)]) | ||
subprocess.check_call(["kill", "-KILL", str(pid)]) | ||
subprocess.check_call(["pkill", "-KILL", "-P", *pids]) | ||
subprocess.check_call(["kill", "-KILL", *pids]) | ||
except subprocess.CalledProcessError: | ||
pass | ||
|
||
|
||
async def async_kill_processes_and_descendants( | ||
pids: List[str], termination_timeout: float | ||
): | ||
# TODO: there's a race condition that new descendants might | ||
# spawn b/w the invocations of 'pkill' and 'kill'. | ||
# Needs to be fixed in future. | ||
sub_term = await asyncio.create_subprocess_exec("pkill", "-TERM", "-P", *pids) | ||
await sub_term.wait() | ||
|
||
main_term = await asyncio.create_subprocess_exec("kill", "-TERM", *pids) | ||
await main_term.wait() | ||
|
||
await asyncio.sleep(termination_timeout) | ||
|
||
sub_kill = await asyncio.create_subprocess_exec("pkill", "-KILL", "-P", *pids) | ||
await sub_kill.wait() | ||
|
||
main_kill = await asyncio.create_subprocess_exec("kill", "-KILL", *pids) | ||
await main_kill.wait() | ||
|
||
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. I think we can encapsulate the SIGTERM part and the SIGKILL part inside try/except each? 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. I was checking |
||
|
||
class LogReadTimeoutError(Exception): | ||
"""Exception raised when reading logs times out.""" | ||
|
||
|
@@ -46,14 +69,28 @@ def __init__(self): | |
loop = asyncio.get_running_loop() | ||
loop.add_signal_handler( | ||
signal.SIGINT, | ||
lambda: self._handle_sigint(signum=signal.SIGINT, frame=None), | ||
lambda: asyncio.create_task(self._async_handle_sigint()), | ||
) | ||
except RuntimeError: | ||
signal.signal(signal.SIGINT, self._handle_sigint) | ||
|
||
async def _async_handle_sigint(self): | ||
pids = [ | ||
str(command.process.pid) | ||
for command in self.commands.values() | ||
if command.process and not check_process_exited(command) | ||
] | ||
if pids: | ||
await async_kill_processes_and_descendants(pids, termination_timeout=2) | ||
|
||
def _handle_sigint(self, signum, frame): | ||
for each_command in self.commands.values(): | ||
each_command.kill(termination_timeout=2) | ||
pids = [ | ||
str(command.process.pid) | ||
for command in self.commands.values() | ||
if command.process and not check_process_exited(command) | ||
] | ||
if pids: | ||
kill_processes_and_descendants(pids, termination_timeout=2) | ||
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. small nitpick: earlier, 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. similarly for the async version, if we choose to do this.. 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. Shouldn't be necessary anymore. All the pids are enumerated before the call and are sent out simultaneously by kill/pkill, so they all get the same amount of time as before. |
||
|
||
async def __aenter__(self) -> "SubprocessManager": | ||
return self | ||
|
@@ -472,7 +509,7 @@ def kill(self, termination_timeout: float = 2): | |
""" | ||
|
||
if self.process is not None: | ||
kill_process_and_descendants(self.process.pid, termination_timeout) | ||
kill_processes_and_descendants([str(self.process.pid)], termination_timeout) | ||
else: | ||
print("No process to kill.") | ||
|
||
|
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.
we are no longer setting the
metadata
explicitly.. i.emetadata(metadata_for_flow)
needs to be removed..refer to the implementation of
__get_executing_run
and you will see the following..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.
Thanks, fixed! Must've missed it while rebasing.