stagehand-rust icon indicating copy to clipboard operation
stagehand-rust copied to clipboard

`chromiumoxide_page_example` panics on `browser.new_page()`

Open danieldaquino opened this issue 5 months ago • 1 comments

Preconditions

stagehand-rust: b8ca9dff483a112382585c98a80c9d74452bb78f

Setup

  1. Setup the Browserbase environment in .env with valid credentials.

Steps

  1. Run RUST_BACKTRACE=1 cargo run --example chromiumoxide_page_example

Expected results

Example should run successfully

Actual results

% RUST_BACKTRACE=1 cargo run --example chromiumoxide_page_example
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.37s
     Running `target/debug/examples/chromiumoxide_page_example`
=== Stagehand Rust SDK + chromiumoxide Page Example ===

1. Connecting to Stagehand...
   Connected!

2. Starting browser session...
   Session ID: Some("c52789b2-d716-4c15-bc16-319cc9d0f609")

3. Fetching Browserbase CDP URL...
   CDP URL: wss://connect.usw2.browserbase.com/?signingKey=<REDACTED>

4. Connecting chromiumoxide over CDP...
   Connected!

5. Getting a page and navigating with chromiumoxide...

thread 'tokio-runtime-worker' (39103388) panicked at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/chromiumoxide-0.8.0/src/handler/mod.rs:208:33:
Created target not present
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/4a4ef493e3a1488c6e321570238084b38948f6db/library/std/src/panicking.rs:689:5
   1: core::panicking::panic_fmt
             at /rustc/4a4ef493e3a1488c6e321570238084b38948f6db/library/core/src/panicking.rs:80:14
   2: chromiumoxide::handler::Handler::on_response
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/chromiumoxide-0.8.0/src/handler/mod.rs:208:33
   3: <chromiumoxide::handler::Handler as futures_core::stream::Stream>::poll_next
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/chromiumoxide-0.8.0/src/handler/mod.rs:618:29
   4: futures_util::stream::stream::StreamExt::poll_next_unpin
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/stream/stream/mod.rs:1638:24
   5: <futures_util::stream::stream::next::Next<St> as core::future::future::Future>::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/stream/stream/next.rs:32:21
   6: chromiumoxide_page_example::main::{{closure}}::{{closure}}
             at ./examples/chromiumoxide_page_example.rs:53:48
   7: tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/core.rs:365:24
   8: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/loom/std/unsafe_cell.rs:16:9
   9: tokio::runtime::task::core::Core<T,S>::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/core.rs:354:30
  10: tokio::runtime::task::harness::poll_future::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:535:30
  11: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:274:9
  12: std::panicking::catch_unwind::do_call
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:581:40
  13: ___rust_try
  14: std::panicking::catch_unwind
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:544:19
  15: std::panic::catch_unwind
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panic.rs:359:14
  16: tokio::runtime::task::harness::poll_future
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:523:18
  17: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:210:27
  18: tokio::runtime::task::harness::Harness<T,S>::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:155:20
  19: tokio::runtime::task::raw::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/raw.rs:325:13
  20: tokio::runtime::task::raw::RawTask::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/raw.rs:255:18
  21: tokio::runtime::task::LocalNotified<S>::run
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/mod.rs:509:13
  22: tokio::runtime::scheduler::multi_thread::worker::Context::run_task::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:600:18
  23: tokio::task::coop::with_budget
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/task/coop/mod.rs:167:5
  24: tokio::task::coop::budget
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/task/coop/mod.rs:133:5
  25: tokio::runtime::scheduler::multi_thread::worker::Context::run_task
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:591:9
  26: tokio::runtime::scheduler::multi_thread::worker::Context::run
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:539:29
  27: tokio::runtime::scheduler::multi_thread::worker::run::{{closure}}::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:504:24
  28: tokio::runtime::context::scoped::Scoped<T>::set
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/context/scoped.rs:40:9
  29: tokio::runtime::context::set_scheduler::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/context.rs:176:38
  30: std::thread::local::LocalKey<T>::try_with
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/thread/local.rs:513:12
  31: std::thread::local::LocalKey<T>::with
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/thread/local.rs:477:20
  32: tokio::runtime::context::set_scheduler
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/context.rs:176:17
  33: tokio::runtime::scheduler::multi_thread::worker::run::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:499:9
  34: tokio::runtime::context::runtime::enter_runtime
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/context/runtime.rs:65:16
  35: tokio::runtime::scheduler::multi_thread::worker::run
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:491:5
  36: tokio::runtime::scheduler::multi_thread::worker::Launch::launch::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/scheduler/multi_thread/worker.rs:457:45
  37: <tokio::runtime::blocking::task::BlockingTask<T> as core::future::future::Future>::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/blocking/task.rs:42:21
  38: tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/core.rs:365:24
  39: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/loom/std/unsafe_cell.rs:16:9
  40: tokio::runtime::task::core::Core<T,S>::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/core.rs:354:30
  41: tokio::runtime::task::harness::poll_future::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:535:30
  42: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:274:9
  43: std::panicking::catch_unwind::do_call
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:581:40
  44: ___rust_try
  45: std::panicking::catch_unwind
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panicking.rs:544:19
  46: std::panic::catch_unwind
             at /Users/user/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/panic.rs:359:14
  47: tokio::runtime::task::harness::poll_future
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:523:18
  48: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:210:27
  49: tokio::runtime::task::harness::Harness<T,S>::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/harness.rs:155:20
  50: tokio::runtime::task::raw::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/raw.rs:325:13
  51: tokio::runtime::task::raw::RawTask::poll
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/raw.rs:255:18
  52: tokio::runtime::task::UnownedTask<S>::run
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/task/mod.rs:546:13
  53: tokio::runtime::blocking::pool::Task::run
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/blocking/pool.rs:161:19
  54: tokio::runtime::blocking::pool::Inner::run
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/blocking/pool.rs:516:22
  55: tokio::runtime::blocking::pool::Spawner::spawn_thread::{{closure}}
             at /Users/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tokio-1.48.0/src/runtime/blocking/pool.rs:474:47
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Error: ChannelSendError(Canceled(Canceled))

