Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(websocket): encapsulate connection commands and cancellation

ldm0e0e6451747ad
diff --git a/moli-renderer-v8/src/native_bridge/context_host/websockets/commands.rs b/moli-renderer-v8/src/native_bridge/context_host/websockets/commands.rs
index 712861df1..28486984e 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/websockets/commands.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/websockets/commands.rs
@@ -140,10 +140,7 @@ impl JsContextHost {
return false;
};
if let Some(command_tx) = state.command_tx {
- let _ = command_tx.send(WebSocketCommand::Close {
- code: None,
- reason: String::new(),
- });
+ command_tx.cancel();
}
let internal_ids = [state.fetch_internal_id, state.response_interception_pending]
.into_iter()
diff --git a/moli-renderer-v8/src/worker/global_scope/fetch.rs b/moli-renderer-v8/src/worker/global_scope/fetch.rs
index 2d90ffaa4..1705f1746 100644
--- a/moli-renderer-v8/src/worker/global_scope/fetch.rs
+++ b/moli-renderer-v8/src/worker/global_scope/fetch.rs
@@ -1335,10 +1335,7 @@ pub(crate) fn register_worker_websocket<'s>(
);
let cancel_tx = command_tx.clone();
load.attach_consumer_cancel(move || {
- let _ = cancel_tx.send(WebSocketCommand::Close {
- code: Some(1001),
- reason: "WorkerGlobalScope terminated".to_owned(),
- });
+ cancel_tx.cancel();
});
(command_tx, Some(load))
};
diff --git a/moli-websocket/src/connection.rs b/moli-websocket/src/connection.rs
index f783572c7..a72a30df3 100644
--- a/moli-websocket/src/connection.rs
+++ b/moli-websocket/src/connection.rs
@@ -7,6 +7,7 @@ use tokio_tungstenite::tungstenite::protocol::{CloseFrame, Message};
use crate::{
Command, ConnectOptions, Event, FrameOpcode,
events::{EventSender, send_error_and_close, send_event},
+ handle::AbortOnDrop,
headers::header_map_entries,
limits::{acquire_pending_websocket_handshake_slot, acquire_websocket_connection_slot},
request::build_websocket_request,
@@ -197,6 +198,7 @@ async fn run_open_websocket_connection(
writer_control_rx,
writer_event_tx,
));
+ let _writer_task = AbortOnDrop::new(writer.abort_handle());
let mut sent_close: Option<(u16, String)> = None;
let mut pending_buffered_amount = VecDeque::new();
let mut writer_done = false;
@@ -391,7 +393,6 @@ async fn run_open_websocket_connection(
}
}
}
- writer.abort();
}
enum WebSocketWriterEvent {
diff --git a/moli-websocket/src/handle.rs b/moli-websocket/src/handle.rs
new file mode 100644
index 000000000..e961e2420
--- /dev/null
+++ b/moli-websocket/src/handle.rs
@@ -0,0 +1,93 @@
+use std::sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+};
+
+use tokio::{sync::mpsc, task::AbortHandle};
+
+use crate::Command;
+
+/// The producer's ownership of a WebSocket connection.
+///
+/// Clones keep the connection alive. Dropping the last handle or calling
+/// `cancel` releases the connection even while its handshake, write, or event
+/// sink is waiting. Use `Command::Close` for the graceful closing handshake.
+#[derive(Clone, Debug)]
+pub struct ConnectionHandle {
+ inner: Arc<ConnectionControl>,
+}
+
+#[derive(Debug)]
+struct ConnectionControl {
+ command_tx: mpsc::UnboundedSender<Command>,
+ cancelled: AtomicBool,
+ task: AbortOnDrop,
+}
+
+impl ConnectionHandle {
+ pub(crate) fn new(command_tx: mpsc::UnboundedSender<Command>, task: AbortHandle) -> Self {
+ Self {
+ inner: Arc::new(ConnectionControl {
+ command_tx,
+ cancelled: AtomicBool::new(false),
+ task: AbortOnDrop::new(task),
+ }),
+ }
+ }
+
+ /// Enqueues a command without waiting for network I/O or event delivery.
+ pub fn send(&self, command: Command) -> Result<(), CommandSendError> {
+ if self.inner.cancelled.load(Ordering::Acquire) {
+ return Err(CommandSendError(command));
+ }
+ self.inner
+ .command_tx
+ .send(command)
+ .map_err(|error| CommandSendError(error.0))
+ }
+
+ /// Cancels all work owned by this connection, including blocked delivery.
+ ///
+ /// Cancellation is idempotent, affects all clones, and does not enqueue a
+ /// graceful Close or publish further browser events.
+ pub fn cancel(&self) {
+ self.inner.cancelled.store(true, Ordering::Release);
+ self.inner.task.abort();
+ }
+
+ pub fn is_closed(&self) -> bool {
+ self.inner.cancelled.load(Ordering::Acquire) || self.inner.command_tx.is_closed()
+ }
+}
+
+/// A command rejected because the connection is no longer available.
+#[derive(Debug)]
+pub struct CommandSendError(pub Command);
+
+impl std::fmt::Display for CommandSendError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("WebSocket connection is closed")
+ }
+}
+
+impl std::error::Error for CommandSendError {}
+
+/// Child tasks must not outlive the connection task when it is cancelled.
+#[derive(Debug)]
+pub(crate) struct AbortOnDrop(AbortHandle);
+
+impl AbortOnDrop {
+ pub(crate) fn new(task: AbortHandle) -> Self {
+ Self(task)
+ }
+
+ fn abort(&self) {
+ self.0.abort();
+ }
+}
+
+impl Drop for AbortOnDrop {
+ fn drop(&mut self) {
+ self.abort();
+ }
+}
diff --git a/moli-websocket/src/lib.rs b/moli-websocket/src/lib.rs
index b6f626814..2bc78044d 100644
--- a/moli-websocket/src/lib.rs
+++ b/moli-websocket/src/lib.rs
@@ -1,6 +1,7 @@
mod connection;
mod cookie;
mod events;
+mod handle;
mod handshake;
mod headers;
mod limits;
@@ -15,6 +16,7 @@ mod types;
pub use cookie::websocket_cookie_url;
pub use events::EventSender;
+pub use handle::{CommandSendError, ConnectionHandle};
pub use protocol::{
WebSocketCloseRequest, WebSocketCloseValidationError, WebSocketSubprotocolError,
WebSocketUrlError, close_info_code_from_number, default_close_code_for_reason,
diff --git a/moli-websocket/src/runtime.rs b/moli-websocket/src/runtime.rs
index 77681d979..ee855ce7c 100644
--- a/moli-websocket/src/runtime.rs
+++ b/moli-websocket/src/runtime.rs
@@ -3,8 +3,9 @@ use std::sync::OnceLock;
use tokio::sync::mpsc;
use crate::{
- CommandSender, ConnectOptions, connection::run_websocket_connection, events::EventSender,
- events::send_error_and_close, synthetic::run_synthetic_websocket_connection,
+ CommandSender, ConnectOptions, ConnectionHandle, connection::run_websocket_connection,
+ events::EventSender, events::send_error_and_close,
+ synthetic::run_synthetic_websocket_connection,
};
pub fn spawn_connection(
@@ -16,10 +17,10 @@ pub fn spawn_connection(
) -> CommandSender {
let event_tx = event_tx.into();
let (command_tx, command_rx) = mpsc::unbounded_channel();
- websocket_runtime().spawn(async move {
+ let task = websocket_runtime().spawn(async move {
run_websocket_connection(socket_id, url, protocols, context, command_rx, event_tx).await;
});
- command_tx
+ ConnectionHandle::new(command_tx, task.abort_handle())
}
pub fn spawn_failed_connection(
@@ -29,10 +30,10 @@ pub fn spawn_failed_connection(
) -> CommandSender {
let event_tx = event_tx.into();
let (command_tx, _command_rx) = mpsc::unbounded_channel();
- websocket_runtime().spawn(async move {
+ let task = websocket_runtime().spawn(async move {
send_error_and_close(&event_tx, socket_id, message).await;
});
- command_tx
+ ConnectionHandle::new(command_tx, task.abort_handle())
}
pub fn spawn_synthetic_connection(
@@ -44,7 +45,7 @@ pub fn spawn_synthetic_connection(
) -> CommandSender {
let event_tx = event_tx.into();
let (command_tx, command_rx) = mpsc::unbounded_channel();
- websocket_runtime().spawn(async move {
+ let task = websocket_runtime().spawn(async move {
run_synthetic_websocket_connection(
socket_id,
command_rx,
@@ -55,7 +56,7 @@ pub fn spawn_synthetic_connection(
)
.await;
});
- command_tx
+ ConnectionHandle::new(command_tx, task.abort_handle())
}
fn websocket_runtime() -> &'static tokio::runtime::Runtime {
diff --git a/moli-websocket/src/tests.rs b/moli-websocket/src/tests.rs
index 816e590ef..843e6f7b8 100644
--- a/moli-websocket/src/tests.rs
+++ b/moli-websocket/src/tests.rs
@@ -453,6 +453,117 @@ async fn websocket_failed_connection_reports_error_then_abnormal_close() {
}
}
+#[tokio::test]
+async fn websocket_last_handle_drop_releases_pending_handshake() {
+ use tokio::io::AsyncReadExt;
+
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let url = format!("ws://{}/pending", listener.local_addr().unwrap());
+ let (event_tx, _event_rx) = mpsc::channel(1);
+ let handle = spawn_connection(61, url, Vec::new(), test_websocket_context(), event_tx);
+ let retained = handle.clone();
+ let (mut server, _) = timeout(Duration::from_secs(3), listener.accept())
+ .await
+ .expect("connection should reach the server")
+ .unwrap();
+ drop(handle);
+ retained
+ .send(Command::SendText("ignored while connecting".to_owned()))
+ .expect("another producer still owns the connection");
+ drop(retained);
+ let result = timeout(Duration::from_secs(3), server.read_to_end(&mut Vec::new()))
+ .await
+ .expect("dropping the last producer must close a pending handshake");
+ if let Err(error) = result {
+ assert_eq!(error.kind(), std::io::ErrorKind::ConnectionReset);
+ }
+}
+
+struct WebSocketSinkDropSignal(mpsc::Sender<()>);
+
+impl Drop for WebSocketSinkDropSignal {
+ fn drop(&mut self) {
+ let _ = self.0.try_send(());
+ }
+}
+
+#[tokio::test]
+async fn websocket_cancel_releases_blocked_sink_and_open_writer() {
+ use futures_util::{SinkExt, StreamExt};
+
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let url = format!("ws://{}/cancel", listener.local_addr().unwrap());
+ let entered = Arc::new(tokio::sync::Notify::new());
+ let sink_entered = Arc::clone(&entered);
+ let (dropped_tx, mut dropped_rx) = mpsc::channel(1);
+ let sink = EventSender::with_async_sink(move |event| {
+ let entered = Arc::clone(&sink_entered);
+ let dropped_tx = dropped_tx.clone();
+ async move {
+ if matches!(event, Event::TextMessage { .. }) {
+ let _guard = WebSocketSinkDropSignal(dropped_tx);
+ entered.notify_one();
+ return std::future::pending::<bool>().await;
+ }
+ true
+ }
+ });
+ let handle = spawn_connection(62, url, Vec::new(), test_websocket_context(), sink);
+ let retained = handle.clone();
+ let (stream, _) = timeout(Duration::from_secs(3), listener.accept())
+ .await
+ .unwrap()
+ .unwrap();
+ let mut server = tokio_tungstenite::accept_async(stream).await.unwrap();
+ server.send("block the sink".into()).await.unwrap();
+ timeout(Duration::from_secs(3), entered.notified())
+ .await
+ .expect("receive task must enter the blocked sink");
+ handle.cancel();
+ handle.cancel();
+ assert!(retained.is_closed());
+ assert!(retained.send(Command::SendBinary(vec![1])).is_err());
+ timeout(Duration::from_secs(3), dropped_rx.recv())
+ .await
+ .expect("cancellation must release the blocked event future")
+ .expect("sink drop signal");
+ let terminal = timeout(Duration::from_secs(3), server.next())
+ .await
+ .expect("the writer must release its socket while producer clones remain alive");
+ assert!(terminal.is_none() || terminal.is_some_and(|result| result.is_err()));
+}
+
+#[tokio::test]
+async fn websocket_failed_and_synthetic_delivery_cancel_on_last_handle_drop() {
+ for synthetic in [false, true] {
+ let entered = Arc::new(tokio::sync::Notify::new());
+ let sink_entered = Arc::clone(&entered);
+ let (dropped_tx, mut dropped_rx) = mpsc::channel(1);
+ let sink = EventSender::with_async_sink(move |_event| {
+ let entered = Arc::clone(&sink_entered);
+ let dropped_tx = dropped_tx.clone();
+ async move {
+ let _guard = WebSocketSinkDropSignal(dropped_tx);
+ entered.notify_one();
+ std::future::pending::<bool>().await
+ }
+ });
+ let handle = if synthetic {
+ spawn_synthetic_connection(63, Vec::new(), 101, Vec::new(), sink)
+ } else {
+ spawn_failed_connection(63, "rejected".to_owned(), sink)
+ };
+ timeout(Duration::from_secs(3), entered.notified())
+ .await
+ .expect("connection should reach the async sink");
+ drop(handle);
+ timeout(Duration::from_secs(3), dropped_rx.recv())
+ .await
+ .expect("last producer drop must cancel blocked delivery")
+ .expect("sink drop signal");
+ }
+}
+
#[tokio::test]
async fn websocket_synthetic_connection_opens_accounts_send_and_closes_cleanly() {
let (event_tx, mut event_rx) = mpsc::channel(32);
diff --git a/moli-websocket/src/types.rs b/moli-websocket/src/types.rs
index d84e3f927..45fb1a9ad 100644
--- a/moli-websocket/src/types.rs
+++ b/moli-websocket/src/types.rs
@@ -1,5 +1,3 @@
-use tokio::sync::mpsc;
-
#[derive(Debug, Clone)]
pub struct ConnectOptions {
pub origin: String,
@@ -106,4 +104,5 @@ pub enum Command {
},
}
-pub type CommandSender = mpsc::UnboundedSender<Command>;
+/// Compatibility name for the connection's producer capability.
+pub type CommandSender = crate::ConnectionHandle;