From d8e9b5a87ac255662a35c58d7ae860ea3155b4c7 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:19:51 +0000 Subject: [PATCH] fix(appd): keep room for a forced stop behind repeated close requests Repeated close requests could fill the stop queue, so a forced stop sent after them was dropped while the session was reported as stopping. A close is now queued only into an empty queue, which leaves room for a forced stop. --- crates/weft-appd/src/main.rs | 39 ++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 644a6fe..d3e716f 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -125,13 +125,14 @@ impl SessionRegistry { Some(AppStateKind::Stopping) } Some(sender) => { - // A full queue already holds a request; a forced one that - // does not fit is preceded by one the supervisor acts on. - let _ = sender.try_send(if force { - runtime::Stop::Force - } else { - runtime::Stop::Close - }); + // A close is queued only into an empty queue, so a forced + // request always finds room; one already queued says the + // same as a repeat. + if force { + let _ = sender.try_send(runtime::Stop::Force); + } else if sender.capacity() == runtime::STOP_QUEUE { + let _ = sender.try_send(runtime::Stop::Close); + } if !force && matches!(state, AppStateKind::Running) { return Some(AppStateKind::Running); } @@ -1814,7 +1815,8 @@ mod tests { while !matches!(rx.recv().await, Ok(Response::AppReady { .. })) {} }) .await; - dispatch( + // The close is under way: the session is running while it is asked. + let asked = dispatch( Request::TerminateApp { session_id, force: false, @@ -1822,6 +1824,17 @@ mod tests { ®istry, ) .await; + // Repeats fill no queue a forced stop then cannot enter. + for _ in 0..3 { + dispatch( + Request::TerminateApp { + session_id, + force: false, + }, + ®istry, + ) + .await; + } tokio::time::sleep(std::time::Duration::from_millis(300)).await; let forced_at = std::time::Instant::now(); let reply = dispatch( @@ -1866,6 +1879,16 @@ mod tests { ), "{reply:?}" ); + assert!( + matches!( + asked, + Response::AppState { + state: AppStateKind::Running, + .. + } + ), + "{asked:?}" + ); assert!(stopped.is_ok(), "the forced stop did not stop the session"); // The forced stop does not wait out the close timeout. assert!(elapsed < std::time::Duration::from_secs(3), "{elapsed:?}");