fix(app): a worker stop returns only after its last output is logged

Stopping (and restarting) the voice worker now waits, at most 5 s, for its output readers to log
what the exited worker wrote, so its last lines are masked with the secrets that run was started
with before the next start replaces them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-13 10:15:20 +09:00
parent 648e8304b0
commit fba258821b
3 changed files with 77 additions and 10 deletions

View File

@@ -17,6 +17,10 @@ if os.environ.get("FAKE_CRASH_FIRST") == "1" and runs == 0:
sys.exit(3)
print("loading models...", flush=True)
print("READY 127.0.0.1:" + sys.argv[sys.argv.index("--port") + 1], flush=True)
if os.environ.get("FAKE_LAST_LINES") == "1":
for i in range(3):
print(f"line {i}", file=sys.stderr, flush=True)
print("LAST bad key old-api-key-1", file=sys.stderr, flush=True)
time.sleep(60)
"#;
@@ -118,3 +122,31 @@ async fn stop_ends_the_worker_for_good() {
assert!(rx.try_recv().is_err(), "no restart after stop");
assert_eq!(p.restarts(), 0);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn stop_returns_only_after_the_last_output_is_logged() {
// A slow log (each line takes 300 ms) and a worker whose last lines are still unread when it is
// stopped: they must be handled before stop returns, so the next start's secrets (logs.rs,
// set_secrets_in_use) never apply to them.
let fake = Fake::new("drain");
let mut o = fake.opts(false);
o.env.insert("FAKE_LAST_LINES".into(), "1".into());
let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
let s2 = seen.clone();
o.line_sink = Some(jarvis_app::worker_process::LineSink(std::sync::Arc::new(move |l: &str| {
std::thread::sleep(Duration::from_millis(300));
s2.lock().unwrap().push(l.to_owned());
})));
let p = WorkerProcess::new(o).unwrap();
let mut rx = p.subscribe();
p.start();
wait_for(&mut rx, |e| *e == ProcessEvent::Ready).await;
tokio::time::timeout(Duration::from_secs(10), async {
while seen.lock().unwrap().is_empty() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
}).await.expect("no output");
p.stop().await;
let lines = seen.lock().unwrap().clone();
assert!(lines.iter().any(|l| l.contains("LAST")), "{lines:?}");
}