Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(websocket): close command admission before terminal delivery
ldm0a460cb3738e9
diff --git a/moli-websocket/src/commands.rs b/moli-websocket/src/commands.rsindex 9f4a2edad..f387f10a0 100644--- a/moli-websocket/src/commands.rs+++ b/moli-websocket/src/commands.rs@@ -101,6 +101,14 @@ impl CommandPort {self.tx.is_closed() || self.admission.failed.load(Ordering::Acquire)}+ #[cfg(test)]+ pub(crate) fn available_data_capacity(&self) -> (usize, usize) {+ (+ self.admission.bytes.available_permits(),+ self.admission.messages.available_permits(),+ )+ }+fn reserve(&self, command: &Command) -> Option<Reservation> {let bytes = match command {Command::SendText(text) | Command::ReceiveText(text) => Some(text.len()),diff --git a/moli-websocket/src/connection.rs b/moli-websocket/src/connection.rsindex 04cf286b1..96602dc6e 100644--- a/moli-websocket/src/connection.rs+++ b/moli-websocket/src/connection.rs@@ -19,6 +19,7 @@ pub(crate) async fn run_websocket_connection(decision: Option<tokio::sync::oneshot::Receiver<HandshakeDecision>>,) -> EventResult {let Some(_connection_slot) = acquire_websocket_connection_slot() else {+ drop(command_rx);send_error_and_close(&event_tx,socket_id,@@ -31,12 +32,14 @@ pub(crate) async fn run_websocket_connection(let request = match prepare_websocket_request(&url, &protocols, &context) {Ok(request) => request,Err(error) => {+ drop(command_rx);send_error_and_close(&event_tx, socket_id, error).await?;return Ok(());}};let Some(pending_handshake_slot) = acquire_pending_websocket_handshake_slot() else {+ drop(command_rx);send_error_and_close(&event_tx,socket_id,@@ -45,63 +48,44 @@ pub(crate) async fn run_websocket_connection(.await?;return Ok(());};- let handshake = open_websocket_connection(request, &context);- tokio::pin!(handshake);- let OpenedConnection {- connection,- handshake,- } = loop {- tokio::select! {- biased;- command = command_rx.recv() => {- match command.map(|queued| queued.command) {- Some(Command::Close { .. }) => {- drop(pending_handshake_slot);- send_error_and_close(- &event_tx,- socket_id,+ // The handshake future owns the pending transport. End its scope before+ // waiting for terminal delivery, including Close received while opening.+ let opened = {+ let handshake = open_websocket_connection(request, &context);+ tokio::pin!(handshake);+ loop {+ tokio::select! {+ biased;+ command = command_rx.recv() => {+ match command.map(|queued| queued.command) {+ Some(Command::Close { .. }) => break Err("WebSocket connection closed before opening".to_owned(),- )- .await?;- return Ok(());- }- Some(Command::SendText(_))- | Some(Command::SendBinary(_))- | Some(Command::ReceiveText(_))- | Some(Command::ReceiveBinary(_))- | Some(Command::ServerClose { .. }) => {- // Browser-visible `send()` throws while CONNECTING, so these commands- // should only appear from direct crate users. Ignore them rather than- // queueing frames before the opening handshake has succeeded.- }- Some(Command::Fail(message)) => {- drop(pending_handshake_slot);- send_error_and_close(&event_tx, socket_id, message).await?;- return Ok(());- }- None => return Ok(()),- }- }- connected = &mut handshake => {- match connected {- Ok(connected) => {- drop(pending_handshake_slot);- break connected;- }- Err(error) => {- drop(pending_handshake_slot);- send_error_and_close(- &event_tx,- socket_id,- format!("WebSocket connection failed: {error}"),- )- .await?;- return Ok(());+ ),+ Some(Command::Fail(message)) => break Err(message),+ None => return Ok(()),+ // The browser rejects sends while CONNECTING. Ignore+ // direct crate users' data until the handshake succeeds.+ _ => {}}}+ connected = &mut handshake => break connected.map_err(|error| {+ format!("WebSocket connection failed: {error}")+ }),}}};+ drop(pending_handshake_slot);+ let OpenedConnection {+ connection,+ handshake,+ } = match opened {+ Ok(opened) => opened,+ Err(error) => {+ drop(command_rx);+ send_error_and_close(&event_tx, socket_id, error).await?;+ return Ok(());+ }+ };let request_headers = header_map_entries(&handshake.request_headers);let mut response_status = handshake.response.status().as_u16();@@ -130,11 +114,13 @@ pub(crate) async fn run_websocket_connection(command = command_rx.recv() => {match command.map(|queued| queued.command) {Some(Command::Fail(message)) => {+ drop(command_rx);drop(connection);send_error_and_close(&event_tx, socket_id, message).await?;return Ok(());}Some(Command::Close { .. }) => {+ drop(command_rx);drop(connection);send_error_and_close(&event_tx, socket_id,@@ -157,6 +143,7 @@ pub(crate) async fn run_websocket_connection(break;}HandshakeDecision::Fail(message) => {+ drop(command_rx);drop(connection);send_error_and_close(&event_tx, socket_id, message).await?;return Ok(());diff --git a/moli-websocket/src/lib.rs b/moli-websocket/src/lib.rsindex f069a2686..2fa5362da 100644--- a/moli-websocket/src/lib.rs+++ b/moli-websocket/src/lib.rs@@ -40,3 +40,5 @@ mod tests;#[cfg(test)]mod native_tests;+#[cfg(test)]+mod terminal_tests;diff --git a/moli-websocket/src/native_tests.rs b/moli-websocket/src/native_tests.rsindex 06e145077..baaf231ec 100644--- a/moli-websocket/src/native_tests.rs+++ b/moli-websocket/src/native_tests.rs@@ -317,6 +317,8 @@ async fn native_close_handshake_finishes_while_message_sink_is_blocked() {.await.expect("physical close must not wait for the event sink or closing timeout").unwrap();+ assert!(handle.is_closed());+ assert_eq!(handle.send_binary(vec![1]), Err(crate::SendError::Closed));resume.add_permits(1);assert_text_message(&mut rx, 75, "blocked").await;assert_closing(&mut rx, 75).await;diff --git a/moli-websocket/src/session.rs b/moli-websocket/src/session.rsindex e0571c46e..5a8f3b1a7 100644--- a/moli-websocket/src/session.rs+++ b/moli-websocket/src/session.rs@@ -82,27 +82,14 @@ pub(super) async fn run_open_session(};let mut delivery: Option<Delivery> = None;let mut sending: Option<Sending> = None;- loop {- if session.terminal {- // Release physical transport and all reservations before awaiting a- // potentially blocked final event. Drop/cancel also aborts delivery.- sender.cancel();- sending = None;- session.outgoing.clear();- session.pongs.clear();- session.assembler = Assembler::default();- }+ while !session.terminal {if delivery.is_none()&& let Some(event) = session.outbox.pop_front(){let sink = event_tx.clone();delivery = Some(Box::pin(async move { send_event(&sink, event).await }));}- if session.terminal && delivery.is_none() {- return Ok(());- }- if !session.terminal- && sending.is_none()+ if sending.is_none()&& let Some((frame, next_flight)) = session.next_frame(){let sender = sender.clone();@@ -115,9 +102,8 @@ pub(super) async fn run_open_session(}));}// Pause reads at the source while application delivery is backpressured.- let should_read = !session.terminal- && ((delivery.is_none() && session.outbox.is_empty())- || session.closing.requested.is_some());+ let should_read = (delivery.is_none() && session.outbox.is_empty())+ || session.closing.requested.is_some();if reading != should_read {reading = should_read;sender.set_reading(reading);@@ -134,9 +120,9 @@ pub(super) async fn run_open_session(activity = async {tokio::select! {result = async { delivery.as_mut().expect("delivery exists").await }, if delivery.is_some() => Activity::Delivered(result),- command = commands.recv(), if !session.terminal => Activity::Command(command),+ command = commands.recv() => Activity::Command(command),event = connection.recv(), if should_read => Activity::Native(event),- _ = tokio::time::sleep_until(deadline), if !session.terminal && session.closing.deadline.is_some() => Activity::CloseTimeout,+ _ = tokio::time::sleep_until(deadline), if session.closing.deadline.is_some() => Activity::CloseTimeout,}} => activity,};@@ -203,6 +189,22 @@ pub(super) async fn run_open_session(}}}++ // Network termination ends admission immediately, even when the browser's+ // last events remain backpressured. Only delivery and the outbox survive.+ let outbox = std::mem::take(&mut session.outbox);+ drop(commands);+ drop(connection);+ drop(sender);+ drop(sending);+ drop(session);+ if let Some(delivery) = delivery {+ delivery.await?;+ }+ for event in outbox {+ send_event(&event_tx, event).await?;+ }+ Ok(())}impl Session {diff --git a/moli-websocket/src/synthetic.rs b/moli-websocket/src/synthetic.rsindex 3d313c1e5..355ca7b75 100644--- a/moli-websocket/src/synthetic.rs+++ b/moli-websocket/src/synthetic.rs@@ -15,6 +15,7 @@ pub(crate) async fn run_synthetic_websocket_connection(response_headers: Vec<(String, String)>,) -> EventResult {let Some(_connection_slot) = acquire_websocket_connection_slot() else {+ drop(command_rx);send_error_and_close(&event_tx,socket_id,@@ -44,7 +45,7 @@ pub(crate) async fn run_synthetic_websocket_connection(.await?;while let Some(queued) = command_rx.recv().await {- let _reservation = queued.reservation;+ let reservation = queued.reservation;match queued.command {Command::SendText(text) => {let amount = text.len();@@ -77,6 +78,8 @@ pub(crate) async fn run_synthetic_websocket_connection(send_event(&event_tx, Event::BinaryMessage { socket_id, data }).await?;}Command::ServerClose { code, reason } => {+ drop(command_rx);+ drop(reservation);let close_event_code = code.unwrap_or(1005);let close_event_reason = code.map(|_| reason).unwrap_or_default();send_event(@@ -92,6 +95,8 @@ pub(crate) async fn run_synthetic_websocket_connection(return Ok(());}Command::Close { code, reason } => {+ drop(command_rx);+ drop(reservation);let close_event_code = code.unwrap_or(1005);let close_event_reason = code.map(|_| reason).unwrap_or_default();send_event(&event_tx, Event::Closing { socket_id }).await?;@@ -108,6 +113,8 @@ pub(crate) async fn run_synthetic_websocket_connection(return Ok(());}Command::Fail(message) => {+ drop(command_rx);+ drop(reservation);send_error_and_close(&event_tx, socket_id, message).await?;return Ok(());}diff --git a/moli-websocket/src/terminal_tests.rs b/moli-websocket/src/terminal_tests.rsnew file mode 100644index 000000000..6046fa79e--- /dev/null+++ b/moli-websocket/src/terminal_tests.rs@@ -0,0 +1,229 @@+use crate::{+ Command, Event, EventSender, SendError,+ commands::{MAX_QUEUED_BYTES, MAX_QUEUED_MESSAGES, command_channel},+ connection::run_websocket_connection,+ handle::HandshakeDecision,+ test_support::{spawn_raw_websocket_response_server, test_websocket_context},+};+use std::sync::Arc;+use tokio::{+ io::AsyncReadExt,+ net::TcpListener,+ sync::{Semaphore, mpsc, oneshot},+ time::{Duration, timeout},+};++const DEADLINE: Duration = Duration::from_secs(3);++fn blocked_terminal_sink() -> (EventSender, mpsc::UnboundedReceiver<Event>, Arc<Semaphore>) {+ let (events, incoming) = mpsc::unbounded_channel();+ let release = Arc::new(Semaphore::new(0));+ let sink_release = release.clone();+ let sink = EventSender::with_async_sink(move |event| {+ let release = sink_release.clone();+ let events = events.clone();+ async move {+ let terminal = matches!(+ event,+ Event::Closing { .. } | Event::Error { .. } | Event::Close { .. }+ );+ events.send(event).unwrap();+ if terminal {+ release.acquire().await.unwrap().forget();+ }+ true+ }+ });+ (sink, incoming, release)+}++async fn next(events: &mut mpsc::UnboundedReceiver<Event>) -> Event {+ timeout(DEADLINE, events.recv()).await.unwrap().unwrap()+}++#[tokio::test]+async fn synthetic_terminal_discards_commands_before_blocked_delivery() {+ for terminal in [+ Command::Close {+ code: Some(1000),+ reason: String::new(),+ },+ Command::ServerClose {+ code: Some(1000),+ reason: String::new(),+ },+ Command::Fail("failed".to_owned()),+ ] {+ let (commands, receiver) = command_channel();+ commands.send(terminal).unwrap();+ commands.send(Command::SendBinary(vec![7; 4096])).unwrap();+ commands+ .send(Command::SendText("discarded".into()))+ .unwrap();+ let (sink, mut events, release) = blocked_terminal_sink();+ let task = tokio::spawn(crate::synthetic::run_synthetic_websocket_connection(+ 98,+ receiver,+ sink,+ Vec::new(),+ 101,+ Vec::new(),+ ));+ assert!(matches!(next(&mut events).await, Event::Open { .. }));+ let first = next(&mut events).await;+ assert!(commands.is_closed());+ assert_eq!(+ commands.send(Command::SendBinary(vec![1])),+ Err(SendError::Closed)+ );+ assert_eq!(+ commands.available_data_capacity(),+ (MAX_QUEUED_BYTES, MAX_QUEUED_MESSAGES)+ );+ release.add_permits(3);+ timeout(DEADLINE, task).await.unwrap().unwrap().unwrap();+ let mut closes = usize::from(matches!(first, Event::Close { .. }));+ while let Some(event) = events.recv().await {+ assert!(+ matches!(event, Event::Close { .. }),+ "unexpected terminal event: {event:?}"+ );+ closes += 1;+ }+ assert_eq!(closes, 1);+ }+}++#[tokio::test]+async fn failed_handshake_closes_commands_before_blocked_error() {+ let (url, server) = spawn_raw_websocket_response_server(+ "terminal-admission",+ b"HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n",+ )+ .await;+ // Also cover request validation, which ends before a native handle exists.+ for url in [url, "not a websocket URL".into()] {+ let (commands, receiver) = command_channel();+ let (sink, mut events, release) = blocked_terminal_sink();+ let task = tokio::spawn(run_websocket_connection(+ 99,+ url,+ Vec::new(),+ test_websocket_context(),+ receiver,+ sink,+ None,+ ));+ assert!(matches!(next(&mut events).await, Event::Error { .. }));+ assert!(commands.is_closed());+ assert_eq!(+ commands.send(Command::SendBinary(vec![1])),+ Err(SendError::Closed)+ );+ release.add_permits(2);+ assert!(matches!(+ next(&mut events).await,+ Event::Close { code: 1006, .. }+ ));+ timeout(DEADLINE, task).await.unwrap().unwrap().unwrap();+ assert!(events.recv().await.is_none());+ }+ server.await.unwrap();+}++#[tokio::test]+async fn close_during_handshake_releases_tcp_before_blocked_error() {+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();+ let url = format!("ws://{}/cancel-opening", listener.local_addr().unwrap());+ let (commands, receiver) = command_channel();+ let (sink, mut events, release) = blocked_terminal_sink();+ let task = tokio::spawn(run_websocket_connection(+ 100,+ url,+ Vec::new(),+ test_websocket_context(),+ receiver,+ sink,+ None,+ ));+ let (mut stream, _) = timeout(DEADLINE, listener.accept()).await.unwrap().unwrap();+ // Confirm the HTTP request arrived, then hold back the upgrade response.+ let mut request = Vec::new();+ timeout(DEADLINE, async {+ while !request.ends_with(b"\r\n\r\n") {+ request.push(stream.read_u8().await.unwrap());+ }+ })+ .await+ .unwrap();+ commands+ .send(Command::Close {+ code: Some(1000),+ reason: String::new(),+ })+ .unwrap();+ assert!(matches!(next(&mut events).await, Event::Error { .. }));+ assert!(commands.is_closed());+ assert_eq!(+ commands.send(Command::SendBinary(vec![1])),+ Err(SendError::Closed)+ );+ assert_eq!(+ timeout(DEADLINE, stream.read(&mut [0]))+ .await+ .unwrap()+ .unwrap(),+ 0+ );+ release.add_permits(2);+ assert!(matches!(+ next(&mut events).await,+ Event::Close { code: 1006, .. }+ ));+ timeout(DEADLINE, task).await.unwrap().unwrap().unwrap();+ assert!(events.recv().await.is_none());+}++#[tokio::test]+async fn rejected_handshake_decision_closes_commands_before_blocked_error() {+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();+ let url = format!("ws://{}/reject-open", listener.local_addr().unwrap());+ let server = tokio::spawn(async move {+ let (stream, _) = listener.accept().await.unwrap();+ let mut socket = tokio_tungstenite::accept_async(stream).await.unwrap();+ assert_eq!(socket.get_mut().read(&mut [0]).await.unwrap(), 0);+ });+ let (commands, receiver) = command_channel();+ let (decision, decide) = oneshot::channel();+ let (sink, mut events, release) = blocked_terminal_sink();+ let task = tokio::spawn(run_websocket_connection(+ 101,+ url,+ Vec::new(),+ test_websocket_context(),+ receiver,+ sink,+ Some(decide),+ ));+ assert!(matches!(+ next(&mut events).await,+ Event::HandshakeResponse { .. }+ ));+ decision+ .send(HandshakeDecision::Fail("rejected".into()))+ .unwrap();+ assert!(matches!(next(&mut events).await, Event::Error { .. }));+ assert!(commands.is_closed());+ assert_eq!(+ commands.send(Command::SendBinary(vec![1])),+ Err(SendError::Closed)+ );+ timeout(DEADLINE, server).await.unwrap().unwrap();+ release.add_permits(2);+ assert!(matches!(+ next(&mut events).await,+ Event::Close { code: 1006, .. }+ ));+ timeout(DEADLINE, task).await.unwrap().unwrap().unwrap();+ assert!(events.recv().await.is_none());+}