diff --git a/mx-quickfix/helpers/quickfix.py b/mx-quickfix/helpers/quickfix.py index f0603c9..38b8cac 100644 --- a/mx-quickfix/helpers/quickfix.py +++ b/mx-quickfix/helpers/quickfix.py @@ -123,8 +123,26 @@ def new_id(): # Units ----------------------------------------------------------------------- def unit_active(id_): - r = subprocess.run(['systemctl', '--user', 'is-active', f'{UNIT}-{id_}.service'], capture_output=True, text=True) - return r.stdout.strip() in ('active', 'activating', 'deactivating') + """False only when systemd says the unit has stopped. While a rebuild + reloads the user manager the query can fail; that counts as running.""" + try: + r = subprocess.run(['systemctl', '--user', 'is-active', f'{UNIT}-{id_}.service'], + capture_output=True, text=True, timeout=10) + except (OSError, subprocess.TimeoutExpired): + return True + return r.stdout.strip() not in ('inactive', 'failed') + + +def alive(pid): + if not isinstance(pid, int) or pid <= 0: + return False + try: + os.kill(pid, 0) + except ProcessLookupError: + return False + except PermissionError: + return True + return True def start_unit(id_, action): @@ -471,6 +489,8 @@ def system_generation(): def worker(id_): + with locked(): + save_state(pid=os.getpid()) session = load_session(id_) options = session['options'] run = session['messages'][-1]['run'] @@ -494,7 +514,7 @@ def worker(id_): session = load_session(id_) session['messages'][-1]['provider'] = provider save_session(session) - save_state(current=id_, running=True, seen=False) + save_state(current=id_, running=True, seen=False, pid=os.getpid()) record = snapshot(run, options) prompt = transcript(session, provider) + prompt command, env, text = build_command(provider, session, prompt, options, last_file) @@ -536,6 +556,8 @@ def worker(id_): def undo_worker(id_): + with locked(): + save_state(pid=os.getpid()) session = load_session(id_) run = session['messages'][-1]['undoes'] generation = system_generation() @@ -581,7 +603,7 @@ def recover(): if not state.get('running') or not state.get('current'): return id_ = state['current'] - if time.time() - state.get('started', 0) < 5 or unit_active(id_): + if time.time() - state.get('started', 0) < 5 or alive(state.get('pid')) or unit_active(id_): return with locked(): if not read_state().get('running'): diff --git a/mx-quickfix/tests/quickfix_test.py b/mx-quickfix/tests/quickfix_test.py index 71f4c3f..3fca745 100644 --- a/mx-quickfix/tests/quickfix_test.py +++ b/mx-quickfix/tests/quickfix_test.py @@ -129,5 +129,19 @@ class QuickFix(unittest.TestCase): self.assertFalse(state['seen']) self.assertEqual(state['session']['messages'][-1]['status'], 'failed') + def test_a_failed_unit_query_is_not_a_dead_job(self): + # During a rebuild systemctl may not answer; the job is still running. + request = {'op': 'send', 'text': 'x', 'provider': 'claude', 'options': {}} + with patch.object(quickfix, 'start_unit'): + quickfix.handle(request) + quickfix.save_state(started=0) + failing = subprocess.CompletedProcess([], 1, '', 'Failed to connect to bus') + with patch.object(quickfix.subprocess, 'run', return_value=failing): + self.assertTrue(quickfix.handle({'op': 'state'})['running']) + quickfix.save_state(pid=os.getpid()) + stopped = subprocess.CompletedProcess([], 3, 'inactive\n', '') + with patch.object(quickfix.subprocess, 'run', return_value=stopped): + self.assertTrue(quickfix.handle({'op': 'state'})['running']) # worker process alive + if __name__ == '__main__': unittest.main()