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): coalesce pending pong responses
ldm030869bb41851
diff --git a/moli-websocket/src/session.rs b/moli-websocket/src/session.rsindex 5a8f3b1a7..26a73222d 100644--- a/moli-websocket/src/session.rs+++ b/moli-websocket/src/session.rs@@ -19,6 +19,8 @@ const CLOSE_TIMEOUT: Duration = Duration::from_secs(5);// Browser delivery budget, independent of native queue capacity.const MAX_PENDING_INCOMING_MESSAGES: usize = 9;+#[cfg(test)]+mod pong_tests;#[cfg(test)]mod tests;@@ -56,7 +58,7 @@ struct Session {socket_id: u64,outbox: VecDeque<Event>,outgoing: VecDeque<Outgoing>,- pongs: VecDeque<Vec<u8>>,+ pending_pong: Option<Vec<u8>>,assembler: Assembler,closing: Closing,terminal: bool,@@ -75,7 +77,7 @@ pub(super) async fn run_open_session(socket_id,outbox: VecDeque::new(),outgoing: VecDeque::new(),- pongs: VecDeque::new(),+ pending_pong: None,assembler: Assembler::default(),closing: Closing::default(),terminal: false,@@ -253,7 +255,7 @@ impl Session {}fn next_frame(&mut self) -> Option<(CurlWebSocketSend, Flight)> {- if let Some(data) = self.pongs.pop_front() {+ if let Some(data) = self.pending_pong.take() {return Some((CurlWebSocketSend {flags: WsFlags::PONG,@@ -318,12 +320,10 @@ impl Session {match received {Received::Text(data) => self.queue_message(Event::TextMessage { socket_id, data }),Received::Binary(data) => self.queue_message(Event::BinaryMessage { socket_id, data }),- Received::Ping(data) => {- if self.pongs.len() == 4 {- self.fail("WebSocket control queue capacity exceeded".to_owned());- } else if !self.closing.sent {- self.pongs.push_back(data);- }+ Received::Ping(data) if !self.closing.sent => {+ // RFC 6455 permits replying only to the latest pending Ping.+ // A Pong already submitted to native I/O owns its payload.+ self.pending_pong = Some(data);}Received::Close {code,@@ -337,7 +337,7 @@ impl Session {.deadline.get_or_insert_with(|| Instant::now() + CLOSE_TIMEOUT);self.outgoing.clear();- self.pongs.clear();+ self.pending_pong = None;self.begin_close(payload, false);self.finish_close();}diff --git a/moli-websocket/src/session/pong_tests.rs b/moli-websocket/src/session/pong_tests.rsnew file mode 100644index 000000000..3bcc031db--- /dev/null+++ b/moli-websocket/src/session/pong_tests.rs@@ -0,0 +1,116 @@+use super::{Assembler, Closing, Flight, Session};+use crate::{+ Command, Event,+ commands::command_channel,+ frames::{Received, close_payload},+};+use moli_curl::websocket::{MAX_SEND_FRAME_BYTES, WsFlags};++fn session() -> Session {+ Session {+ socket_id: 84,+ outbox: Default::default(),+ outgoing: Default::default(),+ pending_pong: Default::default(),+ assembler: Assembler::default(),+ closing: Closing::default(),+ terminal: false,+ }+}++#[tokio::test]+async fn pending_pongs_coalesce_while_a_data_frame_is_in_flight() {+ let mut session = session();+ let (port, mut commands) = command_channel();+ port.send(Command::SendBinary(vec![7; 2 * MAX_SEND_FRAME_BYTES]))+ .unwrap();+ session.command(commands.recv().await.unwrap());+ let (first, flight) = session.next_frame().unwrap();+ // The driver cannot submit another frame before this completion. Process+ // incoming Pings with that completion deliberately withheld, without sleeps.+ for payload in 0..10 {+ session.received(Received::Ping(vec![payload]));+ assert!(+ !session.terminal,+ "Ping backlog must coalesce under send backpressure"+ );+ }+ session.sent(flight);+ let (pong, flight) = session.next_frame().unwrap();+ assert_eq!(pong.flags, WsFlags::PONG);+ assert_eq!(pong.data, [9]);+ session.sent(flight);+ let (last, flight) = session.next_frame().unwrap();+ assert_eq!(last.flags, WsFlags::BINARY);+ assert_eq!(+ [first.data, last.data].concat(),+ vec![7; 2 * MAX_SEND_FRAME_BYTES]+ );+ session.sent(flight);+ assert!(+ matches!(session.outbox.pop_front(), Some(Event::SendCompleted { payload_length, .. }) if payload_length == 2 * MAX_SEND_FRAME_BYTES)+ );+ assert!(session.next_frame().is_none());++ let payload = close_payload(Some(1000), String::new()).unwrap();+ session.begin_close(payload.clone(), true);+ let (close, flight) = session.next_frame().unwrap();+ assert_eq!(close.flags, WsFlags::CLOSE);+ session.sent(flight);+ session.received(Received::Close {+ code: 1000,+ reason: String::new(),+ payload,+ });+ assert!(session.terminal);+ assert!(matches!(+ session.outbox.pop_front(),+ Some(Event::Closing { .. })+ ));+ assert!(matches!(+ session.outbox.pop_front(),+ Some(Event::Close {+ code: 1000,+ was_clean: true,+ ..+ })+ ));+ assert!(session.outbox.is_empty());+}++#[test]+fn in_flight_pong_retains_its_payload_while_later_pings_coalesce() {+ let mut session = session();+ session.received(Received::Ping(vec![42]));+ let (in_flight, flight) = session.next_frame().unwrap();+ assert!(matches!(flight, Flight::Pong));+ for payload in 0..10 {+ session.received(Received::Ping(vec![payload]));+ assert!(!session.terminal);+ }+ // An empty Ping still requires an empty Pong; it is not "no pending Pong".+ session.received(Received::Ping(Vec::new()));+ assert_eq!(in_flight.data, [42]);+ session.sent(flight);+ let (latest, flight) = session.next_frame().unwrap();+ assert_eq!(latest.flags, WsFlags::PONG);+ assert!(latest.data.is_empty());+ session.sent(flight);+ assert!(session.next_frame().is_none());+}++#[test]+fn peer_close_discards_unsent_pong() {+ let mut session = session();+ session.received(Received::Ping(vec![1]));+ session.received(Received::Close {+ code: 1000,+ reason: String::new(),+ payload: close_payload(Some(1000), String::new()).unwrap(),+ });+ let (reply, flight) = session.next_frame().unwrap();+ assert_eq!(reply.flags, WsFlags::CLOSE);+ session.sent(flight);+ assert!(session.terminal);+ assert!(session.next_frame().is_none());+}diff --git a/moli-websocket/src/session/tests.rs b/moli-websocket/src/session/tests.rsindex ea3ed534d..9a0b7f809 100644--- a/moli-websocket/src/session/tests.rs+++ b/moli-websocket/src/session/tests.rs@@ -183,7 +183,7 @@ async fn completed_send_notifications_bound_further_message_scheduling() {socket_id: 83,outbox: Default::default(),outgoing: Default::default(),- pongs: Default::default(),+ pending_pong: Default::default(),assembler: Assembler::default(),closing: Closing::default(),terminal: false,