Skip to main content

chromiumoxide/
conn.rs

1use std::collections::VecDeque;
2use std::marker::PhantomData;
3use std::pin::Pin;
4use std::task::ready;
5
6use futures_util::stream::{FuturesOrdered, SplitSink};
7use futures_util::{SinkExt, Stream, StreamExt};
8use std::future::Future;
9use std::task::{Context, Poll};
10use tokio::sync::mpsc;
11use tokio_tungstenite::tungstenite::Message as WsMessage;
12use tokio_tungstenite::MaybeTlsStream;
13use tokio_tungstenite::{tungstenite::protocol::WebSocketConfig, WebSocketStream};
14
15use chromiumoxide_cdp::cdp::browser_protocol::target::SessionId;
16use chromiumoxide_types::{CallId, EventMessage, Message, MethodCall, MethodId};
17
18use crate::error::CdpError;
19use crate::error::Result;
20
21type ConnectStream = MaybeTlsStream<tokio::net::TcpStream>;
22
23/// Exchanges the messages with the websocket
24#[must_use = "streams do nothing unless polled"]
25#[derive(Debug)]
26pub struct Connection<T: EventMessage> {
27    /// Queue of commands to send.
28    pending_commands: VecDeque<MethodCall>,
29    /// The websocket of the chromium instance
30    ws: WebSocketStream<ConnectStream>,
31    /// The identifier for a specific command
32    next_id: usize,
33    /// Whether the write buffer has unsent data that needs flushing.
34    needs_flush: bool,
35    /// The phantom marker.
36    _marker: PhantomData<T>,
37}
38
39lazy_static::lazy_static! {
40    /// Nagle's algorithm disabled?
41    static ref DISABLE_NAGLE: bool = match std::env::var("DISABLE_NAGLE") {
42        Ok(disable_nagle) => disable_nagle == "true",
43        _ => true
44    };
45    /// Websocket config defaults
46    static ref WEBSOCKET_DEFAULTS: bool = match std::env::var("WEBSOCKET_DEFAULTS") {
47        Ok(d) => d == "true",
48        _ => false
49    };
50}
51
52/// Default number of WebSocket connection retry attempts.
53pub const DEFAULT_CONNECTION_RETRIES: u32 = 4;
54
55/// Initial backoff delay between connection retries (in milliseconds).
56const INITIAL_BACKOFF_MS: u64 = 50;
57
58/// Maximum backoff delay between connection retries (in milliseconds).
59const MAX_BACKOFF_MS: u64 = 2_000;
60
61impl<T: EventMessage + Unpin> Connection<T> {
62    pub async fn connect(debug_ws_url: impl AsRef<str>) -> Result<Self> {
63        Self::connect_with_retries(debug_ws_url, DEFAULT_CONNECTION_RETRIES).await
64    }
65
66    pub async fn connect_with_retries(debug_ws_url: impl AsRef<str>, retries: u32) -> Result<Self> {
67        let mut config = WebSocketConfig::default();
68
69        // Cap the internal write buffer so a slow receiver cannot cause
70        // unbounded memory growth (default is usize::MAX).
71        config.max_write_buffer_size = 4 * 1024 * 1024;
72
73        if !*WEBSOCKET_DEFAULTS {
74            config.max_message_size = None;
75            config.max_frame_size = None;
76        }
77
78        let url = debug_ws_url.as_ref();
79        let use_uring = crate::uring_fs::is_enabled();
80        let mut last_err = None;
81
82        for attempt in 0..=retries {
83            let result = if use_uring {
84                Self::connect_uring(url, config).await
85            } else {
86                Self::connect_default(url, config).await
87            };
88
89            match result {
90                Ok(ws) => {
91                    return Ok(Self {
92                        pending_commands: Default::default(),
93                        ws,
94                        next_id: 0,
95                        needs_flush: false,
96                        _marker: Default::default(),
97                    });
98                }
99                Err(e) => {
100                    // Detect non-retriable errors early to avoid wasting time
101                    // on connections that will never succeed.
102                    let should_retry = match &e {
103                        // Connection refused — nothing is listening on this port.
104                        CdpError::Io(io_err)
105                            if io_err.kind() == std::io::ErrorKind::ConnectionRefused =>
106                        {
107                            false
108                        }
109                        // HTTP response to a WebSocket upgrade (e.g. wrong path
110                        // returns 404 / redirect) — retrying the same URL won't help.
111                        CdpError::Ws(tungstenite_err) => !matches!(
112                            tungstenite_err,
113                            tokio_tungstenite::tungstenite::Error::Http(_)
114                                | tokio_tungstenite::tungstenite::Error::HttpFormat(_)
115                        ),
116                        _ => true,
117                    };
118
119                    last_err = Some(e);
120
121                    if !should_retry {
122                        break;
123                    }
124
125                    if attempt < retries {
126                        let backoff_ms =
127                            (INITIAL_BACKOFF_MS * 3u64.saturating_pow(attempt)).min(MAX_BACKOFF_MS);
128                        tokio::time::sleep(std::time::Duration::from_millis(backoff_ms)).await;
129                    }
130                }
131            }
132        }
133
134        Err(last_err.unwrap_or_else(|| CdpError::msg("connection failed")))
135    }
136
137    /// Default path: let tokio-tungstenite handle TCP connect + WS handshake.
138    async fn connect_default(
139        url: &str,
140        config: WebSocketConfig,
141    ) -> Result<WebSocketStream<ConnectStream>> {
142        let (ws, _) =
143            tokio_tungstenite::connect_async_with_config(url, Some(config), *DISABLE_NAGLE).await?;
144        Ok(ws)
145    }
146
147    /// io_uring path: pre-connect the TCP socket via io_uring, then do WS
148    /// handshake over the pre-connected stream.
149    async fn connect_uring(
150        url: &str,
151        config: WebSocketConfig,
152    ) -> Result<WebSocketStream<ConnectStream>> {
153        use tokio_tungstenite::tungstenite::client::IntoClientRequest;
154
155        let request = url.into_client_request()?;
156        let host = request
157            .uri()
158            .host()
159            .ok_or_else(|| CdpError::msg("no host in CDP WebSocket URL"))?;
160        let port = request.uri().port_u16().unwrap_or(9222);
161
162        // Resolve host → SocketAddr (CDP is always localhost, so this is fast).
163        let addr_str = format!("{}:{}", host, port);
164        let addr: std::net::SocketAddr = match addr_str.parse() {
165            Ok(a) => a,
166            Err(_) => {
167                // Hostname needs DNS — fall back to default path.
168                return Self::connect_default(url, config).await;
169            }
170        };
171
172        // TCP connect via io_uring.
173        let std_stream = crate::uring_fs::tcp_connect(addr)
174            .await
175            .map_err(CdpError::Io)?;
176
177        // Set non-blocking + Nagle.
178        std_stream.set_nonblocking(true).map_err(CdpError::Io)?;
179        if *DISABLE_NAGLE {
180            let _ = std_stream.set_nodelay(true);
181        }
182
183        // Wrap in tokio TcpStream.
184        let tokio_stream = tokio::net::TcpStream::from_std(std_stream).map_err(CdpError::Io)?;
185
186        // WebSocket handshake over the pre-connected stream.
187        let (ws, _) = tokio_tungstenite::client_async_with_config(
188            request,
189            MaybeTlsStream::Plain(tokio_stream),
190            Some(config),
191        )
192        .await?;
193
194        Ok(ws)
195    }
196}
197
198impl<T: EventMessage> Connection<T> {
199    fn next_call_id(&mut self) -> CallId {
200        let id = CallId::new(self.next_id);
201        self.next_id = self.next_id.wrapping_add(1);
202        id
203    }
204
205    /// Queue in the command to send over the socket and return the id for this
206    /// command
207    pub fn submit_command(
208        &mut self,
209        method: MethodId,
210        session_id: Option<SessionId>,
211        params: serde_json::Value,
212    ) -> serde_json::Result<CallId> {
213        let id = self.next_call_id();
214        let call = MethodCall {
215            id,
216            method,
217            session_id: session_id.map(Into::into),
218            params,
219        };
220        self.pending_commands.push_back(call);
221        Ok(id)
222    }
223
224    /// Buffer all queued commands into the WebSocket sink, then flush once.
225    ///
226    /// This batches multiple CDP commands into a single TCP write instead of
227    /// flushing after every individual message.
228    fn start_send_next(&mut self, cx: &mut Context<'_>) -> Result<()> {
229        // Complete any pending flush from a previous poll first.
230        if self.needs_flush {
231            match self.ws.poll_flush_unpin(cx) {
232                Poll::Ready(Ok(())) => self.needs_flush = false,
233                Poll::Ready(Err(e)) => return Err(e.into()),
234                Poll::Pending => return Ok(()),
235            }
236        }
237
238        // Buffer as many queued commands as the sink will accept.
239        let mut sent_any = false;
240        while !self.pending_commands.is_empty() {
241            match self.ws.poll_ready_unpin(cx) {
242                Poll::Ready(Ok(())) => {
243                    let Some(cmd) = self.pending_commands.pop_front() else {
244                        break;
245                    };
246                    tracing::trace!("Sending {:?}", cmd);
247                    let msg = serde_json::to_string(&cmd)?;
248                    self.ws.start_send_unpin(msg.into())?;
249                    sent_any = true;
250                }
251                _ => break,
252            }
253        }
254
255        // Flush the entire batch in one write.
256        if sent_any {
257            match self.ws.poll_flush_unpin(cx) {
258                Poll::Ready(Ok(())) => {}
259                Poll::Ready(Err(e)) => return Err(e.into()),
260                Poll::Pending => self.needs_flush = true,
261            }
262        }
263
264        Ok(())
265    }
266}
267
268/// Capacity of the bounded channel feeding the background WS writer task.
269/// Large enough that bursts of CDP commands never block the handler, small
270/// enough to apply back-pressure before memory grows without bound.
271const WS_CMD_CHANNEL_CAPACITY: usize = 2048;
272
273/// Capacity of the bounded channel from the background WS reader task to
274/// the Handler. Keeps decoded CDP messages buffered so the reader task
275/// can keep reading the socket while the Handler processes a backlog;
276/// applies TCP-level back-pressure on Chrome when the Handler is slow
277/// (the reader awaits channel capacity, stops draining the socket).
278const WS_READ_CHANNEL_CAPACITY: usize = 1024;
279
280/// Maximum number of in-flight decodes the reader pipeline holds at
281/// once. While any of these is still running on the blocking pool,
282/// the reader can keep draining the socket and starting new decodes,
283/// up to this cap. Applies per-connection; the resulting decoded
284/// messages are emitted to the Handler in strict WS arrival order
285/// via a `FuturesOrdered` queue — no behavior change versus the
286/// serial loop, just concurrent execution of independent decodes.
287const MAX_IN_FLIGHT_DECODES: usize = 32;
288
289/// Payload size at/above which `decode_message` runs via
290/// `tokio::task::spawn_blocking` instead of inline on the reader task.
291///
292/// `serde_json::from_slice` is CPU-bound with no `.await` points, so
293/// a multi-MB payload can occupy one tokio worker thread for tens of
294/// milliseconds. Offloading to the blocking thread pool above a
295/// threshold keeps the reader task cooperatively yielding — critical
296/// on single-threaded runtimes where the reader shares its worker
297/// with the Handler, user tasks, and timers.
298///
299/// The threshold is chosen so that typical CDP traffic (events,
300/// responses, small evaluates) stays on the inline fast path and
301/// doesn't pay the ~10-30 µs `spawn_blocking` hand-off cost, while
302/// screenshot payloads, wide network events, and huge console
303/// payloads take the offloaded path.
304const LARGE_FRAME_THRESHOLD: usize = 256 * 1024; // 256 KiB
305
306/// Split parts returned by [`Connection::into_async`].
307#[derive(Debug)]
308pub struct AsyncConnection<T: EventMessage> {
309    /// Receive half for decoded CDP messages. Backed by a bounded mpsc
310    /// fed by a dedicated background reader task — decode runs on that
311    /// task, never on the Handler task, so large CDP responses (multi-MB
312    /// screenshots, huge event payloads) cannot stall the Handler's
313    /// event loop.
314    pub reader: WsReader<T>,
315    /// Sender half for submitting outgoing CDP commands.
316    pub cmd_tx: mpsc::Sender<MethodCall>,
317    /// Handle to the background writer task.
318    pub writer_handle: tokio::task::JoinHandle<Result<()>>,
319    /// Handle to the background reader task (reads + decodes WS frames).
320    pub reader_handle: tokio::task::JoinHandle<()>,
321    /// Next command-call-id counter (continue numbering from where Connection left off).
322    pub next_id: usize,
323}
324
325impl<T: EventMessage + Unpin + Send + 'static> Connection<T> {
326    /// Consume the connection and split into a background reader + writer
327    /// pair, exposing the Handler-facing ends via `AsyncConnection`.
328    ///
329    /// Two `tokio::spawn`'d tasks are created:
330    ///
331    /// * `ws_write_loop` — batches outgoing commands and flushes them in
332    ///   one write per wakeup.
333    /// * `ws_read_loop`  — reads WS frames, decodes them to typed
334    ///   `Message<T>`, and forwards them via a bounded mpsc to the
335    ///   Handler. Ping/pong/malformed frames are skipped on this task
336    ///   and never reach the Handler. Large-message decode (SerDe CPU
337    ///   work) runs here, **not** on the Handler task, so the Handler's
338    ///   poll loop never stalls for tens of milliseconds on a 10 MB
339    ///   screenshot response.
340    ///
341    /// The design uses only `tokio::spawn` (cooperative async) — no
342    /// `spawn_blocking` or blocking thread-pool — so it scales with the
343    /// tokio runtime's worker threads on multi-threaded runtimes, and
344    /// interleaves cleanly with the Handler task on single-threaded
345    /// runtimes.
346    pub fn into_async(self) -> AsyncConnection<T> {
347        let (ws_sink, ws_stream) = self.ws.split();
348        let (cmd_tx, cmd_rx) = mpsc::channel(WS_CMD_CHANNEL_CAPACITY);
349        let (msg_tx, msg_rx) = mpsc::channel::<Result<Box<Message<T>>>>(WS_READ_CHANNEL_CAPACITY);
350
351        let writer_handle = tokio::spawn(ws_write_loop(ws_sink, cmd_rx));
352        let reader_handle = tokio::spawn(ws_read_loop::<T, _>(ws_stream, msg_tx));
353
354        let reader = WsReader {
355            rx: msg_rx,
356            _marker: PhantomData,
357        };
358
359        AsyncConnection {
360            reader,
361            cmd_tx,
362            writer_handle,
363            reader_handle,
364            next_id: self.next_id,
365        }
366    }
367}
368
369/// An entry in the reader's decode pipeline.
370///
371/// Small frames have been decoded inline on the reader task and sit
372/// in `Ready(Some(result))` waiting their turn to emit — zero
373/// allocation beyond the `Option`. Large frames were offloaded to
374/// `tokio::task::spawn_blocking`, so their entry is the
375/// corresponding `JoinHandle`.
376///
377/// A single concrete enum means `FuturesOrdered<InFlightDecode<T>>`
378/// can hold either kind without `Box<dyn Future>`, keeping the
379/// pipeline cost-proportional to the workload.
380enum InFlightDecode<T: EventMessage + Send + 'static> {
381    /// Small-frame fast path: already decoded inline. `take()`'d
382    /// exactly once when `FuturesOrdered` first polls it to Ready.
383    Ready(Option<Result<Box<Message<T>>>>),
384    /// Large-frame path: decoding on the blocking thread pool.
385    Blocking(tokio::task::JoinHandle<Result<Box<Message<T>>>>),
386}
387
388impl<T: EventMessage + Send + 'static> Future for InFlightDecode<T> {
389    type Output = Result<Box<Message<T>>>;
390
391    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
392        // Safety: both variants are structurally pin-agnostic —
393        // `Option<Result<..>>` is `Unpin`, and `tokio::task::JoinHandle`
394        // is documented as `Unpin`. So we can project out a `&mut`
395        // without unsafe.
396        match self.get_mut() {
397            InFlightDecode::Ready(slot) => Poll::Ready(
398                slot.take()
399                    .expect("InFlightDecode::Ready polled after completion"),
400            ),
401            InFlightDecode::Blocking(handle) => match Pin::new(handle).poll(cx) {
402                Poll::Ready(Ok(res)) => Poll::Ready(res),
403                Poll::Ready(Err(join_err)) => Poll::Ready(Err(CdpError::msg(format!(
404                    "WS decode blocking task join error: {join_err}"
405                )))),
406                Poll::Pending => Poll::Pending,
407            },
408        }
409    }
410}
411
412/// Emit a single decoded-frame result to the Handler, logging parse
413/// errors. Returns `true` if the channel is still open, `false` if
414/// the Handler has dropped the receiver (caller should exit).
415async fn emit_decoded<T>(
416    tx: &mpsc::Sender<Result<Box<Message<T>>>>,
417    res: Result<Box<Message<T>>>,
418) -> bool
419where
420    T: EventMessage + Send + 'static,
421{
422    match res {
423        Ok(msg) => tx.send(Ok(msg)).await.is_ok(),
424        Err(err) => {
425            tracing::debug!(
426                target: "chromiumoxide::conn::raw_ws::parse_errors",
427                "Dropping malformed WS frame: {err}",
428            );
429            true
430        }
431    }
432}
433
434/// Drain every in-flight decode in submit order, forwarding each
435/// result to the Handler. Used at shutdown paths (stream ended,
436/// `Close` frame, transport error) so messages that had already been
437/// pulled off the socket aren't silently lost.
438async fn drain_in_flight<T>(
439    in_flight: &mut FuturesOrdered<InFlightDecode<T>>,
440    tx: &mpsc::Sender<Result<Box<Message<T>>>>,
441) where
442    T: EventMessage + Send + 'static,
443{
444    while let Some(res) = in_flight.next().await {
445        if !emit_decoded(tx, res).await {
446            return;
447        }
448    }
449}
450
451/// Background task that reads frames from the WebSocket, decodes them to
452/// typed CDP `Message<T>`, and forwards them to the Handler over a
453/// bounded mpsc.
454///
455/// Runs on a `tokio::spawn`'d task. Small-to-medium frames are
456/// decoded inline (fast path); payloads at or above
457/// [`LARGE_FRAME_THRESHOLD`] are offloaded to `spawn_blocking` so
458/// multi-MB deserialization doesn't monopolise a tokio worker
459/// thread — especially important on single-threaded runtimes where
460/// the reader, Handler, and user tasks share the same worker.
461///
462/// Flow per frame:
463///
464/// * `Text` / `Binary` → [`decode_ws_frame`]; decoded `Ok(msg)` is
465///   sent to the Handler. Decode errors are logged and the frame is
466///   dropped (same behavior as the legacy inline decode path).
467/// * `Close` → loop exits cleanly, dropping `tx`. The Handler's
468///   `next_message().await` returns `None` on the next call.
469/// * `Ping` / `Pong` / unexpected frame types → skipped silently; they
470///   never cross the channel to the Handler.
471/// * Transport error → forwarded as `Err(CdpError::Ws(..))`, then the
472///   loop exits (the WS half is considered dead after an error).
473///
474/// Back-pressure: the outbound `tx` is bounded. If the Handler is busy
475/// and the channel fills, `tx.send(..).await` parks this task, which
476/// stops draining the WS socket. TCP flow control then applies
477/// back-pressure to Chrome instead of letting memory grow without bound.
478async fn ws_read_loop<T, S>(mut stream: S, tx: mpsc::Sender<Result<Box<Message<T>>>>)
479where
480    T: EventMessage + Send + 'static,
481    S: Stream<Item = std::result::Result<WsMessage, tokio_tungstenite::tungstenite::Error>>
482        + Unpin,
483{
484    // Pipeline of decodes in strict arrival order. Small-frame decodes
485    // are produced inline (zero allocation, borrowing the frame body);
486    // large-frame decodes are offloaded to `spawn_blocking`. Both
487    // variants share a single concrete `InFlightDecode<T>` so the
488    // queue avoids `Box<dyn Future>` overhead.
489    let mut in_flight: FuturesOrdered<InFlightDecode<T>> = FuturesOrdered::new();
490
491    loop {
492        tokio::select! {
493            // Bias: emit already-ready decodes before reading more
494            // frames. Keeps the pipeline small in the steady state
495            // while still allowing concurrency under burst.
496            biased;
497
498            // Emit the head of the pipeline as soon as it is ready.
499            // `FuturesOrdered::next` preserves submit order, so
500            // downstream delivery is byte-identical to the serial
501            // loop's ordering guarantee.
502            Some(res) = in_flight.next(), if !in_flight.is_empty() => {
503                if !emit_decoded(&tx, res).await {
504                    return;
505                }
506            }
507
508            // Read the next frame if the pipeline has capacity.
509            // Guard prevents unbounded memory growth when the
510            // Handler is slow and back-pressure propagates here
511            // from the head of `in_flight`.
512            maybe_frame = stream.next(), if in_flight.len() < MAX_IN_FLIGHT_DECODES => {
513                match maybe_frame {
514                    Some(Ok(WsMessage::Text(text))) => {
515                        // Zero-copy enqueue. The small-frame fast
516                        // path decodes inline *now* (borrowing
517                        // `text`, keeping the `raw_text_for_logging`
518                        // preview); the large-frame path moves the
519                        // `Utf8Bytes` (`Send + 'static`) directly
520                        // into `spawn_blocking` without an
521                        // intermediate allocation.
522                        if text.len() >= LARGE_FRAME_THRESHOLD {
523                            in_flight.push_back(InFlightDecode::Blocking(
524                                tokio::task::spawn_blocking(move || {
525                                    decode_message::<T>(text.as_bytes(), None)
526                                }),
527                            ));
528                        } else {
529                            let res = decode_message::<T>(text.as_bytes(), Some(&text));
530                            in_flight.push_back(InFlightDecode::Ready(Some(res)));
531                        }
532                    }
533                    Some(Ok(WsMessage::Binary(buf))) => {
534                        // Same shape as Text: move `Bytes`
535                        // (`Send + 'static`) into `spawn_blocking`
536                        // for large payloads, decode inline for
537                        // small ones.
538                        if buf.len() >= LARGE_FRAME_THRESHOLD {
539                            in_flight.push_back(InFlightDecode::Blocking(
540                                tokio::task::spawn_blocking(move || {
541                                    decode_message::<T>(&buf, None)
542                                }),
543                            ));
544                        } else {
545                            let res = decode_message::<T>(&buf, None);
546                            in_flight.push_back(InFlightDecode::Ready(Some(res)));
547                        }
548                    }
549                    Some(Ok(WsMessage::Close(_))) => {
550                        drain_in_flight(&mut in_flight, &tx).await;
551                        return;
552                    }
553                    Some(Ok(WsMessage::Ping(_))) | Some(Ok(WsMessage::Pong(_))) => {}
554                    Some(Ok(msg)) => {
555                        tracing::debug!(
556                            target: "chromiumoxide::conn::raw_ws::parse_errors",
557                            "Unexpected WS message type: {:?}",
558                            msg
559                        );
560                    }
561                    Some(Err(err)) => {
562                        // Preserve ordering: flush already-decoded
563                        // frames that arrived before the transport
564                        // error before surfacing the error itself.
565                        drain_in_flight(&mut in_flight, &tx).await;
566                        let _ = tx.send(Err(CdpError::Ws(err))).await;
567                        return;
568                    }
569                    None => {
570                        // Stream terminated (connection closed
571                        // without a `Close` frame). Drain and exit.
572                        drain_in_flight(&mut in_flight, &tx).await;
573                        return;
574                    }
575                }
576            }
577        }
578    }
579}
580
581/// Background task that batches and flushes outgoing CDP commands.
582async fn ws_write_loop(
583    mut sink: SplitSink<WebSocketStream<ConnectStream>, WsMessage>,
584    mut rx: mpsc::Receiver<MethodCall>,
585) -> Result<()> {
586    while let Some(call) = rx.recv().await {
587        let msg = crate::serde_json::to_string(&call)?;
588        sink.feed(WsMessage::Text(msg.into()))
589            .await
590            .map_err(CdpError::Ws)?;
591
592        // Batch: drain all buffered commands without waiting.
593        while let Ok(call) = rx.try_recv() {
594            let msg = crate::serde_json::to_string(&call)?;
595            sink.feed(WsMessage::Text(msg.into()))
596                .await
597                .map_err(CdpError::Ws)?;
598        }
599
600        // Flush the entire batch in one write.
601        sink.flush().await.map_err(CdpError::Ws)?;
602    }
603    Ok(())
604}
605
606/// Handler-facing read half of the split WebSocket connection.
607///
608/// Decoded CDP messages are produced by a dedicated background task
609/// (see [`ws_read_loop`]) and forwarded over a bounded mpsc. `WsReader`
610/// itself is a thin `Receiver` wrapper — calling `next_message()` does
611/// a single `rx.recv().await` with no per-message decoding work on the
612/// caller's task. This keeps the Handler's poll loop free of CPU-bound
613/// deserialize time, which matters for large (multi-MB) CDP responses
614/// such as screenshots and wide-header network events.
615#[derive(Debug)]
616pub struct WsReader<T: EventMessage> {
617    rx: mpsc::Receiver<Result<Box<Message<T>>>>,
618    _marker: PhantomData<T>,
619}
620
621impl<T: EventMessage + Unpin> WsReader<T> {
622    /// Read the next CDP message from the WebSocket.
623    ///
624    /// Returns `None` when the background reader task has exited
625    /// (connection closed or sender dropped). This call does only a
626    /// channel `recv` — the actual WS read + JSON decode happens on
627    /// the background `ws_read_loop` task.
628    pub async fn next_message(&mut self) -> Option<Result<Box<Message<T>>>> {
629        self.rx.recv().await
630    }
631}
632
633impl<T: EventMessage + Unpin> Stream for Connection<T> {
634    type Item = Result<Box<Message<T>>>;
635
636    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
637        let pin = self.get_mut();
638
639        // Send and flush outgoing messages
640        if let Err(err) = pin.start_send_next(cx) {
641            return Poll::Ready(Some(Err(err)));
642        }
643
644        // Read from the websocket, skipping non-data frames (pings,
645        // pongs, malformed messages) without yielding back to the
646        // executor.  This avoids a full round-trip per skipped frame.
647        //
648        // Cap consecutive skips so a flood of non-data frames (many
649        // pings, malformed/unexpected types) cannot starve the
650        // runtime — yield Pending after `MAX_SKIPS_PER_POLL` and
651        // self-wake so we resume on the next tick.
652        const MAX_SKIPS_PER_POLL: u32 = 16;
653        let mut skips: u32 = 0;
654        loop {
655            match ready!(pin.ws.poll_next_unpin(cx)) {
656                Some(Ok(WsMessage::Text(text))) => {
657                    match decode_message::<T>(text.as_bytes(), Some(&text)) {
658                        Ok(msg) => return Poll::Ready(Some(Ok(msg))),
659                        Err(err) => {
660                            tracing::debug!(
661                                target: "chromiumoxide::conn::raw_ws::parse_errors",
662                                "Dropping malformed text WS frame: {err}",
663                            );
664                            skips += 1;
665                        }
666                    }
667                }
668                Some(Ok(WsMessage::Binary(buf))) => match decode_message::<T>(&buf, None) {
669                    Ok(msg) => return Poll::Ready(Some(Ok(msg))),
670                    Err(err) => {
671                        tracing::debug!(
672                            target: "chromiumoxide::conn::raw_ws::parse_errors",
673                            "Dropping malformed binary WS frame: {err}",
674                        );
675                        skips += 1;
676                    }
677                },
678                Some(Ok(WsMessage::Close(_))) => return Poll::Ready(None),
679                Some(Ok(WsMessage::Ping(_))) | Some(Ok(WsMessage::Pong(_))) => {
680                    skips += 1;
681                }
682                Some(Ok(msg)) => {
683                    tracing::debug!(
684                        target: "chromiumoxide::conn::raw_ws::parse_errors",
685                        "Unexpected WS message type: {:?}",
686                        msg
687                    );
688                    skips += 1;
689                }
690                Some(Err(err)) => return Poll::Ready(Some(Err(CdpError::Ws(err)))),
691                None => return Poll::Ready(None),
692            }
693
694            if skips >= MAX_SKIPS_PER_POLL {
695                cx.waker().wake_by_ref();
696                return Poll::Pending;
697            }
698        }
699    }
700}
701
702/// Shared decode path for both text and binary WS frames.
703/// `raw_text_for_logging` is only provided for textual frames so we can log the original
704/// payload on parse failure if desired.
705#[cfg(not(feature = "serde_stacker"))]
706fn decode_message<T: EventMessage>(
707    bytes: &[u8],
708    raw_text_for_logging: Option<&str>,
709) -> Result<Box<Message<T>>> {
710    match serde_json::from_slice::<Box<Message<T>>>(bytes) {
711        Ok(msg) => {
712            tracing::trace!("Received {:?}", msg);
713            Ok(msg)
714        }
715        Err(err) => {
716            if let Some(txt) = raw_text_for_logging {
717                let preview = &txt[..txt.len().min(512)];
718                tracing::debug!(
719                    target: "chromiumoxide::conn::raw_ws::parse_errors",
720                    msg_len = txt.len(),
721                    "Skipping unrecognized WS message {err} preview={preview}",
722                );
723            } else {
724                tracing::debug!(
725                    target: "chromiumoxide::conn::raw_ws::parse_errors",
726                    "Skipping unrecognized binary WS message {err}",
727                );
728            }
729            Err(err.into())
730        }
731    }
732}
733
734/// Shared decode path for both text and binary WS frames.
735/// `raw_text_for_logging` is only provided for textual frames so we can log the original
736/// payload on parse failure if desired.
737#[cfg(feature = "serde_stacker")]
738fn decode_message<T: EventMessage>(
739    bytes: &[u8],
740    raw_text_for_logging: Option<&str>,
741) -> Result<Box<Message<T>>> {
742    use serde::Deserialize;
743    let mut de = serde_json::Deserializer::from_slice(bytes);
744
745    de.disable_recursion_limit();
746
747    let de = serde_stacker::Deserializer::new(&mut de);
748
749    match Box::<Message<T>>::deserialize(de) {
750        Ok(msg) => {
751            tracing::trace!("Received {:?}", msg);
752            Ok(msg)
753        }
754        Err(err) => {
755            if let Some(txt) = raw_text_for_logging {
756                let preview = &txt[..txt.len().min(512)];
757                tracing::debug!(
758                    target: "chromiumoxide::conn::raw_ws::parse_errors",
759                    msg_len = txt.len(),
760                    "Skipping unrecognized WS message {err} preview={preview}",
761                );
762            } else {
763                tracing::debug!(
764                    target: "chromiumoxide::conn::raw_ws::parse_errors",
765                    "Skipping unrecognized binary WS message {err}",
766                );
767            }
768            Err(err.into())
769        }
770    }
771}
772
773#[cfg(test)]
774mod ws_read_loop_tests {
775    //! Unit tests for the `ws_read_loop` background reader task.
776    //!
777    //! These tests feed a synthetic `Stream<Item = Result<WsMessage, _>>`
778    //! into `ws_read_loop` — no real WebSocket, no Chrome — and observe
779    //! what comes out the other side of the mpsc channel.
780    //!
781    //! The properties under test are the ones that make the reader-task
782    //! decoupling safe: FIFO ordering, no-deadlock on a bounded channel
783    //! under back-pressure, silent drop of non-data frames, graceful
784    //! transport-error propagation, and clean exit on `Close`.
785    //!
786    //! The typed events are `chromiumoxide_cdp::cdp::CdpEventMessage` —
787    //! the same instantiation the real Handler uses — so these tests
788    //! exercise the actual decode path (`serde_json::from_slice`), not
789    //! a simplified fake.
790    use super::*;
791    use chromiumoxide_cdp::cdp::CdpEventMessage;
792    use chromiumoxide_types::CallId;
793    use futures_util::stream;
794    use tokio::sync::mpsc;
795    use tokio_tungstenite::tungstenite::Message as WsMessage;
796
797    /// Build a CDP `Response` WS frame as text — the smallest valid CDP
798    /// message. `id` tags the frame for ordering assertions.
799    fn response_frame(id: u64) -> WsMessage {
800        WsMessage::Text(
801            format!(r#"{{"id":{id},"result":{{"ok":true}}}}"#)
802                .to_string()
803                .into(),
804        )
805    }
806
807    /// Build a frame far larger than a typical socket chunk, to exercise
808    /// the "large message" path that motivated this refactor. The blob
809    /// field pushes serde_json through a big allocation even though the
810    /// envelope is tiny.
811    fn large_response_frame(id: u64, blob_bytes: usize) -> WsMessage {
812        let blob = "x".repeat(blob_bytes);
813        WsMessage::Text(
814            format!(r#"{{"id":{id},"result":{{"blob":"{blob}"}}}}"#)
815                .to_string()
816                .into(),
817        )
818    }
819
820    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
821    async fn forwards_messages_in_stream_order() {
822        let frames = vec![
823            Ok(response_frame(1)),
824            Ok(response_frame(2)),
825            Ok(response_frame(3)),
826        ];
827        let stream = stream::iter(frames);
828        let (tx, mut rx) = mpsc::channel::<Result<Box<Message<CdpEventMessage>>>>(8);
829        let task = tokio::spawn(ws_read_loop::<CdpEventMessage, _>(stream, tx));
830
831        for expected in [1u64, 2, 3] {
832            let msg = rx.recv().await.expect("msg").expect("decode ok");
833            if let Message::Response(resp) = *msg {
834                assert_eq!(resp.id, CallId::new(expected as usize));
835            } else {
836                panic!("expected Response");
837            }
838        }
839        assert!(rx.recv().await.is_none(), "channel must close on EOF");
840        task.await.expect("reader task join");
841    }
842
843    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
844    async fn pings_and_pongs_never_reach_the_handler() {
845        let frames = vec![
846            Ok(WsMessage::Ping(vec![1, 2, 3].into())),
847            Ok(response_frame(7)),
848            Ok(WsMessage::Pong(vec![].into())),
849            Ok(response_frame(8)),
850        ];
851        let stream = stream::iter(frames);
852        let (tx, mut rx) = mpsc::channel::<Result<Box<Message<CdpEventMessage>>>>(8);
853        let task = tokio::spawn(ws_read_loop::<CdpEventMessage, _>(stream, tx));
854
855        for expected in [7u64, 8] {
856            let msg = rx.recv().await.expect("msg").expect("decode ok");
857            if let Message::Response(resp) = *msg {
858                assert_eq!(resp.id, CallId::new(expected as usize));
859            }
860        }
861        assert!(rx.recv().await.is_none());
862        task.await.expect("reader task join");
863    }
864
865    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
866    async fn malformed_frames_do_not_block_subsequent_valid_frames() {
867        let frames = vec![
868            Ok(WsMessage::Text("{not valid json".to_string().into())),
869            Ok(response_frame(42)),
870        ];
871        let stream = stream::iter(frames);
872        let (tx, mut rx) = mpsc::channel::<Result<Box<Message<CdpEventMessage>>>>(8);
873        let task = tokio::spawn(ws_read_loop::<CdpEventMessage, _>(stream, tx));
874
875        let msg = rx.recv().await.expect("msg").expect("decode ok");
876        if let Message::Response(resp) = *msg {
877            assert_eq!(resp.id, CallId::new(42));
878        }
879        assert!(rx.recv().await.is_none());
880        task.await.expect("reader task join");
881    }
882
883    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
884    async fn close_frame_terminates_the_reader() {
885        let frames = vec![
886            Ok(response_frame(1)),
887            Ok(WsMessage::Close(None)),
888            Ok(response_frame(2)), // unreachable after Close
889        ];
890        let stream = stream::iter(frames);
891        let (tx, mut rx) = mpsc::channel::<Result<Box<Message<CdpEventMessage>>>>(8);
892        let task = tokio::spawn(ws_read_loop::<CdpEventMessage, _>(stream, tx));
893
894        let msg = rx.recv().await.expect("msg").expect("decode ok");
895        if let Message::Response(resp) = *msg {
896            assert_eq!(resp.id, CallId::new(1));
897        }
898        assert!(
899            rx.recv().await.is_none(),
900            "reader must exit on Close; frames after Close must not appear"
901        );
902        task.await.expect("reader task join");
903    }
904
905    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
906    async fn transport_error_is_forwarded_once_then_reader_exits() {
907        let frames = vec![
908            Ok(response_frame(1)),
909            Err(tokio_tungstenite::tungstenite::Error::ConnectionClosed),
910            Ok(response_frame(2)),
911        ];
912        let stream = stream::iter(frames);
913        let (tx, mut rx) = mpsc::channel::<Result<Box<Message<CdpEventMessage>>>>(8);
914        let task = tokio::spawn(ws_read_loop::<CdpEventMessage, _>(stream, tx));
915
916        let msg = rx.recv().await.expect("msg").expect("ok");
917        assert!(matches!(*msg, Message::Response(_)));
918        match rx.recv().await {
919            Some(Err(CdpError::Ws(_))) => {}
920            other => panic!("expected forwarded Ws error, got {other:?}"),
921        }
922        assert!(rx.recv().await.is_none());
923        task.await.expect("reader task join");
924    }
925
926    /// Back-pressure property: with the smallest possible channel and
927    /// many frames, the reader task awaits capacity after each send and
928    /// never deadlocks. This is the core "no deadlock" proof for the
929    /// new design — if the reader held anything across its `.await` that
930    /// the consumer needed, the consumer's `recv().await` would block
931    /// forever. Completion under a 5s watchdog proves it doesn't.
932    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
933    async fn bounded_channel_does_not_deadlock_under_backpressure() {
934        const N: u64 = 512;
935        let frames: Vec<_> = (1..=N).map(|id| Ok(response_frame(id))).collect();
936        let stream = stream::iter(frames);
937
938        let (tx, mut rx) = mpsc::channel::<Result<Box<Message<CdpEventMessage>>>>(1);
939        let task = tokio::spawn(ws_read_loop::<CdpEventMessage, _>(stream, tx));
940
941        let deadline = std::time::Duration::from_secs(5);
942        let collected = tokio::time::timeout(deadline, async {
943            let mut seen = 0u64;
944            while let Some(frame) = rx.recv().await {
945                let msg = frame.expect("decode ok");
946                if let Message::Response(resp) = *msg {
947                    seen += 1;
948                    assert_eq!(
949                        resp.id,
950                        CallId::new(seen as usize),
951                        "back-pressure must preserve FIFO order"
952                    );
953                }
954            }
955            seen
956        })
957        .await
958        .expect("reader must make forward progress despite cap-1 back-pressure");
959
960        assert_eq!(collected, N, "all frames must arrive");
961        task.await.expect("reader task join");
962    }
963
964    /// Large message (>1 MB) is decoded correctly on the background
965    /// task. This is the specific scenario the reader-task refactor
966    /// was built for — we don't measure time here (benches cover that),
967    /// we just prove the end-to-end path works without corruption or
968    /// deadlock.
969    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
970    async fn large_message_decodes_without_corruption() {
971        let big = 2 * 1024 * 1024; // 2 MB payload
972        let frames = vec![
973            Ok(large_response_frame(100, big)),
974            Ok(response_frame(101)),
975        ];
976        let stream = stream::iter(frames);
977        let (tx, mut rx) = mpsc::channel::<Result<Box<Message<CdpEventMessage>>>>(4);
978        let task = tokio::spawn(ws_read_loop::<CdpEventMessage, _>(stream, tx));
979
980        let first = rx.recv().await.expect("msg").expect("ok");
981        if let Message::Response(resp) = *first {
982            assert_eq!(resp.id, CallId::new(100));
983        }
984        let second = rx.recv().await.expect("msg").expect("ok");
985        if let Message::Response(resp) = *second {
986            assert_eq!(resp.id, CallId::new(101));
987        }
988        assert!(rx.recv().await.is_none());
989        task.await.expect("reader task join");
990    }
991
992    /// FIFO ordering under the pipelined reader when large-frame
993    /// decodes run in parallel via `spawn_blocking`.
994    ///
995    /// This test submits an interleaved sequence of large and small
996    /// frames. Large frames take the `spawn_blocking` path (decode
997    /// on the blocking pool, variable completion order); small
998    /// frames take the inline path (decode immediately). The
999    /// pipeline's `FuturesOrdered` queue must emit them to the
1000    /// Handler in strict arrival order regardless of which
1001    /// blocking-pool thread finishes first.
1002    ///
1003    /// If the ordering guarantee were ever broken — e.g. by
1004    /// accidentally swapping `FuturesOrdered` for `FuturesUnordered`
1005    /// — id sequence checks here would catch it immediately.
1006    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
1007    async fn pipelined_large_and_small_frames_keep_fifo_order() {
1008        let big = 2 * 1024 * 1024; // 2 MB payload — forces spawn_blocking
1009        let frames = vec![
1010            Ok(large_response_frame(1, big)),
1011            Ok(response_frame(2)),
1012            Ok(response_frame(3)),
1013            Ok(large_response_frame(4, big)),
1014            Ok(response_frame(5)),
1015            Ok(large_response_frame(6, big)),
1016            Ok(response_frame(7)),
1017            Ok(response_frame(8)),
1018        ];
1019        let expected: Vec<usize> = (1..=8).collect();
1020
1021        let stream = stream::iter(frames);
1022        let (tx, mut rx) = mpsc::channel::<Result<Box<Message<CdpEventMessage>>>>(16);
1023        let task = tokio::spawn(ws_read_loop::<CdpEventMessage, _>(stream, tx));
1024
1025        let deadline = std::time::Duration::from_secs(10);
1026        let observed = tokio::time::timeout(deadline, async {
1027            let mut ids = Vec::with_capacity(expected.len());
1028            while let Some(frame) = rx.recv().await {
1029                let msg = frame.expect("decode ok");
1030                if let Message::Response(resp) = *msg {
1031                    ids.push(CallId::new(ids.len() + 1));
1032                    assert_eq!(
1033                        resp.id,
1034                        *ids.last().unwrap(),
1035                        "pipelined reader must emit frames in strict arrival order \
1036                         regardless of per-frame decode latency"
1037                    );
1038                }
1039            }
1040            ids
1041        })
1042        .await
1043        .expect("pipelined reader should make forward progress within 10s");
1044
1045        assert_eq!(
1046            observed.len(),
1047            expected.len(),
1048            "all {} frames must reach the Handler",
1049            expected.len()
1050        );
1051        task.await.expect("reader task join");
1052    }
1053}