forked from It4innovations/hyperqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_task_cleanup.py
More file actions
197 lines (158 loc) · 5.1 KB
/
Copy pathtest_task_cleanup.py
File metadata and controls
197 lines (158 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
This file contains tests related to automatic kill of tasks when a worker terminates, is stopped
or a task is cancelled.
"""
import subprocess
from typing import Callable
import pytest
import signal
from .utils.cmd import python
from .utils.io import read_file
from .utils.job import default_task_output
from .utils.wait import wait_until, wait_for_pid_exit, wait_for_worker_state
from .utils import wait_for_job_state
from .conftest import HqEnv
def test_kill_task_child_when_worker_is_cancelled(hq_env: HqEnv):
def cancel(_worker_process):
hq_env.command(["job", "cancel", "1"])
wait_for_job_state(hq_env, 1, "CANCELED")
check_task_processes_exited(hq_env, cancel, terminates_worker=False)
def test_cancel_sigint_then_sigkill(hq_env: HqEnv):
"""
Test that the worker sends SIGINT when a task is cancelled,
and then continues with sending SIGKILL when the task refuses to exit.
"""
hq_env.start_server()
hq_env.start_worker()
hq_env.command(
[
"submit",
"--",
*python(
"""
import os
import sys
import time
import signal
def signal_handler(sig, frame):
print(os.getpid(), flush=True)
time.sleep(3600)
signal.signal(signal.SIGINT, signal_handler)
print("ready", flush=True)
time.sleep(3600)
"""
),
]
)
wait_for_job_state(hq_env, 1, "RUNNING")
wait_until(lambda: read_file(default_task_output()).strip() == "ready")
hq_env.command(["job", "cancel", "1"])
wait_for_job_state(hq_env, 1, "CANCELED")
wait_until(lambda: len(read_file(default_task_output()).splitlines()) == 2)
pid = int(read_file(default_task_output()).splitlines()[1])
wait_for_pid_exit(pid)
@pytest.mark.parametrize(
"signal",
[
signal.SIGINT,
signal.SIGTERM,
signal.SIGKILL,
],
)
def test_kill_task_when_worker_receives_signal(hq_env: HqEnv, signal: int):
"""
Make sure that a single child task is killed when the worker receives a signal.
Note that the whole group is not killed, only the worker, which simulates a more realistic
situation.
Usage of PR_SET_PDEATHSIG is what makes this work with SIGKILL.
"""
hq_env.start_server()
worker_process = hq_env.start_worker()
hq_env.command(
[
"submit",
"--",
*python(
"""
import os
import time
print(os.getpid(), flush=True)
time.sleep(3600)
"""
),
]
)
wait_for_job_state(hq_env, 1, "RUNNING")
def get_pid():
pid = read_file(default_task_output()).strip()
if not pid:
return None
return int(pid)
pid = wait_until(get_pid)
worker_process.send_signal(signal)
hq_env.check_process_exited(worker_process, None)
wait_for_pid_exit(pid)
@pytest.mark.parametrize(
"signal",
[
signal.SIGINT,
signal.SIGTERM,
],
)
def test_kill_task_child_when_worker_receives_signal(hq_env: HqEnv, signal: int):
"""
Test that nested child subprocesses are killed when the worker is signalled.
This does not work with SIGKILL, as the worker doesn't get a chance to react to it,
and PR_SET_PDEATHSIG doesn't apply to nested processes.
"""
def interrupt_worker(worker_process):
worker_process.send_signal(signal)
check_task_processes_exited(hq_env, interrupt_worker)
@pytest.mark.xfail
def test_kill_task_child_when_worker_receives_sigkill(hq_env: HqEnv):
"""
HyperQueue currently cannot handle this situation gracefully due to it running in user space
and not having control over (grand)child processes.
"""
def terminate_worker(worker_process):
hq_env.kill_worker(1, signal=signal.SIGKILL)
check_task_processes_exited(hq_env, terminate_worker)
def test_kill_task_child_when_worker_is_stopped(hq_env: HqEnv):
def stop_worker(worker_process):
hq_env.command(["worker", "stop", "1"])
wait_for_worker_state(hq_env, 1, "STOPPED", check_running_processes=False)
check_task_processes_exited(hq_env, stop_worker)
def check_task_processes_exited(hq_env: HqEnv, stop_fn: Callable[[subprocess.Popen], None], terminates_worker=True):
"""
Creates a task that spawns a child, and then calls `stop_fn`, which should kill either the task
or the worker. The function then checks that both the task process and its child have been killed.
"""
hq_env.start_server()
worker_process = hq_env.start_worker()
hq_env.command(
[
"submit",
"--",
*python(
"""
import os
import sys
import time
print(os.getpid(), flush=True)
pid = os.fork()
if pid > 0:
print(pid, flush=True)
time.sleep(3600)
"""
),
]
)
wait_for_job_state(hq_env, 1, "RUNNING")
wait_until(lambda: len(read_file(default_task_output()).splitlines()) == 2)
pids = [int(pid) for pid in read_file(default_task_output()).splitlines()]
stop_fn(worker_process)
parent, child = pids
wait_for_pid_exit(parent)
wait_for_pid_exit(child)
if terminates_worker:
hq_env.check_process_exited(worker_process, None)