Skip to content

Commit 6247341

Browse files
Yalhashclaude
andcommitted
Keep a fatal error on screen after the automatic stop
When the speech model failed to load, the pipeline stopped capture in the background, and that stop published a plain STOPPED status, which replaced the error a moment after it appeared. A fatal stop now leaves the status at ERROR with its message. ERROR is not a listening state, so the window still offers Start; the next Start (or a manual Stop) clears it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0178pC5pfGXepbgU7JEpqVUh
1 parent 48d522d commit 6247341

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

‎src/polyglot/core/pipeline.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,10 @@ def __init__(
947947
def _stop_in_background(self) -> None:
948948
# Called on the coordinator thread; stop() drains the coordinator, so it must
949949
# not run there.
950-
threading.Thread(target=self.stop, name="polyglot-fatal-stop", daemon=True).start()
950+
# The error stays on screen: the stop must not replace it with "Stopped".
951+
threading.Thread(
952+
target=self.stop, kwargs={"keep_error": True}, name="polyglot-fatal-stop", daemon=True
953+
).start()
951954

952955
def _asr_depth(self) -> int:
953956
q = self._asr_queue
@@ -1061,8 +1064,12 @@ def start(self) -> None:
10611064
self._stop_user_frontend(flush=False)
10621065
coord.post(_SourceError(exc, Speaker.USER))
10631066

1064-
def stop(self, timeout: float = 10.0) -> None:
1065-
"""Stop listening. The utterance in progress is finished and transcribed."""
1067+
def stop(self, timeout: float = 10.0, *, keep_error: bool = False) -> None:
1068+
"""Stop listening. The utterance in progress is finished and transcribed.
1069+
1070+
``keep_error`` is for stopping because of a fatal error (the model cannot load):
1071+
the status stays ERROR, with its message, instead of becoming STOPPED. ERROR is
1072+
not a listening state, so the UI still offers Start; the next start clears it."""
10661073
with self._lock:
10671074
if not self._running:
10681075
return
@@ -1078,7 +1085,8 @@ def stop(self, timeout: float = 10.0) -> None:
10781085
log.exception("microphone failed to stop cleanly")
10791086
self._teardown(flush=True, timeout=timeout)
10801087
self._coord.post(_Command("abandon_open"))
1081-
self._coord.post(_Command("status", (PipelineState.STOPPED, "")))
1088+
if not keep_error:
1089+
self._coord.post(_Command("status", (PipelineState.STOPPED, "")))
10821090
# When stop() returns, the last utterance's transcript has been applied.
10831091
self._coord.drain(timeout=2.0)
10841092

‎tests/pipeline/test_pipeline.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,31 @@ def test_model_load_failure_stops_capture_and_keeps_error(
503503
)
504504
p.start()
505505
assert wait_for(lambda: not p.running, 5), "capture should stop when the model cannot load"
506-
assert wait_for(lambda: recorder.of(PipelineStatus)[-1].state is PipelineState.STOPPED, 5)
506+
time.sleep(0.3) # let the background stop finish posting
507507
states = [s.state for s in recorder.of(PipelineStatus)]
508508
assert PipelineState.ERROR in states
509509
assert PipelineState.FALLING_BEHIND not in states[states.index(PipelineState.ERROR) :]
510+
# The error is the last word: stopping must not replace it with a bare "Stopped",
511+
# or it only flashes on screen.
512+
last = recorder.of(PipelineStatus)[-1]
513+
assert last.state is PipelineState.ERROR and "no model" in last.detail
514+
assert p.state is PipelineState.ERROR
510515
# nothing left "listening" forever
511516
assert all(u.status is not UtteranceStatus.LISTENING for u in recorder.utterances().values())
512517

513518

519+
def test_the_next_start_clears_a_kept_error(make_pipeline: Any, recorder: Recorder) -> None:
520+
asr = ScriptedTranscriber(load_error=TranscriptionFailed("no model"))
521+
p = make_pipeline(utterances_audio(1), transcriber=asr)
522+
p.start()
523+
assert wait_for(lambda: not p.running and p.state is PipelineState.ERROR, 5)
524+
asr._load_error = None # e.g. the missing package was installed
525+
p.start()
526+
assert wait_for(lambda: p.state is PipelineState.LISTENING, 5)
527+
p.stop()
528+
assert wait_for(lambda: recorder.of(PipelineStatus)[-1].state is PipelineState.STOPPED)
529+
530+
514531
def test_model_loaded_after_stop_does_not_claim_listening(
515532
make_pipeline: Any, recorder: Recorder
516533
) -> None:

0 commit comments

Comments
 (0)