Skip to content

Commit 570a56f

Browse files
authored
PYTHON-5594 Make process shutdown more robust (#322)
1 parent f674bb4 commit 570a56f

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

mongo_orchestration/daemon.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import sys
2323
import time
2424

25-
from signal import SIGTERM
25+
from signal import SIGTERM, SIGKILL
2626

2727
DEVNULL = open(os.devnull, 'r+b')
2828

@@ -174,8 +174,15 @@ def stop(self):
174174
# Try killing the daemon process
175175
try:
176176
os.kill(pid, SIGTERM)
177+
t0 = time.time()
177178
while is_unix_process_running(pid):
178179
time.sleep(0.25)
180+
# After 5 seconds resend a SIGTERM to the process.
181+
if time.time() - t0 > 5:
182+
os.kill(pid, SIGTERM)
183+
# After 10 seconds send a SIGKILL to the process.
184+
if time.time() - t0 > 10:
185+
os.kill(pid, SIGKILL)
179186
except OSError as err:
180187
if err.errno == errno.ESRCH:
181188
if os.path.exists(self.pidfile):
@@ -209,4 +216,3 @@ def is_unix_process_running(pid):
209216
else:
210217
raise err
211218
return True
212-

tests/test_launch.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ def run(cmd, **kwargs):
2626
if proc.returncode != 0:
2727
raise RuntimeError('Process failed!')
2828

29-
30-
def stop():
31-
pid = Path("server.pid").read_text().strip()
32-
os.kill(int(pid), signal.SIGTERM)
33-
Path("server.pid").unlink()
34-
3529
class TestLaunch(unittest.TestCase):
3630

3731
def test_launch_single(self):
@@ -43,9 +37,7 @@ def test_launch_single(self):
4337
proc.send('q\n')
4438
proc.wait()
4539
self.assertEqual(proc.exitstatus, 0)
46-
# TODO: https://jira.mongodb.org/browse/PYTHON-5594
47-
# run('mongo-orchestration stop')
48-
stop()
40+
run('mongo-orchestration stop')
4941

5042
def test_launch_replica_set(self):
5143
if os.name != 'posix':
@@ -56,9 +48,7 @@ def test_launch_replica_set(self):
5648
proc.send('q\n')
5749
proc.wait()
5850
self.assertEqual(proc.exitstatus, 0)
59-
# TODO: https://jira.mongodb.org/browse/PYTHON-5594
60-
# run('mongo-orchestration stop')
61-
stop()
51+
run('mongo-orchestration stop')
6252

6353

6454
def test_launch_sharded(self):
@@ -70,6 +60,4 @@ def test_launch_sharded(self):
7060
proc.send('q\n')
7161
proc.wait()
7262
self.assertEqual(proc.exitstatus, 0)
73-
# TODO: https://jira.mongodb.org/browse/PYTHON-5594
74-
# run('mongo-orchestration stop')
75-
stop()
63+
run('mongo-orchestration stop')

0 commit comments

Comments
 (0)