Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(browser): open a session in a fresh profile while another Chrome holds it

Shanu222317ccfa54
diff --git a/crates/openhuman-core/src/modules/browser.rs b/crates/openhuman-core/src/modules/browser.rs
index 0e8170ced4..53e89e0076 100644
--- a/crates/openhuman-core/src/modules/browser.rs
+++ b/crates/openhuman-core/src/modules/browser.rs
@@ -204,6 +204,7 @@ impl BrowserClient {
} else {
None
};
+ fresh_if_held(&mut options, super::browser_profile::SESSION_PROFILE_WAIT).await;
options.download_dir = browser.download_dir.clone();
options.default_timeout_ms = browser.task_timeout_secs.saturating_mul(1000);
options.allowed_origins =
@@ -480,6 +481,23 @@ fn parsed_browser_url(raw: &str) -> Result<url::Url, BrowserCallError> {
.map_err(|_| BrowserCallError::Policy("invalid URL".into()))
}
+/// Leaves `options` its persistent profile only when no other Chrome holds
+/// it within `wait`. Chrome lets one browser at a time use a profile folder:
+/// a second one hands its window to the first and exits, and a session in
+/// one conversation found the profile held by another conversation's
+/// browser, or a task's. Such a session uses a fresh profile instead.
+async fn fresh_if_held(options: &mut SessionOptions, wait: std::time::Duration) {
+ let Some(profile) = options.user_data_dir.clone() else {
+ return;
+ };
+ if !super::browser_profile::free_within(std::path::Path::new(&profile), wait).await {
+ tracing::warn!(
+ "[browser] another Chrome holds the persistent profile; this session uses a fresh profile"
+ );
+ options.user_data_dir = None;
+ }
+}
+
fn browser_allow_all() -> bool {
matches!(
std::env::var("OPENHUMAN_BROWSER_ALLOW_ALL").ok().as_deref(),
diff --git a/crates/openhuman-core/src/modules/browser_profile.rs b/crates/openhuman-core/src/modules/browser_profile.rs
index f1b76602f5..2f0544c007 100644
--- a/crates/openhuman-core/src/modules/browser_profile.rs
+++ b/crates/openhuman-core/src/modules/browser_profile.rs
@@ -3,10 +3,11 @@
//!
//! Chrome lets one browser at a time use a profile folder: a second one
//! started on it hands its window to the first and exits, so a browser task
-//! given the persistent profile while another Chrome holds it could not
-//! start its browser. A task waits up to [`PROFILE_WAIT`] for a Chrome that
-//! is closing (the one the task before it closes as it ends), and otherwise
-//! runs in a fresh profile.
+//! or session given the persistent profile while another Chrome holds it
+//! could not start its browser. A task waits up to [`PROFILE_WAIT`] for a
+//! Chrome that is closing (the one the task before it closes as it ends), a
+//! session up to [`SESSION_PROFILE_WAIT`], and otherwise each runs in a fresh
+//! profile.
use std::path::Path;
use std::time::Duration;
@@ -15,6 +16,12 @@ use std::time::Duration;
/// let it go before it runs in a fresh profile instead.
pub(crate) const PROFILE_WAIT: Duration = Duration::from_secs(5);
+/// How long a browser session waits for the same, before it opens in a
+/// fresh profile: shorter than a task's, as a person waits on it, and the
+/// Chrome holding the profile is most often another conversation's, which
+/// does not close.
+pub(crate) const SESSION_PROFILE_WAIT: Duration = Duration::from_secs(1);
+
/// How often the profile is looked at while waiting.
const PROFILE_POLL: Duration = Duration::from_millis(250);
diff --git a/crates/openhuman-core/src/modules/browser_tests.rs b/crates/openhuman-core/src/modules/browser_tests.rs
index d152ceca2b..19541b9314 100644
--- a/crates/openhuman-core/src/modules/browser_tests.rs
+++ b/crates/openhuman-core/src/modules/browser_tests.rs
@@ -188,6 +188,35 @@ fn tinycomputers_own_words_for_a_missing_or_unusable_chrome_get_the_users_fix()
assert_eq!(chrome_hint("Chrome launch timed out"), None);
}
+#[tokio::test]
+async fn a_session_opens_in_a_fresh_profile_while_another_chrome_holds_it() {
+ // Live, a second conversation's browser found the profile held by the
+ // first's, and Chrome exited at once.
+ let profile =
+ std::env::temp_dir().join(format!("openhuman-session-profile-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&profile);
+ std::fs::create_dir_all(&profile).unwrap();
+ let held = profile.to_string_lossy().into_owned();
+ let mut options = SessionOptions {
+ user_data_dir: Some(held.clone()),
+ ..SessionOptions::default()
+ };
+ fresh_if_held(&mut options, std::time::Duration::from_millis(50)).await;
+ assert_eq!(options.user_data_dir.as_deref(), Some(held.as_str()));
+ #[cfg(not(windows))]
+ {
+ let here = sysinfo::System::host_name().unwrap_or_default();
+ std::os::unix::fs::symlink(
+ format!("{here}-{}", std::process::id()),
+ profile.join("SingletonLock"),
+ )
+ .unwrap();
+ fresh_if_held(&mut options, std::time::Duration::from_millis(50)).await;
+ assert!(options.user_data_dir.is_none());
+ }
+ let _ = std::fs::remove_dir_all(&profile);
+}
+
#[test]
fn fresh_profile_is_the_default() {
let config = Config::default();