Fix ChromeDriver session hanging when Chrome is already running

- Add unique user-data-dir per process to avoid profile conflicts
- Add 30-second timeout to connection attempts to prevent indefinite hangs
- Fix borrow checker issue with ClientBuilder

The session creation was hanging because ChromeDriver was trying to
use the same profile as the running Chrome browser. Using a unique
temp directory (/tmp/g3-chrome-{pid}) isolates the headless session.
This commit is contained in:
Dhanji R. Prasanna
2025-12-15 17:36:34 +11:00
parent 81cba42c8d
commit bbe57b4764

View File

@@ -44,6 +44,8 @@ impl ChromeDriver {
chrome_options.insert( chrome_options.insert(
"args".to_string(), "args".to_string(),
Value::Array(vec![ Value::Array(vec![
// Use a unique temp directory to avoid conflicts with running Chrome instances
Value::String(format!("--user-data-dir=/tmp/g3-chrome-{}", std::process::id())),
Value::String("--headless=new".to_string()), Value::String("--headless=new".to_string()),
Value::String("--disable-gpu".to_string()), Value::String("--disable-gpu".to_string()),
Value::String("--no-sandbox".to_string()), Value::String("--no-sandbox".to_string()),
@@ -62,11 +64,16 @@ impl ChromeDriver {
Value::Object(chrome_options), Value::Object(chrome_options),
); );
let client = ClientBuilder::native() // Use a timeout for the connection attempt to avoid hanging indefinitely
let mut builder = ClientBuilder::native();
let connect_future = builder
.capabilities(caps) .capabilities(caps)
.connect(&url) .connect(&url);
let client = tokio::time::timeout(Duration::from_secs(30), connect_future)
.await .await
.context("Failed to connect to ChromeDriver. Make sure ChromeDriver is running and Chrome is installed.")?; .context("Connection to ChromeDriver timed out after 30 seconds")?
.context("Failed to connect to ChromeDriver")?;
Ok(Self { client }) Ok(Self { client })
} }