Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(curl): drive HTTP and WebSocket on one owner

ldm0150c01436a9a
diff --git a/Cargo.lock b/Cargo.lock
index cd6c416b6..f9761ab17 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2467,6 +2467,7 @@ dependencies = [
"anyhow",
"crossbeam-channel",
"curl",
+ "curl-sys",
"moli-dns-resolver",
"openssl-sys",
"parking_lot",
diff --git a/moli-curl/Cargo.toml b/moli-curl/Cargo.toml
index 4bed4413c..dbb9b585e 100644
--- a/moli-curl/Cargo.toml
+++ b/moli-curl/Cargo.toml
@@ -8,6 +8,7 @@ edition = "2024"
anyhow = "1.0.100"
crossbeam-channel = "0.5.15"
curl = { version = "0.4.49", default-features = false, features = ["brotli", "http2", "poll_7_68_0", "ssl", "static-curl", "websocket"] }
+curl-sys = { version = "0.4.87", default-features = false }
moli-dns-resolver = { path = "../moli-dns-resolver" }
openssl-sys = { version = "0.9", features = ["aws-lc"] }
parking_lot = "0.12"
diff --git a/moli-curl/src/runtime.rs b/moli-curl/src/runtime.rs
index 8db5a4bb5..bf94dd83a 100644
--- a/moli-curl/src/runtime.rs
+++ b/moli-curl/src/runtime.rs
@@ -22,6 +22,7 @@ use curl::{
use parking_lot::Mutex;
use crate::dns_adapter::CurlDnsResolution;
+use crate::websocket::CurlWebSocketConnector;
pub use config::CurlMultiRuntimeConfig;
pub use identity::CurlTransferId;
@@ -124,6 +125,7 @@ struct CurlMultiRuntimeInner<H: Handler + Send + 'static, C: Send + 'static> {
command_tx: Sender<CurlRuntimeCommand<H, C>>,
owner_waker: MultiWaker,
shutdown_requested: Arc<AtomicBool>,
+ websocket_connector: CurlWebSocketConnector,
#[cfg(test)]
owner_started: Arc<AtomicBool>,
owner_handle: Mutex<Option<thread::JoinHandle<()>>>,
@@ -137,6 +139,7 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlMultiRuntime<H, C> {
let (command_tx, command_rx) = crossbeam_channel::unbounded();
let (completion_tx, completion_rx) = crossbeam_channel::unbounded();
let (waker_tx, waker_rx) = crossbeam_channel::bounded(1);
+ let (websocket_tx, websocket_rx) = CurlWebSocketConnector::channel();
let shutdown_requested = Arc::new(AtomicBool::new(false));
#[cfg(test)]
let owner_started = Arc::new(AtomicBool::new(false));
@@ -146,6 +149,7 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlMultiRuntime<H, C> {
completion_tx,
waker_tx,
Arc::clone(&shutdown_requested),
+ websocket_rx,
#[cfg(test)]
Arc::clone(&owner_started),
);
@@ -160,6 +164,11 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlMultiRuntime<H, C> {
let runtime = Self {
inner: Arc::new(CurlMultiRuntimeInner {
command_tx,
+ websocket_connector: CurlWebSocketConnector::new(
+ websocket_tx,
+ owner_waker.clone(),
+ shutdown_requested.clone(),
+ ),
owner_waker,
shutdown_requested,
#[cfg(test)]
@@ -209,6 +218,12 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlMultiRuntime<H, C> {
self.inner.shutdown();
}
+ /// Creates WebSockets on this runtime's owner and Multi. The capability
+ /// cannot keep the runtime alive or shut down unrelated HTTP transfers.
+ pub fn websocket_connector(&self) -> CurlWebSocketConnector {
+ self.inner.websocket_connector.clone()
+ }
+
#[cfg(test)]
pub fn owner_count_for_testing(&self) -> usize {
usize::from(self.inner.owner_started.load(Ordering::SeqCst))
diff --git a/moli-curl/src/runtime/owner.rs b/moli-curl/src/runtime/owner.rs
index e51a1427a..f2c249126 100644
--- a/moli-curl/src/runtime/owner.rs
+++ b/moli-curl/src/runtime/owner.rs
@@ -16,14 +16,15 @@ use curl::{
use tracing::debug;
use crate::dns_adapter::{CurlDnsOwnerCompletion, CurlDnsReady};
+use crate::websocket::{Submission, registry::WebSocketRegistry};
use super::{
CurlMultiCompletion, CurlMultiJob, CurlMultiRuntimeConfig, CurlTransferId,
config::{make_runtime_multi, runtime_wait_timeout},
residence::{
CurlActiveTransfer, CurlOwnerState, CurlPendingJob, active_origin_count,
- completed_transfers, enqueue_existing_pending_job, enqueue_pending_job, job_is_eligible,
- pending_origin_count, take_expired_pending_jobs, take_transfers_in_notification_order,
+ enqueue_existing_pending_job, enqueue_pending_job, job_is_eligible, pending_origin_count,
+ take_expired_pending_jobs, take_transfers_in_notification_order,
},
};
@@ -39,6 +40,7 @@ pub(super) enum CurlRuntimeCommand<H: Handler, C> {
enum CurlOwnerEvent<H: Handler, C> {
Command(std::result::Result<CurlRuntimeCommand<H, C>, crossbeam_channel::RecvError>),
Dns(std::result::Result<CurlDnsOwnerCompletion<CurlTransferId>, crossbeam_channel::RecvError>),
+ WebSocket(std::result::Result<Submission, crossbeam_channel::RecvError>),
Deadline,
}
@@ -48,6 +50,7 @@ pub(super) struct CurlRuntimeOwner<H: Handler + Send + 'static, C: Send + 'stati
completion_tx: Sender<CurlMultiCompletion<H, C>>,
waker_tx: Sender<MultiWaker>,
shutdown_requested: Arc<AtomicBool>,
+ websocket_rx: Receiver<Submission>,
#[cfg(test)]
owner_started: Arc<AtomicBool>,
}
@@ -59,6 +62,7 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlRuntimeOwner<H, C> {
completion_tx: Sender<CurlMultiCompletion<H, C>>,
waker_tx: Sender<MultiWaker>,
shutdown_requested: Arc<AtomicBool>,
+ websocket_rx: Receiver<Submission>,
#[cfg(test)] owner_started: Arc<AtomicBool>,
) -> Self {
Self {
@@ -67,6 +71,7 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlRuntimeOwner<H, C> {
completion_tx,
waker_tx,
shutdown_requested,
+ websocket_rx,
#[cfg(test)]
owner_started,
}
@@ -82,32 +87,44 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlRuntimeOwner<H, C> {
let mut multi = make_runtime_multi(&self.config);
let _ = self.waker_tx.send(multi.waker());
let mut state = CurlOwnerState::default();
+ let mut websockets = WebSocketRegistry::new(self.websocket_rx.clone());
loop {
self.drain_commands(&mut state, &mut multi);
+ if state.closed {
+ websockets.shutdown(&mut multi);
+ }
self.drain_dns_completions(&mut state);
self.expire_waiting_jobs(&mut state);
self.start_eligible_jobs(&mut state, &mut multi);
- self.process_completed_transfers(&mut state, &mut multi);
+ self.process_completed_transfers(&mut state, &mut multi, &mut websockets);
+ let progressed = websockets.advance(&mut multi);
if state.closed
&& state.pending.is_empty()
&& state.dns.is_empty()
&& state.active.is_empty()
+ && self.command_rx.is_empty()
{
return;
}
- if state.active.is_empty() && state.pending.is_empty() {
- self.wait_for_next_owner_event(&mut state, &mut multi);
- } else if !state.active.is_empty() {
- self.wait_for_curl_progress(&multi, &state);
+ let runnable = progressed || !self.command_rx.is_empty();
+ if !runnable
+ && state.active.is_empty()
+ && state.pending.is_empty()
+ && websockets.is_empty()
+ {
+ self.wait_for_next_owner_event(&mut state, &mut multi, &mut websockets);
+ } else {
+ self.wait_for_curl_progress(&mut multi, &state, &mut websockets, runnable);
}
}
}
fn drain_commands(&self, state: &mut CurlOwnerState<H, C>, multi: &mut Multi) {
- loop {
+ // Continuous HTTP submissions must not starve native I/O or WebSockets.
+ for _ in 0..256 {
match self.command_rx.try_recv() {
Ok(command) => self.handle_command(state, multi, command),
Err(crossbeam_channel::TryRecvError::Empty) => break,
@@ -119,33 +136,35 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlRuntimeOwner<H, C> {
}
}
- fn wait_for_next_owner_event(&self, state: &mut CurlOwnerState<H, C>, multi: &mut Multi) {
+ fn wait_for_next_owner_event(
+ &self,
+ state: &mut CurlOwnerState<H, C>,
+ multi: &mut Multi,
+ websockets: &mut WebSocketRegistry,
+ ) {
if state.closed {
return;
}
- if state.dns.is_empty() {
- match self.command_rx.recv() {
- Ok(command) => self.handle_command(state, multi, command),
- Err(_) => self.close(state, multi),
- }
- return;
- }
let event = if let Some(deadline) = state.dns.next_deadline(|pending| pending.job.deadline)
{
let deadline_rx =
crossbeam_channel::after(deadline.saturating_duration_since(Instant::now()));
crossbeam_channel::select! {
recv(self.command_rx) -> command => CurlOwnerEvent::Command(command),
+ recv(websockets.submissions()) -> submission => CurlOwnerEvent::WebSocket(submission),
recv(state.dns.completion_receiver()) -> completion => CurlOwnerEvent::Dns(completion),
recv(deadline_rx) -> _ => CurlOwnerEvent::Deadline,
}
} else {
crossbeam_channel::select! {
recv(self.command_rx) -> command => CurlOwnerEvent::Command(command),
+ recv(websockets.submissions()) -> submission => CurlOwnerEvent::WebSocket(submission),
recv(state.dns.completion_receiver()) -> completion => CurlOwnerEvent::Dns(completion),
}
};
match event {
+ CurlOwnerEvent::WebSocket(Ok(submission)) => websockets.admit(submission, multi),
+ CurlOwnerEvent::WebSocket(Err(_)) => self.close(state, multi),
CurlOwnerEvent::Command(command) => match command {
Ok(command) => self.handle_command(state, multi, command),
Err(_) => self.close(state, multi),
@@ -448,11 +467,37 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlRuntimeOwner<H, C> {
}
}
- fn process_completed_transfers(&self, state: &mut CurlOwnerState<H, C>, multi: &mut Multi) {
+ fn process_completed_transfers(
+ &self,
+ state: &mut CurlOwnerState<H, C>,
+ multi: &mut Multi,
+ websockets: &mut WebSocketRegistry,
+ ) {
if let Err(error) = multi.perform() {
debug!("curl multi runtime perform failed: {error}");
+ websockets.fail_sessions(multi, &error.to_string());
+ }
+ // Drain CURLMSG_DONE once. A WebSocket DONE starts its open residence;
+ // HTTP DONE removes a finished transfer, preserving notification order.
+ let mut completed = Vec::new();
+ multi.messages(|message| {
+ let Some(id) = message.token().ok().and_then(CurlTransferId::from_token) else {
+ return;
+ };
+ // Keep the easy handle's error buffer as well as the CURLcode.
+ let result = match state.active.get(&id) {
+ Some(transfer) => message.result_for2(&transfer.handle),
+ None => websockets.handshake_result(id, &message),
+ };
+ if let Some(result) = result {
+ completed.push((id, result));
+ }
+ });
+ for (id, result) in &completed {
+ if !state.active.contains_key(id) {
+ websockets.complete_handshake(*id, result.clone(), multi);
+ }
}
- let completed = completed_transfers(multi, &state.active);
for (transfer_id, active, result) in
take_transfers_in_notification_order(&mut state.active, completed)
{
@@ -581,18 +626,31 @@ impl<H: Handler + Send + 'static, C: Send + 'static> CurlRuntimeOwner<H, C> {
});
}
- fn wait_for_curl_progress(&self, multi: &Multi, state: &CurlOwnerState<H, C>) {
- let mut wait_timeout = runtime_wait_timeout(multi, self.config.poll_interval)
- .unwrap_or(self.config.poll_interval);
+ fn wait_for_curl_progress(
+ &self,
+ multi: &mut Multi,
+ state: &CurlOwnerState<H, C>,
+ websockets: &mut WebSocketRegistry,
+ progressed: bool,
+ ) {
+ // HTTP cancellation uses its configured progress interval. Idle WS
+ // sessions use socket/waker readiness and need not inherit that cadence.
+ let interval = if state.active.is_empty() {
+ Duration::from_secs(1)
+ } else {
+ self.config.poll_interval
+ };
+ let mut wait_timeout = runtime_wait_timeout(multi, interval).unwrap_or(interval);
if let Some(deadline) = state.next_waiting_deadline() {
wait_timeout = wait_timeout.min(deadline.saturating_duration_since(Instant::now()));
}
- if wait_timeout.is_zero() {
- return;
+ if let Some(deadline) = websockets.next_deadline() {
+ wait_timeout = wait_timeout.min(deadline.saturating_duration_since(Instant::now()));
}
- if let Err(error) = multi.poll(&mut [], wait_timeout) {
- debug!("curl multi runtime poll failed: {error}");
+ if progressed {
+ wait_timeout = Duration::ZERO;
}
+ websockets.wait(multi, wait_timeout, progressed);
}
fn send_completion(&self, completion: CurlMultiCompletion<H, C>) {
diff --git a/moli-curl/src/runtime/residence.rs b/moli-curl/src/runtime/residence.rs
index f4651cb9f..850448649 100644
--- a/moli-curl/src/runtime/residence.rs
+++ b/moli-curl/src/runtime/residence.rs
@@ -5,11 +5,7 @@ use std::{
time::{Duration, Instant},
};
-use curl::{
- easy::Handler,
- multi::{Easy2Handle, Multi},
-};
-use tracing::debug;
+use curl::{easy::Handler, multi::Easy2Handle};
use crate::dns_adapter::CurlDnsOwnerResidence;
@@ -142,31 +138,6 @@ pub(super) fn pending_origin_count<H: Handler, C>(
.count()
}
-pub(super) fn completed_transfers<H: Handler, C>(
- multi: &Multi,
- active: &HashMap<CurlTransferId, CurlActiveTransfer<H, C>>,
-) -> Vec<(CurlTransferId, std::result::Result<(), curl::Error>)> {
- let mut completed = Vec::new();
- multi.messages(|message| {
- let Ok(token) = message.token() else {
- debug!("ignored curl completion whose private token could not be read");
- return;
- };
- let Some(transfer_id) = CurlTransferId::from_token(token) else {
- debug!("ignored curl completion with an empty private token");
- return;
- };
- let Some(transfer) = active.get(&transfer_id) else {
- debug!(%transfer_id, "ignored stale curl completion");
- return;
- };
- if let Some(result) = message.result_for2(&transfer.handle) {
- completed.push((transfer_id, result));
- }
- });
- completed
-}
-
/// Removes exact active transfers while preserving libcurl's notification
/// order. Unknown IDs are stale terminals for already-retired residences and
/// cannot recover or disturb a newer transfer.
diff --git a/moli-curl/src/websocket.rs b/moli-curl/src/websocket.rs
index cadb5406c..5d3764399 100644
--- a/moli-curl/src/websocket.rs
+++ b/moli-curl/src/websocket.rs
@@ -4,14 +4,15 @@
//! close-handshake semantics belong to the caller. Dropping the receiver cancels
//! its session, including DNS and handshake work, independently of queue capacity.
//!
-//! Internally, owner coordinates DNS and scheduling; session owns the native
+//! Internally, the shared runtime drives DNS and scheduling; session owns the native
//! handle and its Opening/Open/ReceivedClose lifecycle; scheduling holds I/O
//! admission state and maps polled sockets back to their sessions. Returning
//! AGAIN parks that I/O until its socket is signalled. Application wakeups only
//! resume paused work, such as a new frame or restored receive capacity.
+mod connection_pool;
mod diagnostics;
-mod owner;
+pub(crate) mod registry;
mod request;
mod scheduling;
mod session;
@@ -23,7 +24,6 @@ use std::{
Arc,
atomic::{AtomicBool, Ordering},
},
- thread,
time::Duration,
};
@@ -33,7 +33,8 @@ use parking_lot::Mutex;
use tokio::sync::{OwnedSemaphorePermit, Semaphore, mpsc, oneshot};
use crate::{
- CurlDnsResolution, CurlTlsConfig, CurlTransferId, runtime::identity::next_transfer_id,
+ CurlDnsResolution, CurlMultiRuntime, CurlMultiRuntimeConfig, CurlTlsConfig, CurlTransferId,
+ runtime::identity::next_transfer_id,
};
pub use curl::easy::{WsFlags, WsFrame};
@@ -138,6 +139,8 @@ struct Control {
receive_allocations: std::sync::atomic::AtomicUsize,
#[cfg(test)]
read_waiting: tokio::sync::Notify,
+ #[cfg(test)]
+ owner_thread: Mutex<Option<std::thread::ThreadId>>,
}
impl Control {
@@ -277,48 +280,48 @@ impl Drop for SessionIo {
}
}
-struct Submission {
+pub(crate) struct Submission {
id: CurlTransferId,
request: CurlWebSocketRequest,
io: SessionIo,
}
+/// Admission capability for one native owner. Clones do not keep that owner
+/// alive; shutting down its runtime closes connections and rejects new ones.
#[derive(Clone, Debug)]
-pub struct CurlWebSocketRuntime {
- inner: Arc<RuntimeInner>,
+pub struct CurlWebSocketConnector {
+ inner: Arc<ConnectorInner>,
}
#[derive(Debug)]
-struct RuntimeInner {
+struct ConnectorInner {
submissions: crossbeam_channel::Sender<Submission>,
slots: Arc<Semaphore>,
waker: MultiWaker,
shutdown: Arc<AtomicBool>,
- thread: Mutex<Option<thread::JoinHandle<()>>>,
}
-impl CurlWebSocketRuntime {
- pub fn new() -> Result<Self> {
- let (submissions, rx) = crossbeam_channel::bounded(SESSION_CAPACITY);
- let (waker_tx, waker_rx) = crossbeam_channel::bounded(1);
- let shutdown = Arc::new(AtomicBool::new(false));
- let owner_shutdown = shutdown.clone();
- let thread = thread::Builder::new()
- .name("moli-curl-websocket".to_owned())
- .spawn(move || owner::run(rx, waker_tx, owner_shutdown))
- .context("failed to start curl WebSocket owner")?;
- let waker = waker_rx
- .recv()
- .context("curl WebSocket owner failed to start")?;
- Ok(Self {
- inner: Arc::new(RuntimeInner {
+impl CurlWebSocketConnector {
+ pub(crate) fn channel() -> (
+ crossbeam_channel::Sender<Submission>,
+ crossbeam_channel::Receiver<Submission>,
+ ) {
+ crossbeam_channel::bounded(SESSION_CAPACITY)
+ }
+
+ pub(crate) fn new(
+ submissions: crossbeam_channel::Sender<Submission>,
+ waker: MultiWaker,
+ shutdown: Arc<AtomicBool>,
+ ) -> Self {
+ Self {
+ inner: Arc::new(ConnectorInner {
submissions,
slots: Arc::new(Semaphore::new(SESSION_CAPACITY)),
waker,
shutdown,
- thread: Mutex::new(Some(thread)),
}),
- })
+ }
}
pub fn connect(&self, request: CurlWebSocketRequest) -> Result<CurlWebSocketConnection> {
@@ -349,6 +352,8 @@ impl CurlWebSocketRuntime {
receive_allocations: std::sync::atomic::AtomicUsize::new(0),
#[cfg(test)]
read_waiting: tokio::sync::Notify::new(),
+ #[cfg(test)]
+ owner_thread: Mutex::new(None),
});
let (event_tx, events) = mpsc::channel(MAX_PENDING_EVENTS);
let io = SessionIo {
@@ -370,12 +375,32 @@ impl CurlWebSocketRuntime {
}
}
-impl Drop for RuntimeInner {
- fn drop(&mut self) {
- self.shutdown.store(true, Ordering::Release);
- let _ = self.waker.wakeup();
- if let Some(thread) = self.thread.get_mut().take() {
- let _ = thread.join();
- }
+/// Standalone owner for callers without an existing HTTP runtime. Uses the
+/// same Multi driver as HTTP, with no HTTP submissions.
+#[derive(Clone, Debug)]
+pub struct CurlWebSocketRuntime {
+ runtime: CurlMultiRuntime<StandaloneHandler, ()>,
+}
+
+#[derive(Debug)]
+struct StandaloneHandler;
+impl curl::easy::Handler for StandaloneHandler {}
+
+impl CurlWebSocketRuntime {
+ pub fn new() -> Result<Self> {
+ let (runtime, _) = CurlMultiRuntime::new(CurlMultiRuntimeConfig {
+ thread_name: "moli-curl-websocket".to_owned(),
+ poll_interval: Duration::from_secs(1),
+ ..CurlMultiRuntimeConfig::default()
+ })?;
+ Ok(Self { runtime })
+ }
+
+ pub fn connector(&self) -> CurlWebSocketConnector {
+ self.runtime.websocket_connector()
+ }
+
+ pub fn connect(&self, request: CurlWebSocketRequest) -> Result<CurlWebSocketConnection> {
+ self.connector().connect(request)
}
}
diff --git a/moli-curl/src/websocket/connection_pool.rs b/moli-curl/src/websocket/connection_pool.rs
new file mode 100644
index 000000000..4e18ac656
--- /dev/null
+++ b/moli-curl/src/websocket/connection_pool.rs
@@ -0,0 +1,54 @@
+//! A separate connection cache for persistent WebSockets on the shared Multi.
+//! HTTP pool limits continue to apply to HTTP connections. WebSocket admission
+//! bounds this cache instead. Everything using it stays on the owner thread.
+use std::{ptr::NonNull, rc::Rc};
+
+use anyhow::{Context, Result, bail};
+use curl::easy::Easy2;
+
+use super::request::Handshake;
+
+pub(super) struct ConnectionPool(NonNull<curl_sys::CURLSH>);
+
+impl ConnectionPool {
+ pub(super) fn new() -> Result<Rc<Self>> {
+ // Multi has already initialized libcurl. The pointer is owned here and
+ // is never sent across threads; each bound handler retains an Rc.
+ let pool = Self(
+ NonNull::new(unsafe { curl_sys::curl_share_init() })
+ .context("failed to create WebSocket connection cache")?,
+ );
+ let result = unsafe {
+ curl_sys::curl_share_setopt(
+ pool.0.as_ptr(),
+ curl_sys::CURLSHOPT_SHARE,
+ curl_sys::CURL_LOCK_DATA_CONNECT,
+ )
+ };
+ if result != curl_sys::CURLSHE_OK {
+ bail!("failed to configure WebSocket connection cache: {result}");
+ }
+ Ok(Rc::new(pool))
+ }
+
+ pub(super) fn bind(self: &Rc<Self>, easy: &mut Easy2<Handshake>) -> Result<()> {
+ // CURLOPT_SHARE borrows the pointer. Easy2 runs curl_easy_cleanup
+ // before dropping its Handler, whose Rc therefore outlives that borrow.
+ let result = unsafe {
+ curl_sys::curl_easy_setopt(easy.raw(), curl_sys::CURLOPT_SHARE, self.0.as_ptr())
+ };
+ if result != curl_sys::CURLE_OK {
+ return Err(curl::Error::new(result).into());
+ }
+ easy.get_mut().connection_pool = Some(self.clone());
+ Ok(())
+ }
+}
+
+impl Drop for ConnectionPool {
+ fn drop(&mut self) {
+ // All easy handles borrowing the share have already been cleaned up.
+ let result = unsafe { curl_sys::curl_share_cleanup(self.0.as_ptr()) };
+ debug_assert_eq!(result, curl_sys::CURLSHE_OK);
+ }
+}
diff --git a/moli-curl/src/websocket/owner.rs b/moli-curl/src/websocket/owner.rs
deleted file mode 100644
index 569e0d219..000000000
--- a/moli-curl/src/websocket/owner.rs
+++ /dev/null
@@ -1,242 +0,0 @@
-//! Coordinates admission, DNS, handshakes and scheduling. Native frame I/O and
-//! the lifetime of an attached easy handle belong to Session.
-
-use std::{
- collections::HashMap,
- sync::{
- Arc,
- atomic::{AtomicBool, Ordering},
- },
- time::{Duration, Instant},
-};
-
-use curl::{
- easy::Easy2,
- multi::{Multi, MultiWaker},
-};
-
-use super::{
- SessionIo, Submission,
- diagnostics::Diagnostics,
- request::{self, Handshake},
- scheduling::SocketPoll,
- session::{Session, Step},
-};
-use crate::{CurlDnsResolution, CurlTransferId, dns_adapter::CurlDnsOwnerResidence};
-
-const IDLE_WAIT: Duration = Duration::from_secs(1);
-
-struct Pending {
- id: CurlTransferId,
- easy: Easy2<Handshake>,
- dns: CurlDnsResolution,
- deadline: Instant,
- io: SessionIo,
-}
-
-struct Owner {
- multi: Multi,
- sessions: HashMap<CurlTransferId, Session>,
- dns: CurlDnsOwnerResidence<CurlTransferId, Pending>,
- poll: SocketPoll,
- receive: Vec<u8>,
- diagnostics: Diagnostics,
-}
-
-pub(super) fn run(
- submissions: crossbeam_channel::Receiver<Submission>,
- waker_tx: crossbeam_channel::Sender<MultiWaker>,
- shutdown: Arc<AtomicBool>,
-) {
- let mut owner = Owner {
- multi: Multi::new(),
- sessions: HashMap::new(),
- dns: CurlDnsOwnerResidence::default(),
- poll: SocketPoll::default(),
- receive: Vec::new(),
- diagnostics: Diagnostics::from_env(),
- };
- let _ = waker_tx.send(owner.multi.waker());
- while !shutdown.load(Ordering::Acquire) {
- owner.accept_submissions(&submissions);
- owner.resolve_dns();
- owner.advance_handshakes();
- let progressed = owner.advance_sessions();
- if let Some(counters) = owner.diagnostics.counters() {
- counters.turns += 1;
- counters.progressed_turns += u64::from(progressed);
- }
- owner.wait_for_work(progressed);
- }
- owner.fail_sessions("curl WebSocket runtime shut down");
- for pending in owner.dns.drain() {
- pending
- .io
- .finish(Err("curl WebSocket runtime shut down".to_owned()));
- }
- owner.diagnostics.report(0, true);
-}
-
-impl Owner {
- fn accept_submissions(&mut self, submissions: &crossbeam_channel::Receiver<Submission>) {
- for submission in submissions.try_iter().take(super::SESSION_CAPACITY) {
- if submission.io.cancelled() {
- continue;
- }
- let easy = match request::configure(&submission.request) {
- Ok(easy) => easy,
- Err(error) => {
- submission.io.finish(Err(error.to_string()));
- continue;
- }
- };
- let Some(deadline) = Instant::now().checked_add(submission.request.handshake_timeout)
- else {
- submission
- .io
- .finish(Err("WebSocket handshake timeout is too large".to_owned()));
- continue;
- };
- let pending = Pending {
- id: submission.id,
- easy,
- dns: submission.request.dns_resolution,
- deadline,
- io: submission.io,
- };
- match pending.dns.target().cloned() {
- Some(target) => self
- .dns
- .start(pending.id, pending, target, self.multi.waker()),
- None => self.start(pending),
- }
- }
- }
-
- fn resolve_dns(&mut self) {
- for pending in self
- .dns
- .take_matching(|pending| pending.io.cancelled() || pending.deadline <= Instant::now())
- {
- pending
- .io
- .finish(Err("WebSocket DNS cancelled or timed out".to_owned()));
- }
- while let Some(ready) = self.dns.try_claim_next() {
- let mut pending = ready.pending;
- if pending.io.cancelled() {
- continue;
- }
- let result = ready
- .result
- .map_err(|error| error.to_string())
- .and_then(|addresses| {
- pending
- .dns
- .install(&mut pending.easy, &addresses)
- .map_err(|error| error.to_string())
- });
- match result {
- Ok(()) => self.start(pending),
- Err(error) => pending.io.finish(Err(error)),
- }
- }
- }
-
- fn start(&mut self, pending: Pending) {
- if let Some(session) = Session::attach(
- &mut self.multi,
- pending.id,
- pending.easy,
- pending.io,
- pending.deadline,
- ) {
- self.sessions.insert(pending.id, session);
- }
- }
-
- fn advance_handshakes(&mut self) {
- if let Err(error) = self.multi.perform() {
- self.fail_sessions(&error.to_string());
- }
- let mut completed = Vec::new();
- self.multi.messages(|message| {
- if let (Ok(token), Some(result)) = (message.token(), message.result())
- && let Some(id) = CurlTransferId::from_token(token)
- {
- completed.push((id, result));
- }
- });
- for (id, result) in completed {
- if let Some(session) = self.sessions.get_mut(&id) {
- let step = session.complete_handshake(result);
- self.apply_step(id, step);
- }
- }
- }
-
- fn advance_sessions(&mut self) -> bool {
- let mut retired = Vec::new();
- let mut progressed = false;
- for (id, session) in &mut self.sessions {
- match session.advance(&mut self.receive, &mut self.diagnostics) {
- Ok(Step::Progress) => progressed = true,
- Ok(Step::Idle) => {}
- terminal => retired.push((*id, terminal)),
- }
- }
- for (id, step) in retired {
- self.apply_step(id, step);
- }
- progressed
- }
-
- fn apply_step(&mut self, id: CurlTransferId, step: Result<Step, String>) {
- let result = match step {
- Ok(Step::Progress | Step::Idle) => return,
- Ok(Step::Closed) => Ok(()),
- Err(error) => Err(error),
- };
- if let Some(session) = self.sessions.remove(&id) {
- session.finish(&mut self.multi, result);
- }
- }
-
- fn wait_for_work(&mut self, progressed: bool) {
- let deadline = self
- .sessions
- .values()
- .filter_map(Session::handshake_deadline)
- .chain(self.dns.next_deadline(|pending| Some(pending.deadline)))
- .min();
- let timeout = if progressed {
- // Keep draining local work, but collect other sockets' readiness on
- // every turn so a busy session cannot starve a newly readable one.
- Duration::ZERO
- } else {
- deadline
- .map(|deadline| {
- deadline
- .saturating_duration_since(Instant::now())
- .min(IDLE_WAIT)
- })
- .unwrap_or(IDLE_WAIT)
- };
- if let Err(error) = self.poll.wait(
- &self.multi,
- &mut self.sessions,
- timeout,
- progressed,
- &mut self.diagnostics,
- ) {
- self.fail_sessions(&error.to_string());
- }
- self.diagnostics.report(self.sessions.len(), false);
- }
-
- fn fail_sessions(&mut self, error: &str) {
- for (_, session) in self.sessions.drain() {
- session.finish(&mut self.multi, Err(error.to_owned()));
- }
- }
-}
diff --git a/moli-curl/src/websocket/registry.rs b/moli-curl/src/websocket/registry.rs
new file mode 100644
index 000000000..cfd54cb61
--- /dev/null
+++ b/moli-curl/src/websocket/registry.rs
@@ -0,0 +1,255 @@
+//! Persistent WebSocket residences on the common HTTP/WebSocket Multi.
+//! The runtime owns perform, completion dispatch and the single wait loop.
+
+use std::{
+ collections::HashMap,
+ rc::Rc,
+ time::{Duration, Instant},
+};
+
+use curl::{easy::Easy2, multi::Multi};
+
+use super::{
+ SessionIo, Submission,
+ connection_pool::ConnectionPool,
+ diagnostics::Diagnostics,
+ request::{self, Handshake},
+ scheduling::SocketPoll,
+ session::{Session, Step},
+};
+use crate::{CurlDnsResolution, CurlTransferId, dns_adapter::CurlDnsOwnerResidence};
+
+struct Pending {
+ id: CurlTransferId,
+ easy: Easy2<Handshake>,
+ dns: CurlDnsResolution,
+ deadline: Instant,
+ io: SessionIo,
+}
+
+pub(crate) struct WebSocketRegistry {
+ submissions: crossbeam_channel::Receiver<Submission>,
+ sessions: HashMap<CurlTransferId, Session>,
+ dns: CurlDnsOwnerResidence<CurlTransferId, Pending>,
+ poll: SocketPoll,
+ receive: Vec<u8>,
+ diagnostics: Diagnostics,
+ pool: Option<Rc<ConnectionPool>>,
+ closed: bool,
+}
+
+impl WebSocketRegistry {
+ pub(crate) fn new(submissions: crossbeam_channel::Receiver<Submission>) -> Self {
+ Self {
+ submissions,
+ sessions: HashMap::new(),
+ dns: CurlDnsOwnerResidence::default(),
+ poll: SocketPoll::default(),
+ receive: Vec::new(),
+ diagnostics: Diagnostics::from_env(),
+ pool: None,
+ closed: false,
+ }
+ }
+
+ pub(crate) fn submissions(&self) -> &crossbeam_channel::Receiver<Submission> {
+ &self.submissions
+ }
+
+ pub(crate) fn is_empty(&self) -> bool {
+ self.sessions.is_empty() && self.dns.is_empty()
+ }
+
+ pub(crate) fn shutdown(&mut self, multi: &mut Multi) {
+ if self.closed {
+ return;
+ }
+ self.closed = true;
+ self.fail_sessions(multi, "curl WebSocket runtime shut down");
+ for pending in self.dns.drain() {
+ pending
+ .io
+ .finish(Err("curl WebSocket runtime shut down".to_owned()));
+ }
+ for submission in self.submissions.try_iter() {
+ submission
+ .io
+ .finish(Err("curl WebSocket runtime shut down".to_owned()));
+ }
+ self.diagnostics.report(0, true);
+ }
+
+ pub(crate) fn admit(&mut self, submission: Submission, multi: &mut Multi) {
+ if submission.io.cancelled() {
+ return;
+ }
+ if self.closed {
+ submission
+ .io
+ .finish(Err("curl WebSocket runtime shut down".to_owned()));
+ return;
+ }
+ let easy = request::configure(&submission.request).and_then(|mut easy| {
+ if self.pool.is_none() {
+ self.pool = Some(ConnectionPool::new()?);
+ }
+ self.pool
+ .as_ref()
+ .expect("pool initialized")
+ .bind(&mut easy)?;
+ Ok(easy)
+ });
+ let easy = match easy {
+ Ok(easy) => easy,
+ Err(error) => {
+ submission.io.finish(Err(error.to_string()));
+ return;
+ }
+ };
+ let Some(deadline) = Instant::now().checked_add(submission.request.handshake_timeout)
+ else {
+ submission
+ .io
+ .finish(Err("WebSocket handshake timeout is too large".to_owned()));
+ return;
+ };
+ let pending = Pending {
+ id: submission.id,
+ easy,
+ dns: submission.request.dns_resolution,
+ deadline,
+ io: submission.io,
+ };
+ match pending.dns.target().cloned() {
+ Some(target) => self.dns.start(pending.id, pending, target, multi.waker()),
+ None => self.start(pending, multi),
+ }
+ }
+
+ fn resolve_dns(&mut self, multi: &mut Multi) {
+ for pending in self
+ .dns
+ .take_matching(|pending| pending.io.cancelled() || pending.deadline <= Instant::now())
+ {
+ pending
+ .io
+ .finish(Err("WebSocket DNS cancelled or timed out".to_owned()));
+ }
+ while let Some(ready) = self.dns.try_claim_next() {
+ let mut pending = ready.pending;
+ if pending.io.cancelled() {
+ continue;
+ }
+ let result = ready
+ .result
+ .map_err(|error| error.to_string())
+ .and_then(|addresses| {
+ pending
+ .dns
+ .install(&mut pending.easy, &addresses)
+ .map_err(|error| error.to_string())
+ });
+ match result {
+ Ok(()) => self.start(pending, multi),
+ Err(error) => pending.io.finish(Err(error)),
+ }
+ }
+ }
+
+ fn start(&mut self, pending: Pending, multi: &mut Multi) {
+ if let Some(session) = Session::attach(
+ multi,
+ pending.id,
+ pending.easy,
+ pending.io,
+ pending.deadline,
+ ) {
+ self.sessions.insert(pending.id, session);
+ }
+ }
+
+ pub(crate) fn complete_handshake(
+ &mut self,
+ id: CurlTransferId,
+ result: Result<(), curl::Error>,
+ multi: &mut Multi,
+ ) {
+ if let Some(session) = self.sessions.get_mut(&id) {
+ let step = session.complete_handshake(result);
+ self.apply_step(id, step, multi);
+ }
+ }
+
+ pub(crate) fn handshake_result(
+ &self,
+ id: CurlTransferId,
+ message: &curl::multi::Message<'_>,
+ ) -> Option<Result<(), curl::Error>> {
+ self.sessions.get(&id)?.handshake_result(message)
+ }
+
+ pub(crate) fn advance(&mut self, multi: &mut Multi) -> bool {
+ for _ in 0..super::SESSION_CAPACITY {
+ let Ok(submission) = self.submissions.try_recv() else {
+ break;
+ };
+ self.admit(submission, multi);
+ }
+ self.resolve_dns(multi);
+ let mut retired = Vec::new();
+ let mut progressed = false;
+ for (id, session) in &mut self.sessions {
+ match session.advance(&mut self.receive, &mut self.diagnostics) {
+ Ok(Step::Progress) => progressed = true,
+ Ok(Step::Idle) => {}
+ terminal => retired.push((*id, terminal)),
+ }
+ }
+ for (id, step) in retired {
+ self.apply_step(id, step, multi);
+ }
+ if let Some(counters) = self.diagnostics.counters() {
+ counters.turns += 1;
+ counters.progressed_turns += u64::from(progressed);
+ }
+ progressed
+ }
+
+ fn apply_step(&mut self, id: CurlTransferId, step: Result<Step, String>, multi: &mut Multi) {
+ let result = match step {
+ Ok(Step::Progress | Step::Idle) => return,
+ Ok(Step::Closed) => Ok(()),
+ Err(error) => Err(error),
+ };
+ if let Some(session) = self.sessions.remove(&id) {
+ session.finish(multi, result);
+ }
+ }
+
+ pub(crate) fn next_deadline(&self) -> Option<Instant> {
+ self.sessions
+ .values()
+ .filter_map(Session::handshake_deadline)
+ .chain(self.dns.next_deadline(|pending| Some(pending.deadline)))
+ .min()
+ }
+
+ pub(crate) fn wait(&mut self, multi: &mut Multi, timeout: Duration, progressed: bool) {
+ if let Err(error) = self.poll.wait(
+ multi,
+ &mut self.sessions,
+ timeout,
+ progressed,
+ &mut self.diagnostics,
+ ) {
+ self.fail_sessions(multi, &error.to_string());
+ }
+ self.diagnostics.report(self.sessions.len(), false);
+ }
+
+ pub(crate) fn fail_sessions(&mut self, multi: &mut Multi, error: &str) {
+ for (_, session) in self.sessions.drain() {
+ session.finish(multi, Err(error.to_owned()));
+ }
+ }
+}
diff --git a/moli-curl/src/websocket/request.rs b/moli-curl/src/websocket/request.rs
index 81e9694b4..250b9cc50 100644
--- a/moli-curl/src/websocket/request.rs
+++ b/moli-curl/src/websocket/request.rs
@@ -10,6 +10,7 @@ pub(super) struct Handshake {
pub request: Vec<u8>,
pub response: Vec<u8>,
pub error: Option<String>,
+ pub connection_pool: Option<std::rc::Rc<super::connection_pool::ConnectionPool>>,
proxy_connect: bool,
header_bytes: usize,
}
diff --git a/moli-curl/src/websocket/session.rs b/moli-curl/src/websocket/session.rs
index cc8ded67d..184a48841 100644
--- a/moli-curl/src/websocket/session.rs
+++ b/moli-curl/src/websocket/session.rs
@@ -45,6 +45,10 @@ impl Session {
io: SessionIo,
deadline: Instant,
) -> Option<Self> {
+ #[cfg(test)]
+ {
+ *io.control.owner_thread.lock() = Some(std::thread::current().id());
+ }
if io.cancelled() {
return None;
}
@@ -114,6 +118,13 @@ impl Session {
Ok(Step::Idle)
}
+ pub(super) fn handshake_result(
+ &self,
+ message: &curl::multi::Message<'_>,
+ ) -> Option<Result<(), curl::Error>> {
+ message.result_for2(&self.handle)
+ }
+
pub(super) fn advance(
&mut self,
receive: &mut Vec<u8>,
diff --git a/moli-curl/src/websocket/tests.rs b/moli-curl/src/websocket/tests.rs
index 5b6d9e8d5..d7ea99783 100644
--- a/moli-curl/src/websocket/tests.rs
+++ b/moli-curl/src/websocket/tests.rs
@@ -10,6 +10,7 @@ use tokio_tungstenite::tungstenite::{self, Message, handshake::derive_accept_key
use super::*;
mod readiness;
+mod shared;
mod storage;
const DEADLINE: Duration = Duration::from_secs(10);
diff --git a/moli-curl/src/websocket/tests/shared.rs b/moli-curl/src/websocket/tests/shared.rs
new file mode 100644
index 000000000..95c0b9e36
--- /dev/null
+++ b/moli-curl/src/websocket/tests/shared.rs
@@ -0,0 +1,265 @@
+use std::num::NonZeroUsize;
+
+use curl::easy::{Easy2, Handler, WriteError};
+
+use super::*;
+use crate::{CurlMultiJob, CurlOriginKey};
+
+#[derive(Default, Debug)]
+struct HttpCapture {
+ body: Vec<u8>,
+ owner: Option<thread::ThreadId>,
+ headers: Option<tokio::sync::oneshot::Sender<()>>,
+}
+
+impl Handler for HttpCapture {
+ fn header(&mut self, data: &[u8]) -> bool {
+ if data == b"\r\n"
+ && let Some(sender) = self.headers.take()
+ {
+ let _ = sender.send(());
+ }
+ true
+ }
+
+ fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
+ self.owner = Some(thread::current().id());
+ self.body.extend_from_slice(data);
+ Ok(data.len())
+ }
+}
+
+fn request(url: &str, handler: HttpCapture, timeout: Duration) -> CurlMultiJob<HttpCapture, ()> {
+ let mut easy = Easy2::new(handler);
+ easy.url(url).unwrap();
+ easy.proxy("").unwrap();
+ let url = url::Url::parse(url).unwrap();
+ CurlMultiJob {
+ easy,
+ context: (),
+ origin: Some(CurlOriginKey {
+ scheme: url.scheme().to_owned(),
+ host: url.host_str().unwrap().to_owned(),
+ port: url.port_or_known_default(),
+ }),
+ deadline: Some(std::time::Instant::now() + timeout),
+ dns_resolution: CurlDnsResolution::curl_managed(),
+ priority: 1,
+ label: "mixed test".to_owned(),
+ }
+}
+
+// HTTP and WS use the same authority so both per-host and total pool caps are
+// exercised. /held keeps an HTTP connection occupied until explicitly released.
+fn peer() -> (
+ String,
+ crossbeam_channel::Sender<()>,
+ thread::JoinHandle<()>,
+) {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let base = format!("http://{}", listener.local_addr().unwrap());
+ let (release, released) = crossbeam_channel::bounded(1);
+ let task = thread::spawn(move || {
+ let mut children = Vec::new();
+ loop {
+ let (mut stream, _) = listener.accept().unwrap();
+ stream.set_read_timeout(Some(DEADLINE)).unwrap();
+ stream.set_write_timeout(Some(DEADLINE)).unwrap();
+ let request = read_request(&mut stream);
+ if request.starts_with("GET /stop ") {
+ break;
+ }
+ let released = released.clone();
+ children.push(thread::spawn(move || {
+ if request.starts_with("GET /native ") {
+ let key = request.lines().find_map(|line| {
+ let (key, value) = line.split_once(':')?;
+ key.eq_ignore_ascii_case("sec-websocket-key").then_some(value.trim())
+ }).unwrap();
+ write!(stream, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: {}\r\n\r\n", derive_accept_key(key.as_bytes())).unwrap();
+ let mut socket = tungstenite::WebSocket::from_raw_socket(stream, tungstenite::protocol::Role::Server, None);
+ match socket.read() {
+ Ok(message) => {
+ socket.send(message).unwrap();
+ assert_eq!(socket.get_mut().read(&mut [0]).unwrap(), 0);
+ }
+ Err(tungstenite::Error::Protocol(tungstenite::error::ProtocolError::ResetWithoutClosingHandshake)) => {}
+ result => panic!("unexpected peer read: {result:?}"),
+ }
+ } else {
+ stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n").unwrap();
+ if request.starts_with("GET /held ") {
+ released.recv_timeout(DEADLINE).unwrap();
+ }
+ stream.write_all(b"ok").unwrap();
+ }
+ }));
+ }
+ for child in children {
+ child.join().unwrap();
+ }
+ });
+ (base, release, task)
+}
+
+fn stop_peer(base: &str, peer: thread::JoinHandle<()>) {
+ let authority = base.strip_prefix("http://").unwrap();
+ let mut stream = TcpStream::connect(authority).unwrap();
+ stream
+ .write_all(b"GET /stop HTTP/1.1\r\nHost: localhost\r\n\r\n")
+ .unwrap();
+ peer.join().unwrap();
+}
+
+fn config(total: bool) -> CurlMultiRuntimeConfig {
+ CurlMultiRuntimeConfig {
+ max_active: NonZeroUsize::new(2).unwrap(),
+ max_host_connections: (!total).then(|| NonZeroUsize::new(1).unwrap()),
+ max_total_connections: total.then(|| NonZeroUsize::new(1).unwrap()),
+ ..CurlMultiRuntimeConfig::default()
+ }
+}
+
+#[tokio::test]
+async fn shared_owner_keeps_http_and_websocket_pools_independent() {
+ for total in [false, true] {
+ let (base, _, peer) = peer();
+ let (runtime, completed) = CurlMultiRuntime::new(config(total)).unwrap();
+ let connector = runtime.websocket_connector();
+ let mut connections = Vec::new();
+ // Neither the HTTP host cap nor its total cap of 1 limits these sessions.
+ for _ in 0..2 {
+ let mut request = CurlWebSocketRequest::new(base.replacen("http", "ws", 1) + "/native");
+ request.handshake_timeout = DEADLINE;
+ let mut connection = connector.connect(request).unwrap();
+ opened(&mut connection).await;
+ connections.push(connection);
+ }
+ runtime
+ .submit(request(
+ &(base.clone() + "/ok"),
+ HttpCapture::default(),
+ DEADLINE,
+ ))
+ .unwrap();
+ let completion = completed.recv_timeout(DEADLINE).unwrap();
+ completion.result.unwrap();
+ let easy = completion.easy.unwrap();
+ assert_eq!(easy.get_ref().body, b"ok");
+ for mut connection in connections {
+ assert_eq!(
+ *connection.sender.control.owner_thread.lock(),
+ easy.get_ref().owner
+ );
+ connection.sender().set_reading(true);
+ connection
+ .sender()
+ .send_frame(CurlWebSocketSend {
+ flags: WsFlags::BINARY,
+ data: vec![42],
+ })
+ .await
+ .unwrap();
+ assert!(
+ matches!(event(&mut connection).await, CurlWebSocketEvent::Chunk { data, .. } if data == [42])
+ );
+ drop(connection);
+ }
+ // Retiring WS does not retire the shared owner or its HTTP pool.
+ runtime
+ .submit(request(
+ &(base.clone() + "/after"),
+ HttpCapture::default(),
+ DEADLINE,
+ ))
+ .unwrap();
+ completed.recv_timeout(DEADLINE).unwrap().result.unwrap();
+ drop(runtime);
+ assert!(
+ connector
+ .connect(CurlWebSocketRequest::new(
+ base.replacen("http", "ws", 1) + "/native"
+ ))
+ .is_err()
+ );
+ stop_peer(&base, peer);
+ }
+}
+
+#[tokio::test]
+async fn shared_owner_preserves_http_connection_caps_with_live_websocket() {
+ for total in [false, true] {
+ let (base, release, peer) = peer();
+ let (runtime, completed) = CurlMultiRuntime::new(config(total)).unwrap();
+ let connector = runtime.websocket_connector();
+ let mut connection = connector
+ .connect(CurlWebSocketRequest::new(
+ base.replacen("http", "ws", 1) + "/native",
+ ))
+ .unwrap();
+ opened(&mut connection).await;
+ let (headers, ready) = tokio::sync::oneshot::channel();
+ let first = runtime
+ .submit(request(
+ &(base.clone() + "/held"),
+ HttpCapture {
+ headers: Some(headers),
+ ..HttpCapture::default()
+ },
+ DEADLINE,
+ ))
+ .unwrap();
+ timeout(DEADLINE, ready).await.unwrap().unwrap();
+ // Both HTTP jobs fit max_active=2. Only the connection cap keeps the
+ // second waiting until its own deadline, while the first is held open.
+ let second = runtime
+ .submit(request(
+ &(base.clone() + "/queued"),
+ HttpCapture::default(),
+ Duration::from_millis(200),
+ ))
+ .unwrap();
+ let completion = completed.recv_timeout(DEADLINE).unwrap();
+ assert_eq!(completion.transfer_id, second);
+ assert!(completion.result.unwrap_err().chain().any(|error| {
+ error
+ .downcast_ref::<curl::Error>()
+ .is_some_and(curl::Error::is_operation_timedout)
+ }));
+ release.send(()).unwrap();
+ let completion = completed.recv_timeout(DEADLINE).unwrap();
+ assert_eq!(completion.transfer_id, first);
+ completion.result.unwrap();
+ drop(connection);
+ runtime.shutdown();
+ stop_peer(&base, peer);
+ }
+}
+
+#[tokio::test]
+async fn shared_owner_shutdown_releases_websocket_with_live_connector() {
+ let (base, _, peer) = peer();
+ let (runtime, _) = CurlMultiRuntime::<HttpCapture, ()>::new(config(true)).unwrap();
+ let connector = runtime.websocket_connector();
+ let mut connection = connector
+ .connect(CurlWebSocketRequest::new(
+ base.replacen("http", "ws", 1) + "/native",
+ ))
+ .unwrap();
+ opened(&mut connection).await;
+ // Keep both capabilities alive. Neither owns the native thread's lifetime.
+ drop(runtime);
+ while let Some(event) = timeout(DEADLINE, connection.recv()).await.unwrap() {
+ if let CurlWebSocketEvent::Closed { result } = event {
+ assert!(result.is_err());
+ }
+ }
+ assert!(
+ connector
+ .connect(CurlWebSocketRequest::new(
+ "ws://127.0.0.1:1/native".to_owned()
+ ))
+ .is_err()
+ );
+ stop_peer(&base, peer);
+}