Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions crates/tinycomputer-browser/src/sessions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct Session {
dir: PathBuf,
action_context: ActionContext,
refs: Vec<ElementRef>,
closing: bool,
closed: bool,
}

Expand All @@ -114,14 +115,19 @@ impl std::fmt::Debug for Session {

impl Session {
async fn run(&mut self, command: Value) -> Result<Value> {
if self.closed {
if self.closing || self.closed {
return Err(Error::NoSuchSession {
id: self.info.id.to_string(),
});
}
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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down
Loading