mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-24 13:45:57 +00:00
Stop interrupted runs before removing their profile copy
This commit is contained in:
1 parent
767f2f6663
commit
d7c408c4d7
2 files changed
+65
-3
No files matched your search
@@ -2,6 +2,7 @@
|
||||
import contextlib
|
||||
import io
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -66,6 +67,7 @@ class ProfileTest(unittest.TestCase):
|
||||
build = self.folder / 'build'
|
||||
proc = unittest.mock.Mock()
|
||||
proc.wait.return_value = 0
|
||||
proc.poll.return_value = 0
|
||||
with patch.object(sys, 'argv', ['run_game.py', str(build), '--seconds', '1', '--tag', 'bench-1', '--show']), \
|
||||
patch.object(run, 'take_slot', return_value=None), patch.object(run, 'count', return_value=0), \
|
||||
patch.object(run.subprocess, 'Popen', return_value=proc) as launch, \
|
||||
@@ -77,6 +79,51 @@ class ProfileTest(unittest.TestCase):
|
||||
self.assertFalse((build / 'prof-bench-1').exists())
|
||||
self.assertIn('rc=0', (build / 'logs' / 'bench-1.log').read_text())
|
||||
launch.assert_called_once()
|
||||
proc.terminate.assert_not_called()
|
||||
|
||||
def test_interrupted_wait_stops_the_owned_child_before_cleanup(self):
|
||||
child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(60)'],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
proxy = unittest.mock.Mock(wraps=child)
|
||||
interrupted = False
|
||||
|
||||
def wait(*args, **kwargs):
|
||||
nonlocal interrupted
|
||||
if not interrupted:
|
||||
interrupted = True
|
||||
raise KeyboardInterrupt
|
||||
return child.wait(*args, **kwargs)
|
||||
proxy.wait.side_effect = wait
|
||||
build = self.folder / 'build'
|
||||
remove = run.shutil.rmtree
|
||||
|
||||
def remove_after_exit(path, *args, **kwargs):
|
||||
if Path(path) == build / 'prof-run':
|
||||
self.assertIsNotNone(child.poll(), 'profile cleanup ran while the child was alive')
|
||||
return remove(path, *args, **kwargs)
|
||||
try:
|
||||
with patch.object(sys, 'argv', ['run_game.py', str(build), '--seconds', '1', '--show']), \
|
||||
patch.object(run, 'take_slot', return_value=None), patch.object(run, 'count', return_value=0), \
|
||||
patch.object(run.shutil, 'rmtree', side_effect=remove_after_exit), \
|
||||
patch.object(run.subprocess, 'Popen', return_value=proxy):
|
||||
with self.assertRaises(KeyboardInterrupt):
|
||||
run.main()
|
||||
self.assertIsNotNone(child.poll(), 'interrupted run left its child alive')
|
||||
self.assertFalse((build / 'prof-run').exists())
|
||||
self.assertEqual((self.profile / 'marker').read_text(), 'original profile')
|
||||
finally:
|
||||
if child.poll() is None:
|
||||
child.kill()
|
||||
child.wait(timeout=5)
|
||||
|
||||
def test_unresponsive_child_is_killed_after_termination_timeout(self):
|
||||
proc = unittest.mock.Mock()
|
||||
proc.poll.return_value = None
|
||||
proc.wait.side_effect = [subprocess.TimeoutExpired('owned child', 5), 0]
|
||||
run.stop_process(proc)
|
||||
proc.terminate.assert_called_once()
|
||||
proc.kill.assert_called_once()
|
||||
self.assertEqual(proc.wait.call_args_list, [unittest.mock.call(timeout=5), unittest.mock.call()])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
+18
-3
@@ -162,6 +162,18 @@ def profile_copy(dest, save):
|
||||
shutil.copytree(SAVES / save, users[0])
|
||||
|
||||
|
||||
def stop_process(proc):
|
||||
"""Stop this run's child before its disposable profile is removed, including an interrupted wait."""
|
||||
if proc.poll() is not None:
|
||||
return
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
proc.wait()
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument('exe_dir', type=Path, help='build directory holding totk.exe')
|
||||
@@ -209,9 +221,12 @@ def main():
|
||||
f.flush()
|
||||
start = time.time()
|
||||
proc = subprocess.Popen(args, cwd=exe_dir, env=env, stdout=f, stderr=subprocess.STDOUT)
|
||||
if not a.show:
|
||||
threading.Thread(target=keep_in_background, args=(proc,), daemon=True).start()
|
||||
rc = proc.wait()
|
||||
try:
|
||||
if not a.show:
|
||||
threading.Thread(target=keep_in_background, args=(proc,), daemon=True).start()
|
||||
rc = proc.wait()
|
||||
finally:
|
||||
stop_process(proc)
|
||||
f.write(f'run_game: rc={rc} wall={time.time() - start:.0f}s; others running after: '
|
||||
f'totk.exe {count("totk.exe")}, ninja.exe {count("ninja.exe")}\n')
|
||||
finally:
|
||||
|
||||
Reference in new issue
Block a user