When the issue first appeared

It is unclear exactly when the issue first appeared, but this seems to have been working back in February 2026 (through a similar symptom on an app I am working on). I believe I have run this example before successfully on the same version of this library.

This indicates the possibility that a regression occurred in the Browserbase service itself.

danieldaquino avatar Jun 17 '26 00:06 danieldaquino

Debugging

Doing some debugging revealed that the panic and "cancel" errors come around the same time from these places (See annotated comments):

chromium_page_example.rs

    (...)
    println!("4. Connecting chromiumoxide over CDP...");
    let (browser, mut handler) = Browser::connect(&cdp_url).await?;
    let handler_task = tokio::spawn(async move {
        while let Some(event) = handler.next().await {     // <--- PANIC COMES FROM HERE
            if event.is_err() {
                break;
            }
        }
    });
    println!("   Connected!\n");

    println!("5. Getting a page and navigating with chromiumoxide...");
    let pages = browser.pages().await?;
    let page = if pages.is_empty() {
        browser.new_page("about:blank").await?   // <--- This returns `ChannelSendError(Canceled(Canceled))`
    } else {
        pages.into_iter().next().unwrap()
    };
    (...)

The ChannelSendError(Canceled(Canceled)) seems to be a result of the panic on the handler itself.

Digging deeper, the panic comes from the chromiumoxide library (See annotation):

chromiumoxide-0.8.0/src/handler/mod.rs

    /// Received a response to a request.
    fn on_response(&mut self, resp: Response) {
        if let Some((req, method, _)) = self.pending_commands.remove(&resp.id) {
            match req {
                PendingRequest::CreateTarget(tx) => {
                    match to_command_response::<CreateTargetParams>(resp, method) {
                        Ok(resp) => {
                            if let Some(target) = self.targets.get_mut(&resp.target_id) {
                                // move the sender to the target that sends its page once
                                // initialized
                                target.set_initiator(tx);
                            } else {
                                // TODO can this even happen?
                                panic!("Created target not present")     // <---- Panic happens here
                            }
                        }
                        Err(err) => {
                            let _ = tx.send(Err(err)).ok();
                        }
                    }
                }

With the debugger I found that self.targets (a hashmap) was empty, leading to the panic.

self.targets only gets new items on the method on_target_created, which is called here and here. The latter seems more relevant:

    /// Process an incoming event read from the websocket
    fn on_event(&mut self, event: CdpEventMessage) {
        if let Some(ref session_id) = event.session_id {
            if let Some(session) = self.sessions.get(session_id.as_str()) {
                if let Some(target) = self.targets.get_mut(session.target_id()) {
                    return target.on_event(event);
                }
            }
        }
        let CdpEventMessage { params, method, .. } = event;
        match params.clone() {
            CdpEvent::TargetTargetCreated(ev) => self.on_target_created(*ev), // <---- Here
            CdpEvent::TargetAttachedToTarget(ev) => self.on_attached_to_target(ev),
            CdpEvent::TargetTargetDestroyed(ev) => self.on_target_destroyed(ev),
            CdpEvent::TargetDetachedFromTarget(ev) => self.on_detached_from_target(ev),
            _ => {}
        }

Analysis

At the CDP protocol level, it seems that chromiumoxide is expecting the events to happen in the following sequence:

  1. Client sends a CreatePage request.
  2. Browser sends back an TargetCreated event with target info including the ID.
  3. Browser sends back the response for the CreatePage request, with the target ID.

However, Browserbase seems to be sending back the CreatePage response before the TargetCreated event.

I could not find any specific mentions about the expected order of things in the CDP protocol, but I locally tested swapping out BrowserBase for a local Chrome instance, and it did not have this issue.

Furthermore, given that I only noticed this issue recently, maybe it is possible that this Browserbase behaviour changed in a recent Browserbase release?

@pirate, @shrey150, can you please take a look at whether this is an issue on the Browserbase side?

danieldaquino avatar Jun 17 '26 01:06 danieldaquino