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.
This commit is contained in:
Marco Allegretti 2026-10-09 17:19:51 +00:00
parent d509513e0a
commit d8e9b5a87a
No known key found for this signature in database

View file

@ -125,13 +125,14 @@ impl SessionRegistry {
Some(AppStateKind::Stopping) Some(AppStateKind::Stopping)
} }
Some(sender) => { Some(sender) => {
// A full queue already holds a request; a forced one that // A close is queued only into an empty queue, so a forced
// does not fit is preceded by one the supervisor acts on. // request always finds room; one already queued says the
let _ = sender.try_send(if force { // same as a repeat.
runtime::Stop::Force if force {
} else { let _ = sender.try_send(runtime::Stop::Force);
runtime::Stop::Close } else if sender.capacity() == runtime::STOP_QUEUE {
}); let _ = sender.try_send(runtime::Stop::Close);
}
if !force && matches!(state, AppStateKind::Running) { if !force && matches!(state, AppStateKind::Running) {
return Some(AppStateKind::Running); return Some(AppStateKind::Running);
} }
@ -1814,7 +1815,8 @@ mod tests {
while !matches!(rx.recv().await, Ok(Response::AppReady { .. })) {} while !matches!(rx.recv().await, Ok(Response::AppReady { .. })) {}
}) })
.await; .await;
dispatch( // The close is under way: the session is running while it is asked.
let asked = dispatch(
Request::TerminateApp { Request::TerminateApp {
session_id, session_id,
force: false, force: false,
@ -1822,6 +1824,17 @@ mod tests {
&registry, &registry,
) )
.await; .await;
// Repeats fill no queue a forced stop then cannot enter.
for _ in 0..3 {
dispatch(
Request::TerminateApp {
session_id,
force: false,
},
&registry,
)
.await;
}
tokio::time::sleep(std::time::Duration::from_millis(300)).await; tokio::time::sleep(std::time::Duration::from_millis(300)).await;
let forced_at = std::time::Instant::now(); let forced_at = std::time::Instant::now();
let reply = dispatch( let reply = dispatch(
@ -1866,6 +1879,16 @@ mod tests {
), ),
"{reply:?}" "{reply:?}"
); );
assert!(
matches!(
asked,
Response::AppState {
state: AppStateKind::Running,
..
}
),
"{asked:?}"
);
assert!(stopped.is_ok(), "the forced stop did not stop the session"); assert!(stopped.is_ok(), "the forced stop did not stop the session");
// The forced stop does not wait out the close timeout. // The forced stop does not wait out the close timeout.
assert!(elapsed < std::time::Duration::from_secs(3), "{elapsed:?}"); assert!(elapsed < std::time::Duration::from_secs(3), "{elapsed:?}");