From ddd6a6e57e5226db1bab3d7e0059bc115766843c Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Wed, 21 Oct 2026 05:17:56 +0530 Subject: [PATCH] fix(browser): refuse operations after failed close Co-authored-by: Medulla --- .../tinycomputer-browser/src/sessions/mod.rs | 15 +++++++-- .../sessions_tests/lifecycle_tests.rs | 31 ++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/tinycomputer-browser/src/sessions/mod.rs b/crates/tinycomputer-browser/src/sessions/mod.rs index 35ed28458..a9aec2962 100644 --- a/crates/tinycomputer-browser/src/sessions/mod.rs +++ b/crates/tinycomputer-browser/src/sessions/mod.rs @@ -99,6 +99,7 @@ struct Session { dir: PathBuf, action_context: ActionContext, refs: Vec, + closing: bool, closed: bool, } @@ -114,7 +115,7 @@ impl std::fmt::Debug for Session { impl Session { async fn run(&mut self, command: Value) -> Result { - if self.closed { + if self.closing || self.closed { return Err(Error::NoSuchSession { id: self.info.id.to_string(), }); @@ -122,6 +123,11 @@ impl Session { reply::data(&self.engine.execute(command).await) } + async fn close(&mut self) -> Result<()> { + self.closing = true; + reply::data(&self.engine.execute(json!({"action": "close"})).await).map(|_| ()) + } + /// The current address for gate context, or `None` while the page is /// briefly between documents. Never substitute a cached, potentially /// stale address for the location of the page about to be acted on. @@ -340,6 +346,7 @@ impl Browser { dir: self.scratch.join(id.as_str()), action_context, refs: Vec::new(), + closing: false, closed: false, }; // A browser this module launches says what to set when it cannot @@ -406,8 +413,10 @@ impl Browser { if session.closed { return Ok(()); } - // A browser that is already gone has nothing left to close. - session.run(json!({"action": "close"})).await?; + // A browser that is already gone has nothing left to close. A failure + // leaves `closing` set so normal operations stay refused while a later + // explicit close may retry. + session.close().await?; let _removed = std::fs::remove_dir_all(&session.dir); session.closed = true; drop(session); diff --git a/crates/tinycomputer-browser/src/sessions/sessions_tests/lifecycle_tests.rs b/crates/tinycomputer-browser/src/sessions/sessions_tests/lifecycle_tests.rs index 81a0e4825..259c3b559 100644 --- a/crates/tinycomputer-browser/src/sessions/sessions_tests/lifecycle_tests.rs +++ b/crates/tinycomputer-browser/src/sessions/sessions_tests/lifecycle_tests.rs @@ -2,7 +2,7 @@ use std::{sync::Arc, time::Duration}; -use tinycomputer_bus::browser::{NavigateRequest, SessionId, SessionOptions}; +use tinycomputer_bus::browser::{NavigateRequest, SessionId, SessionOptions, SnapshotRequest}; use super::{open, scratch}; use crate::error::Error; @@ -82,6 +82,35 @@ async fn a_failed_close_keeps_the_session_unconfirmed() { assert!(error.to_string().contains("browser close failed")); assert_eq!(browser.list_sessions().await.unwrap().len(), 1); + let close_count = fake + .actions() + .iter() + .filter(|action| *action == "close") + .count(); + assert!( + matches!( + browser.snapshot(&id, SnapshotRequest::default()).await, + Err(crate::error::Error::NoSuchSession { .. }) + ), + "normal page operations stay refused after an uncertain close" + ); + assert_eq!( + fake.actions() + .iter() + .filter(|action| *action == "close") + .count(), + close_count, + "a snapshot after failure must not reach the browser" + ); + assert!(browser.close_session(&id).await.is_err()); + assert_eq!( + fake.actions() + .iter() + .filter(|action| *action == "close") + .count(), + close_count + 1, + "an explicit close call may retry the failed close" + ); } #[tokio::test]