Quick Fix: don't call a job interrupted while a rebuild reloads systemd

A rebuild restarts the shell mid-job, and the restarted shell asked
systemd whether the job's unit was alive while the user manager was
reloading. The failed query read as "stopped", so the chat showed
"interrupted" and stopped polling, and the real result never appeared.
A job now counts as dead only when systemd says its unit stopped and its
worker process is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mxandClaude Opus 5.5 committed 2026-11-26 00:19:37 +01:00
1 parent 3d976b5961
commit f35dbee430
2 files changed
+40 -4

No files matched your search

+26 -4
View File
@@ -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'):