Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(websocket): decouple send progress from event delivery

ldm00c116aa35027
diff --git a/moli-curl/src/websocket.rs b/moli-curl/src/websocket.rs
index 44f4f024f..63eca7d0a 100644
--- a/moli-curl/src/websocket.rs
+++ b/moli-curl/src/websocket.rs
@@ -30,7 +30,8 @@ pub use curl::easy::{WsFlags, WsFrame};
/// Fragment large messages above this layer to bound native write residence.
pub const MAX_SEND_FRAME_BYTES: usize = 64 * 1024;
-const EVENT_CAPACITY: usize = 8;
+/// Maximum native events queued per connection, including chunks and send receipts.
+pub const MAX_PENDING_EVENTS: usize = 8;
const DATA_CAPACITY: usize = 8;
const CONTROL_CAPACITY: usize = 4;
const SESSION_CAPACITY: usize = 255;
@@ -168,6 +169,8 @@ impl CurlWebSocketSender {
}
/// Handshake delivery starts paused, so application data cannot outrun Open.
+ /// Pausing does not retract queued chunks or a read already in progress.
+ /// Keep receiving events while paused so send receipts can make progress.
pub fn set_reading(&self, enabled: bool) {
self.control.reading.store(enabled, Ordering::Release);
self.control.wake();
@@ -309,7 +312,7 @@ impl CurlWebSocketRuntime {
#[cfg(test)]
write_blocked: tokio::sync::Notify::new(),
});
- let (event_tx, events) = mpsc::channel(EVENT_CAPACITY);
+ let (event_tx, events) = mpsc::channel(MAX_PENDING_EVENTS);
let (data_tx, data) = mpsc::channel(DATA_CAPACITY);
let (control_tx, control_frames) = mpsc::channel(CONTROL_CAPACITY);
let io = SessionIo {
diff --git a/moli-curl/src/websocket/tests.rs b/moli-curl/src/websocket/tests.rs
index b23c05197..f943ff00c 100644
--- a/moli-curl/src/websocket/tests.rs
+++ b/moli-curl/src/websocket/tests.rs
@@ -203,7 +203,7 @@ async fn native_full_delivery_queue_does_not_block_other_sessions_or_cancellatio
timeout(DEADLINE, blocked.sender.control.read_blocked.notified())
.await
.unwrap();
- assert_eq!(blocked.events.len(), EVENT_CAPACITY);
+ assert_eq!(blocked.events.len(), MAX_PENDING_EVENTS);
let (other_url, other_task) = server(|mut stream| {
upgrade(&mut stream, &[0x81, 2, b'o', b'k']);
diff --git a/moli-websocket/src/session.rs b/moli-websocket/src/session.rs
index a1aa29875..21013b275 100644
--- a/moli-websocket/src/session.rs
+++ b/moli-websocket/src/session.rs
@@ -1,12 +1,15 @@
//! Browser session state on top of the native frame transport.
use crate::{
Command, Event, FrameOpcode,
- commands::{CommandReceiver, QueuedCommand, Reservation},
+ commands::{
+ CommandReceiver, MAX_QUEUED_BYTES, MAX_QUEUED_MESSAGES, QueuedCommand, Reservation,
+ },
events::{EventResult, EventSender, send_event},
frames::{Assembler, Received, close_payload},
};
use moli_curl::websocket::{
- CurlWebSocketConnection, CurlWebSocketEvent, CurlWebSocketSend, MAX_SEND_FRAME_BYTES, WsFlags,
+ CurlWebSocketConnection, CurlWebSocketEvent, CurlWebSocketSend, MAX_PENDING_EVENTS,
+ MAX_SEND_FRAME_BYTES, WsFlags,
};
use std::{collections::VecDeque, future::Future, pin::Pin};
use tokio::time::{Duration, Instant};
@@ -14,6 +17,8 @@ use tokio::time::{Duration, Instant};
type Delivery = Pin<Box<dyn Future<Output = EventResult> + Send>>;
type Sending = Pin<Box<dyn Future<Output = Result<(), String>> + Send>>;
const CLOSE_TIMEOUT: Duration = Duration::from_secs(5);
+// A pause can race one native read in addition to the already queued events.
+const MAX_PENDING_INCOMING_MESSAGES: usize = MAX_PENDING_EVENTS + 1;
#[cfg(test)]
mod tests;
@@ -57,7 +62,8 @@ pub(super) async fn run_open_session(
event_tx: EventSender,
) -> EventResult {
let sender = connection.sender();
- sender.set_reading(true);
+ let mut reading = true;
+ sender.set_reading(reading);
let mut session = Session {
socket_id,
outbox: VecDeque::new(),
@@ -101,6 +107,15 @@ pub(super) async fn run_open_session(
}));
flight = Some(next_flight);
}
+ // Apply application backpressure to socket reads, not to the shared
+ // event queue: Sent must remain consumable to finish outgoing messages.
+ let should_read = !session.terminal
+ && ((delivery.is_none() && session.outbox.is_empty())
+ || session.closing.requested.is_some());
+ if reading != should_read {
+ reading = should_read;
+ sender.set_reading(reading);
+ }
let deadline = session
.closing
.deadline
@@ -120,7 +135,7 @@ pub(super) async fn run_open_session(
sending = None;
if let Err(error) = result { session.fail(error); }
}
- event = connection.recv(), if !session.terminal && ((delivery.is_none() && session.outbox.is_empty()) || session.closing.requested.is_some()) => {
+ event = connection.recv(), if !session.terminal => {
match event {
Some(CurlWebSocketEvent::Chunk { data, frame }) => match session.assembler.push(data, frame) {
Ok(Some(received)) => session.received(received),
@@ -203,6 +218,19 @@ impl Session {
));
}
if let Some(message) = self.outgoing.front() {
+ // Native completion releases the data reservation, but its browser
+ // notifications can still be blocked. Bound that separate residence
+ // at message boundaries so a fragmented message always finishes.
+ if message.offset == 0
+ && self
+ .outbox
+ .iter()
+ .filter(|event| matches!(event, Event::FrameSent { .. }))
+ .count()
+ >= MAX_QUEUED_MESSAGES
+ {
+ return None;
+ }
let end = (message.offset + MAX_SEND_FRAME_BYTES).min(message.data.len());
let last = end == message.data.len();
let mut flags = match message.opcode {
@@ -285,16 +313,16 @@ impl Session {
_ => None,
}
}
- // Closing must still read control frames while an older delivery waits.
- // Preserve arriving messages in order, with a separate finite residence.
+ // Preserve chunks already admitted when native reads paused. Closing
+ // also keeps reading control frames; its message backlog stays bounded.
let (count, bytes) = self
.outbox
.iter()
.filter_map(message_size)
.fold((0, 0usize), |(count, bytes), len| (count + 1, bytes + len));
let size = message_size(&event).expect("only message events use this queue");
- if count >= 8 || bytes.saturating_add(size) > crate::commands::MAX_QUEUED_BYTES {
- self.fail("WebSocket closing delivery queue capacity exceeded".to_owned());
+ if count >= MAX_PENDING_INCOMING_MESSAGES || bytes.saturating_add(size) > MAX_QUEUED_BYTES {
+ self.fail("WebSocket delivery queue capacity exceeded".to_owned());
} else {
self.outbox.push_back(event);
}
diff --git a/moli-websocket/src/session/tests.rs b/moli-websocket/src/session/tests.rs
index df65a9938..f6814e9d7 100644
--- a/moli-websocket/src/session/tests.rs
+++ b/moli-websocket/src/session/tests.rs
@@ -116,3 +116,135 @@ async fn native_close_still_times_out_when_peer_never_answers_close() {
finish_tx.send(()).unwrap();
server.await.unwrap();
}
+
+#[tokio::test]
+async fn native_fragmented_sends_complete_while_incoming_sink_is_blocked() {
+ use std::sync::Arc;
+ use tokio::sync::{Notify, Semaphore};
+
+ const MESSAGE_BYTES: usize = 128 * 1024;
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let url = format!("ws://{}/independent-sends", listener.local_addr().unwrap());
+ let blocked = Arc::new(Notify::new());
+ let resume = Arc::new(Semaphore::new(0));
+ let (received_tx, received_rx) = oneshot::channel();
+ let server_resume = resume.clone();
+ let server = tokio::spawn(async move {
+ let (stream, _) = listener.accept().await.unwrap();
+ let mut socket = tokio_tungstenite::accept_async(stream).await.unwrap();
+ socket.send(Message::Text("blocked".into())).await.unwrap();
+ for expected in [17, 29] {
+ let Message::Binary(data) = socket.next().await.unwrap().unwrap() else {
+ panic!("expected a complete binary message before any Close");
+ };
+ assert_eq!(data.len(), MESSAGE_BYTES);
+ assert!(data.iter().all(|byte| *byte == expected));
+ }
+ // The sink can only resume after both multi-frame messages reach the
+ // server. Sending Close earlier would bypass the original deadlock.
+ server_resume.add_permits(1);
+ received_tx.send(()).unwrap();
+ assert!(matches!(socket.next().await, Some(Ok(Message::Close(_)))));
+ socket.flush().await.unwrap();
+ });
+ let (events, mut rx) = mpsc::channel(8);
+ let sink = crate::EventSender::with_async_sink({
+ let blocked = blocked.clone();
+ move |event| {
+ let blocked = blocked.clone();
+ let resume = resume.clone();
+ let events = events.clone();
+ async move {
+ if matches!(event, Event::TextMessage { .. }) {
+ blocked.notify_one();
+ resume.acquire().await.unwrap().forget();
+ }
+ events.send(event).await.is_ok()
+ }
+ }
+ });
+ let handle = spawn_connection(82, url, Vec::new(), test_websocket_context(), sink);
+ recv_open_event(&mut rx).await;
+ timeout(Duration::from_secs(3), blocked.notified())
+ .await
+ .unwrap();
+ for byte in [17, 29] {
+ handle
+ .send(Command::SendBinary(vec![byte; MESSAGE_BYTES]))
+ .unwrap();
+ }
+ timeout(Duration::from_secs(3), received_rx)
+ .await
+ .expect("all fragments must be sent before the incoming sink resumes")
+ .unwrap();
+ assert_text_message(&mut rx, 82, "blocked").await;
+ for _ in 0..2 {
+ assert_frame_sent(&mut rx, 82, FrameOpcode::Binary, MESSAGE_BYTES).await;
+ assert_buffered_amount_consumed(&mut rx, 82, MESSAGE_BYTES).await;
+ }
+ handle
+ .send(Command::Close {
+ code: Some(1000),
+ reason: String::new(),
+ })
+ .unwrap();
+ assert_closing(&mut rx, 82).await;
+ assert_close(&mut rx, 82, 1000, "", true).await;
+ server.await.unwrap();
+}
+
+#[tokio::test]
+async fn completed_send_notifications_bound_further_message_scheduling() {
+ use super::{Assembler, Closing, Session};
+ use crate::commands::{MAX_QUEUED_MESSAGES, command_channel};
+ use moli_curl::websocket::MAX_SEND_FRAME_BYTES;
+
+ let mut session = Session {
+ socket_id: 83,
+ outbox: Default::default(),
+ outgoing: Default::default(),
+ pongs: Default::default(),
+ assembler: Assembler::default(),
+ closing: Closing::default(),
+ terminal: false,
+ };
+ let (port, mut commands) = command_channel();
+ // Complete a full admission window while the application's notifications
+ // remain undelivered. Their residence must not grow with new send calls.
+ for _ in 0..MAX_QUEUED_MESSAGES {
+ port.send(Command::SendText("sent".to_owned())).unwrap();
+ session.command(commands.recv().await.unwrap());
+ let (_, flight) = session
+ .next_frame()
+ .expect("bounded sends can make progress");
+ session.sent(flight);
+ }
+ port.send(Command::SendBinary(vec![7; 2 * MAX_SEND_FRAME_BYTES]))
+ .unwrap();
+ session.command(commands.recv().await.unwrap());
+ assert!(
+ session.next_frame().is_none(),
+ "pending completion notifications must bound new messages"
+ );
+ assert!(matches!(
+ session.outbox.pop_front(),
+ Some(Event::FrameSent { .. })
+ ));
+ for _ in 0..2 {
+ let (frame, flight) = session
+ .next_frame()
+ .expect("notification delivery permits the next complete message");
+ assert_eq!(frame.data.len(), MAX_SEND_FRAME_BYTES);
+ session.sent(flight);
+ }
+ assert!(session.outgoing.is_empty());
+ assert!(!session.terminal);
+ assert_eq!(
+ session
+ .outbox
+ .iter()
+ .filter(|event| matches!(event, Event::FrameSent { .. }))
+ .count(),
+ MAX_QUEUED_MESSAGES
+ );
+}