use super::*;
use crate::api::http::{HttpByteStream, TimeoutPhase};
use bytes::Bytes;
use futures_util::task::AtomicWaker;
use futures_util::Stream;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
pub(super) use crate::module::http::http_timeout::TimeoutBudget as StreamBudget;
pub(super) type StreamSupervisor = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
pub(super) struct StreamOwner {
#[cfg(feature = "quality-doctor")]
pub(super) quality_body_phase: Option<QualityPhaseGuard>,
#[cfg(feature = "quality-doctor")]
pub(super) quality_result_on_drop: bool,
pub(super) lease: Option<StreamRegistrationLease>,
pub(super) supervisor_slot: Option<mpsc::OwnedPermit<StreamSupervisor>>,
pub(super) lifecycle: Option<RetryObserverLifecycle>,
pub(super) report: Option<RetryReport>,
pub(super) status_stop_reason: Option<RetryStopReason>,
}
impl StreamOwner {
pub(super) fn new(policy: &crate::api::http::HttpRetryPolicy, start: Instant) -> Self {
Self {
#[cfg(feature = "quality-doctor")]
quality_body_phase: None,
#[cfg(feature = "quality-doctor")]
quality_result_on_drop: true,
lease: None,
supervisor_slot: None,
report: None,
status_stop_reason: None,
lifecycle: Some(RetryObserverLifecycle::new(
policy.observer(),
start,
policy.schedule().clone(),
)),
}
}
pub(super) fn complete(
&mut self,
result: Result<http::StatusCode, &NetError>,
exhausted: bool,
) -> RetryReport {
self.complete_inner(
result,
exhausted,
#[cfg(feature = "quality-doctor")]
true,
)
}
fn complete_inner(
&mut self,
result: Result<http::StatusCode, &NetError>,
exhausted: bool,
#[cfg(feature = "quality-doctor")] record_quality_result: bool,
) -> RetryReport {
#[cfg(feature = "quality-doctor")]
let quality_control = self.lease.as_ref().map(|lease| Arc::clone(&lease.control));
#[cfg(feature = "quality-doctor")]
{
drop(self.quality_body_phase.take());
if record_quality_result {
if let Some(control) = &quality_control {
control.quality_result(result);
}
}
}
drop(self.supervisor_slot.take());
drop(self.lease.take());
let report = if let Some(mut lifecycle) = self.lifecycle.take() {
if result.is_ok() {
if let Some(reason) = self.status_stop_reason.take() {
lifecycle.stop(reason);
}
}
lifecycle.complete_summary(
result,
exhausted,
#[cfg(feature = "quality-doctor")]
record_quality_result,
);
let report = lifecycle.report();
self.report = Some(report.clone());
report
} else {
match &self.report {
Some(report) => report.clone(),
None => RetryReport::new(),
}
};
#[cfg(feature = "quality-doctor")]
if let Some(control) = &quality_control {
#[cfg(feature = "http-client-middleware")]
let middleware_pending = control.middleware_active.load(Ordering::SeqCst);
#[cfg(not(feature = "http-client-middleware"))]
let middleware_pending = false;
if !middleware_pending {
control.quality_finish();
}
}
report
}
}
impl Drop for StreamOwner {
fn drop(&mut self) {
let error = NetError::from(ErrorKind::Cancelled).with_stage(ErrorStage::Receive);
#[cfg(feature = "quality-doctor")]
let record_quality_result = self.quality_result_on_drop;
self.complete_inner(
Err(&error),
false,
#[cfg(feature = "quality-doctor")]
record_quality_result,
);
}
}
struct BodyState {
body: Option<HttpByteStream>,
owner: Option<StreamOwner>,
base_budget: StreamBudget,
budget: StreamBudget,
idle_timeout: Option<Duration>,
polling: bool,
finished: bool,
terminal_ready: bool,
terminal_error: Option<NetError>,
pending_cleanup: Option<Cleanup>,
}
struct SharedBody {
state: Mutex<BodyState>,
control: Arc<CancellationSignal>,
progress: Notify,
consumer: AtomicWaker,
request_id: HttpRequestId,
attempt: u32,
status: http::StatusCode,
exhausted: bool,
}
struct Cleanup {
body: Option<HttpByteStream>,
owner: Option<StreamOwner>,
error: Option<NetError>,
status: http::StatusCode,
exhausted: bool,
}
impl Cleanup {
fn run(self) -> Option<RetryReport> {
let Self {
body,
owner,
error,
status,
exhausted,
} = self;
drop(body);
owner.map(|mut owner| owner.complete(error.as_ref().map_or(Ok(status), Err), exhausted))
}
}
impl SharedBody {
fn with_state<T>(&self, operation: impl FnOnce(&mut BodyState) -> T) -> T {
let (result, poisoned) = {
let (mut guard, poisoned) = match self.state.lock() {
Ok(guard) => (guard, false),
Err(error) => (error.into_inner(), true),
};
(operation(&mut guard), poisoned)
};
if poisoned {
crate::log_e!(crate::LogType::HTTP; "http.stream_body.with_state", "error|operation", "state_poisoned", "provided");
}
result
}
fn annotate(&self, error: NetError) -> NetError {
#[cfg(feature = "quality-doctor")]
let error = self.control.quality_annotate(error);
let error = error
.with_http_request_id(self.request_id)
.with_http_attempt(self.attempt);
match self.control.retry_report() {
Some(report) => error.with_retry_report(report),
None => error,
}
}
fn cancelled(&self) -> NetError {
self.annotate(NetError::from(ErrorKind::Cancelled).with_stage(ErrorStage::Receive))
}
fn boundary_error(&self, state: &BodyState, now: Instant) -> Option<NetError> {
if self.control.is_cancelled() {
Some(self.cancelled())
} else {
state
.budget
.expired(now, ErrorStage::Receive)
.map(|error| self.annotate(error))
}
}
fn finish_locked(&self, state: &mut BodyState, error: Option<NetError>) -> Option<Cleanup> {
if state.finished {
return None;
}
state.finished = true;
state.terminal_error = error.clone();
let cleanup = Cleanup {
body: state.body.take(),
owner: state.owner.take(),
error,
status: self.status,
exhausted: self.exhausted,
};
if state.polling {
state.pending_cleanup = Some(cleanup);
None
} else {
Some(cleanup)
}
}
fn clean_and_wake(&self, cleanup: Option<Cleanup>) {
if let Some(cleanup) = cleanup {
let report = cleanup.run();
self.with_state(|state| {
if let Some(report) = report {
if let Some(error) = state.terminal_error.take() {
state.terminal_error = Some(error.with_retry_report(report));
}
}
state.terminal_ready = true;
});
self.consumer.wake();
self.progress.notify_one();
}
}
fn poll_terminal(&self) -> Poll<Option<Result<Bytes, NetError>>> {
self.with_state(|state| {
if state.terminal_ready {
Poll::Ready(state.terminal_error.take())
} else {
Poll::Pending
}
})
.map(|terminal| terminal.map(|error| Err(self.annotate(error))))
}
fn cancel(&self) {
let cleanup = self.with_state(|state| self.finish_locked(state, Some(self.cancelled())));
self.clean_and_wake(cleanup);
}
}
struct BodyPoll {
shared: Arc<SharedBody>,
body: Option<HttpByteStream>,
}
impl Drop for BodyPoll {
fn drop(&mut self) {
let checked_out = self.body.is_some();
drop(self.body.take());
let cleanup = self.shared.with_state(|state| {
state.polling = false;
if checked_out && !state.finished {
self.shared
.finish_locked(state, Some(self.shared.cancelled()))
} else {
state.pending_cleanup.take()
}
});
self.shared.clean_and_wake(cleanup);
}
}
struct SupervisorGuard(Arc<SharedBody>);
impl Drop for SupervisorGuard {
fn drop(&mut self) {
self.0.cancel();
}
}
pub(super) struct FusedHttpBody {
shared: Arc<SharedBody>,
}
impl FusedHttpBody {
pub(super) fn publish_supervisor(&self, supervisor: StreamSupervisor) -> Result<(), NetError> {
let slot = self
.shared
.with_state(|state| {
state
.owner
.as_mut()
.and_then(|owner| owner.supervisor_slot.take())
})
.ok_or_else(|| NetError::from(ErrorKind::Internal).with_stage(ErrorStage::Dispatch))?;
drop(slot.send(supervisor));
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(super) fn new(
body: HttpByteStream,
control: Arc<CancellationSignal>,
budget: StreamBudget,
owner: StreamOwner,
status: http::StatusCode,
exhausted: bool,
request_id: HttpRequestId,
attempt: u32,
body_start: Instant,
idle_timeout: Option<Duration>,
) -> Result<(Self, StreamSupervisor), NetError> {
let active_budget = budget.with_phase(body_start, idle_timeout, TimeoutPhase::IdleRead)?;
#[cfg(feature = "quality-doctor")]
let owner = {
let mut owner = owner;
owner.quality_body_phase = control.quality_phase(PhaseKind::ResponseBody);
owner
};
let shared = Arc::new(SharedBody {
state: Mutex::new(BodyState {
body: Some(body),
owner: Some(owner),
base_budget: budget,
budget: active_budget,
idle_timeout,
polling: false,
finished: false,
terminal_ready: false,
terminal_error: None,
pending_cleanup: None,
}),
control,
progress: Notify::new(),
consumer: AtomicWaker::new(),
request_id,
attempt,
status,
exhausted,
});
let guard = SupervisorGuard(Arc::clone(&shared));
let supervisor = Box::pin(async move {
let shared = &guard.0;
loop {
let progress = shared.progress.notified();
let (finished, budget, cleanup) = shared.with_state(|state| {
let cleanup = if state.finished {
None
} else {
shared
.boundary_error(state, Instant::now())
.and_then(|error| shared.finish_locked(state, Some(error)))
};
(state.finished, state.budget, cleanup)
});
shared.clean_and_wake(cleanup);
if finished {
return;
}
let mut timer = budget.timer();
tokio::select! {
biased;
_ = shared.control.wait() => {},
_ = progress => {},
_ = wait_budget(&mut timer) => {},
}
}
});
Ok((Self { shared }, supervisor))
}
}
impl Stream for FusedHttpBody {
type Item = Result<Bytes, NetError>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let shared = &self.get_mut().shared;
shared.consumer.register(cx.waker());
let (body, cleanup) = shared.with_state(|state| {
let cleanup = if state.finished {
None
} else {
shared
.boundary_error(state, Instant::now())
.and_then(|error| shared.finish_locked(state, Some(error)))
};
if state.finished {
(None, cleanup)
} else {
let body = state.body.take();
state.polling = body.is_some();
(body, cleanup)
}
});
shared.clean_and_wake(cleanup);
let Some(body) = body else {
return shared.poll_terminal();
};
let mut body = BodyPoll {
shared: Arc::clone(shared),
body: Some(body),
};
let result = match body.body.as_mut() {
Some(body) => body.as_mut().poll_next(cx),
None => return Poll::Pending,
};
let (finished, cleanup, progressed) = shared.with_state(|state| {
let mut cleanup = None;
let mut progressed = false;
if !state.finished {
if let Some(error) = shared.boundary_error(state, Instant::now()) {
cleanup = shared.finish_locked(state, Some(error));
} else {
match &result {
Poll::Ready(Some(Err(error))) => {
cleanup =
shared.finish_locked(state, Some(shared.annotate(error.clone())));
}
Poll::Ready(None) => cleanup = shared.finish_locked(state, None),
Poll::Ready(Some(Ok(bytes))) if !bytes.is_empty() => {
match state.base_budget.with_phase(
Instant::now(),
state.idle_timeout,
TimeoutPhase::IdleRead,
) {
Ok(budget) => {
state.budget = budget;
progressed = true;
}
Err(error) => {
cleanup =
shared.finish_locked(state, Some(shared.annotate(error)))
}
}
}
_ => {}
}
}
}
if !state.finished {
state.body = body.body.take();
}
(state.finished, cleanup, progressed)
});
drop(body);
shared.clean_and_wake(cleanup);
if progressed {
shared.progress.notify_one();
}
if finished {
shared.poll_terminal()
} else {
#[cfg(feature = "quality-doctor")]
if let Poll::Ready(Some(Ok(bytes))) = &result {
shared.control.quality_chunk(bytes.len());
}
result
}
}
}
impl Drop for FusedHttpBody {
fn drop(&mut self) {
self.shared.cancel();
}
}
#[cfg(test)]
mod tests {
use super::super::tests::{check, paused_worker};
use super::*;
use std::collections::VecDeque;
use std::sync::atomic::AtomicUsize;
type TestResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
struct TerminalWake(AtomicUsize);
impl std::task::Wake for TerminalWake {
fn wake(self: Arc<Self>) {
self.wake_by_ref();
}
fn wake_by_ref(self: &Arc<Self>) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}
struct ProbeBody {
items: VecDeque<Result<Bytes, NetError>>,
eof: bool,
polls: Arc<AtomicUsize>,
drops: Arc<AtomicUsize>,
}
impl Stream for ProbeBody {
type Item = Result<Bytes, NetError>;
fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
this.polls.fetch_add(1, Ordering::SeqCst);
match this.items.pop_front() {
Some(item) => Poll::Ready(Some(item)),
None if this.eof => Poll::Ready(None),
None => Poll::Pending,
}
}
}
impl Drop for ProbeBody {
fn drop(&mut self) {
self.drops.fetch_add(1, Ordering::SeqCst);
}
}
struct Fixture {
worker: Arc<HttpClientInner>,
stream: FusedHttpBody,
supervisor: StreamSupervisor,
control: Arc<CancellationSignal>,
request_id: HttpRequestId,
polls: Arc<AtomicUsize>,
drops: Arc<AtomicUsize>,
completed: Arc<AtomicUsize>,
clean_at_observer: Arc<AtomicBool>,
}
fn fixture(
items: Vec<Result<Bytes, NetError>>,
eof: bool,
idle: Option<Duration>,
total: Option<Duration>,
) -> Result<Fixture, Box<dyn std::error::Error + Send + Sync>> {
let (worker, _receiver) = paused_worker()?;
let worker = Arc::new(worker);
let polls = Arc::new(AtomicUsize::new(0));
let drops = Arc::new(AtomicUsize::new(0));
let completed = Arc::new(AtomicUsize::new(0));
let clean_at_observer = Arc::new(AtomicBool::new(false));
let callback_worker = Arc::clone(&worker);
let callback_completed = Arc::clone(&completed);
let callback_clean = Arc::clone(&clean_at_observer);
let callback_drops = Arc::clone(&drops);
let observed_shared = Arc::new(std::sync::OnceLock::<std::sync::Weak<SharedBody>>::new());
let callback_shared = Arc::clone(&observed_shared);
let policy = crate::api::http::HttpRetryPolicy::builder()
.observer(move |event: &RetryEvent| {
if matches!(event, RetryEvent::Completed(_)) {
callback_completed.fetch_add(1, Ordering::SeqCst);
let unlocked = callback_shared
.get()
.and_then(std::sync::Weak::upgrade)
.is_some_and(|shared| {
let unlocked = shared.state.try_lock().is_ok();
if unlocked {
shared.cancel();
}
unlocked
});
callback_clean.store(
unlocked
&& callback_worker.registry.is_empty().is_ok_and(|empty| empty)
&& callback_worker.in_flight.available_permits() == 128
&& callback_drops.load(Ordering::SeqCst) == 1,
Ordering::SeqCst,
);
}
})
.build()?;
let (registered, registration) = worker.register_request(None)?;
registration.commit();
let control = registered.control;
let start = Instant::now();
let mut owner = StreamOwner::new(&policy, start);
if let Some(lifecycle) = owner.lifecycle.as_mut() {
lifecycle.control = Some(Arc::clone(&control));
}
owner.lease = Some(StreamRegistrationLease {
registry: worker.registry(),
request_id: registered.id,
control: Arc::clone(&control),
permit: Some(Arc::new(worker.try_acquire_permit()?)),
});
let body = ProbeBody {
items: items.into(),
eof,
polls: Arc::clone(&polls),
drops: Arc::clone(&drops),
};
let (stream, supervisor) = FusedHttpBody::new(
Box::pin(body),
Arc::clone(&control),
StreamBudget::total(start, total)?,
owner,
http::StatusCode::OK,
false,
registered.id,
2,
start,
idle,
)?;
observed_shared
.set(Arc::downgrade(&stream.shared))
.map_err(|_| std::io::Error::other("stream observation was already installed"))?;
Ok(Fixture {
worker,
stream,
supervisor,
control,
request_id: registered.id,
polls,
drops,
completed,
clean_at_observer,
})
}
#[tokio::test(start_paused = true)]
async fn terminal_waits_for_cleanup_and_the_final_report_before_delivery() -> TestResult {
for cancelled in [false, true] {
let Fixture {
worker,
mut stream,
supervisor,
control,
drops,
completed,
clean_at_observer,
..
} = fixture(vec![], false, None, Some(Duration::from_secs(1)))?;
if cancelled {
control.cancel();
} else {
tokio::time::advance(Duration::from_secs(1)).await;
}
let cleanup = stream
.shared
.with_state(|state| {
stream
.shared
.boundary_error(state, Instant::now())
.and_then(|error| stream.shared.finish_locked(state, Some(error)))
})
.ok_or_else(|| std::io::Error::other("terminal cleanup missing"))?;
let wake = Arc::new(TerminalWake(AtomicUsize::new(0)));
let waker = std::task::Waker::from(Arc::clone(&wake));
let mut cx = Context::from_waker(&waker);
check(
Pin::new(&mut stream).poll_next(&mut cx).is_pending(),
"consumer received a terminal item before cleanup published its report",
)?;
stream.shared.clean_and_wake(Some(cleanup));
check(
wake.0.load(Ordering::SeqCst) > 0,
"terminal publication did not wake the waiting consumer",
)?;
let error = match Pin::new(&mut stream).poll_next(&mut cx) {
Poll::Ready(Some(Err(error))) => error,
_ => return Err(std::io::Error::other("terminal error missing").into()),
};
let report = control
.retry_report()
.ok_or_else(|| std::io::Error::other("final report missing"))?;
check(
error.retry_report() == Some(&report)
&& report.stop_reason
== Some(if cancelled {
RetryStopReason::Cancelled
} else {
RetryStopReason::TimedOut
})
&& error.timeout_phase()
== if cancelled {
None
} else {
Some(TimeoutPhase::Total)
},
"delivered error lost its terminal reason or final report",
)?;
check(
worker.registry.is_empty()?
&& worker.in_flight.available_permits() == 128
&& clean_at_observer.load(Ordering::SeqCst),
"terminal publication preceded resource release",
)?;
check(
matches!(Pin::new(&mut stream).poll_next(&mut cx), Poll::Ready(None)),
"terminal error was delivered more than once",
)?;
drop(supervisor);
drop(stream);
check(
drops.load(Ordering::SeqCst) == 1 && completed.load(Ordering::SeqCst) == 1,
"terminal cleanup or observer completion ran more than once",
)?;
}
Ok(())
}
struct CancelDuringPoll {
shared: std::sync::Weak<SharedBody>,
drops: Arc<AtomicUsize>,
completed: Arc<AtomicUsize>,
completed_during_poll: Arc<AtomicUsize>,
}
impl Stream for CancelDuringPoll {
type Item = Result<Bytes, NetError>;
fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if let Some(shared) = self.shared.upgrade() {
shared.cancel();
self.completed_during_poll
.store(self.completed.load(Ordering::SeqCst), Ordering::SeqCst);
}
Poll::Ready(Some(Ok(Bytes::from_static(b"late bytes"))))
}
}
impl Drop for CancelDuringPoll {
fn drop(&mut self) {
self.drops.fetch_add(1, Ordering::SeqCst);
}
}
#[tokio::test]
async fn cancellation_during_body_poll_releases_body_before_completing() -> TestResult {
let Fixture {
mut stream,
supervisor,
drops,
completed,
clean_at_observer,
..
} = fixture(vec![], false, None, None)?;
let completed_during_poll = Arc::new(AtomicUsize::new(0));
let body = CancelDuringPoll {
shared: Arc::downgrade(&stream.shared),
drops: Arc::clone(&drops),
completed: Arc::clone(&completed),
completed_during_poll: Arc::clone(&completed_during_poll),
};
let previous = stream
.shared
.with_state(|state| state.body.replace(Box::pin(body)));
drop(previous);
drops.store(0, Ordering::SeqCst);
check(
matches!(stream.next().await, Some(Err(error)) if error.kind() == ErrorKind::Cancelled),
"cancellation did not discard the checked-out body's late bytes",
)?;
check(
completed_during_poll.load(Ordering::SeqCst) == 0,
"observer completed while the transport body was still being polled",
)?;
check(
clean_at_observer.load(Ordering::SeqCst)
&& completed.load(Ordering::SeqCst) == 1
&& drops.load(Ordering::SeqCst) == 1,
"cleanup did not release the body before completing exactly once",
)?;
check(
stream.next().await.is_none(),
"cancelled body was not fused",
)?;
drop(supervisor);
Ok(())
}
#[tokio::test(start_paused = true)]
async fn supervisor_handoff_and_prepublication_cancel_release_before_observer() -> TestResult {
for publish in [false, true] {
let (worker, _requests) = paused_worker()?;
let worker = Arc::new(worker);
let (supervisor_tx, mut supervisor_rx) = mpsc::channel::<StreamSupervisor>(1);
let drops = Arc::new(AtomicUsize::new(0));
let completed = Arc::new(AtomicUsize::new(0));
let clean_at_observer = Arc::new(AtomicBool::new(false));
let callback_worker = Arc::clone(&worker);
let callback_supervisor_tx = supervisor_tx.clone();
let callback_drops = Arc::clone(&drops);
let callback_completed = Arc::clone(&completed);
let callback_clean = Arc::clone(&clean_at_observer);
let policy = crate::api::http::HttpRetryPolicy::builder()
.observer(move |event: &RetryEvent| {
if matches!(event, RetryEvent::Completed(_)) {
callback_completed.fetch_add(1, Ordering::SeqCst);
callback_clean.store(
callback_worker.registry.is_empty().is_ok_and(|empty| empty)
&& callback_worker.in_flight.available_permits() == 128
&& callback_supervisor_tx.try_reserve().is_ok()
&& callback_drops.load(Ordering::SeqCst) == 1,
Ordering::SeqCst,
);
}
})
.build()?;
let (registered, registration) = worker.register_request(None)?;
registration.commit();
let control = registered.control;
let start = Instant::now();
let mut owner = StreamOwner::new(&policy, start);
owner.supervisor_slot = Some(
supervisor_tx
.clone()
.try_reserve_owned()
.map_err(|_| std::io::Error::other("supervisor reservation failed"))?,
);
owner.lease = Some(StreamRegistrationLease {
registry: worker.registry(),
request_id: registered.id,
control: Arc::clone(&control),
permit: Some(Arc::new(worker.try_acquire_permit()?)),
});
let body = ProbeBody {
items: VecDeque::new(),
eof: false,
polls: Arc::new(AtomicUsize::new(0)),
drops: Arc::clone(&drops),
};
let (mut stream, supervisor) = FusedHttpBody::new(
Box::pin(body),
Arc::clone(&control),
StreamBudget::total(start, None)?,
owner,
http::StatusCode::OK,
false,
registered.id,
1,
start,
None,
)?;
check(
supervisor_tx.capacity() == 0,
"opening must retain its supervisor slot until handoff or cleanup",
)?;
let supervisor = if publish {
stream.publish_supervisor(supervisor)?;
supervisor_rx
.recv()
.await
.ok_or_else(|| std::io::Error::other("supervisor was not published"))?
} else {
supervisor
};
control.cancel();
tokio::time::timeout(Duration::from_secs(1), supervisor).await?;
check(
clean_at_observer.load(Ordering::SeqCst),
"observer ran before supervisor slot, permit, registry, and body were released",
)?;
check(
completed.load(Ordering::SeqCst) == 1 && supervisor_tx.capacity() == 1,
"supervisor handoff cleanup was duplicated or retained lane capacity",
)?;
check(
matches!(stream.next().await, Some(Err(error)) if error.kind() == ErrorKind::Cancelled),
"cancelled handoff lost the terminal stream error",
)?;
drop(stream);
check(
completed.load(Ordering::SeqCst) == 1 && drops.load(Ordering::SeqCst) == 1,
"dropping the terminated stream repeated completion or body cleanup",
)?;
}
Ok(())
}
#[tokio::test(start_paused = true)]
async fn unpolled_idle_expiry_releases_resources_and_preserves_one_error() -> TestResult {
let Fixture {
worker,
mut stream,
supervisor,
control,
request_id,
polls,
drops,
completed,
clean_at_observer,
} = fixture(vec![], false, Some(Duration::from_secs(5)), None)?;
let task = tokio::spawn(supervisor);
tokio::task::yield_now().await;
tokio::time::advance(Duration::from_secs(5)).await;
task.await?;
check(
polls.load(Ordering::SeqCst) == 0,
"supervisor prefetched body",
)?;
check(
drops.load(Ordering::SeqCst) == 1,
"unpolled transport was not dropped",
)?;
check(
worker.registry.is_empty()? && worker.in_flight.available_permits() == 128,
"unpolled expiry retained registration or capacity",
)?;
check(
clean_at_observer.load(Ordering::SeqCst),
"observer ran before cleanup",
)?;
control.cancel();
check(
matches!(stream.next().await, Some(Err(error))
if error.timeout_phase() == Some(TimeoutPhase::IdleRead)
&& error.context().http_request_id == Some(request_id)
&& error.context().http_attempt == Some(2)),
"timeout lost phase or request context",
)?;
check(stream.next().await.is_none(), "timeout error was not fused")?;
drop(stream);
check(
completed.load(Ordering::SeqCst) == 1 && drops.load(Ordering::SeqCst) == 1,
"termination happened more than once",
)
}
#[tokio::test(start_paused = true)]
async fn cancellation_wins_ready_deadline_and_works_without_any_timeout() -> TestResult {
for timeout in [None, Some(Duration::from_secs(5))] {
let Fixture {
mut stream,
supervisor,
control,
drops,
..
} = fixture(vec![], false, timeout, timeout)?;
tokio::time::advance(Duration::from_secs(5)).await;
control.cancel();
tokio::spawn(supervisor).await?;
check(
drops.load(Ordering::SeqCst) == 1,
"cancellation retained transport",
)?;
check(
matches!(stream.next().await, Some(Err(error)) if error.kind() == ErrorKind::Cancelled),
"visible cancellation lost to the deadline",
)?;
check(
stream.next().await.is_none(),
"cancelled stream was not fused",
)?;
}
Ok(())
}
#[tokio::test(start_paused = true)]
async fn only_nonempty_delivery_renews_idle_and_total_still_wins_ties() -> TestResult {
let Fixture {
mut stream,
supervisor,
drops,
..
} = fixture(
vec![Ok(Bytes::from_static(b"progress")), Ok(Bytes::new())],
false,
Some(Duration::from_secs(5)),
Some(Duration::from_secs(9)),
)?;
let task = tokio::spawn(supervisor);
tokio::task::yield_now().await;
tokio::time::advance(Duration::from_secs(4)).await;
check(
matches!(stream.next().await, Some(Ok(bytes)) if bytes == Bytes::from_static(b"progress")),
"nonempty delivery failed",
)?;
tokio::time::advance(Duration::from_secs(2)).await;
tokio::task::yield_now().await;
check(
drops.load(Ordering::SeqCst) == 0,
"stale idle timer terminated progress",
)?;
check(
matches!(stream.next().await, Some(Ok(bytes)) if bytes.is_empty()),
"empty delivery failed",
)?;
let waker = futures_util::task::noop_waker();
let mut cx = Context::from_waker(&waker);
for _ in 0..3 {
check(
Pin::new(&mut stream).poll_next(&mut cx).is_pending(),
"body was not pending",
)?;
}
tokio::time::advance(Duration::from_secs(3)).await;
task.await?;
check(
matches!(stream.next().await, Some(Err(error))
if error.timeout_phase() == Some(TimeoutPhase::Total)),
"total did not win an equal idle deadline",
)?;
check(
stream.next().await.is_none(),
"terminal stream was not fused",
)
}
#[tokio::test(start_paused = true)]
async fn empty_chunks_and_pending_polls_do_not_renew_idle() -> TestResult {
let Fixture {
mut stream,
supervisor,
..
} = fixture(
vec![Ok(Bytes::new())],
false,
Some(Duration::from_secs(5)),
None,
)?;
let task = tokio::spawn(supervisor);
tokio::time::advance(Duration::from_secs(4)).await;
check(
matches!(stream.next().await, Some(Ok(bytes)) if bytes.is_empty()),
"empty chunk failed",
)?;
let waker = futures_util::task::noop_waker();
let mut cx = Context::from_waker(&waker);
check(
Pin::new(&mut stream).poll_next(&mut cx).is_pending(),
"body was not pending",
)?;
tokio::time::advance(Duration::from_secs(1)).await;
task.await?;
check(
matches!(stream.next().await, Some(Err(error))
if error.timeout_phase() == Some(TimeoutPhase::IdleRead)),
"empty chunk or pending poll renewed idle",
)
}
#[tokio::test(start_paused = true)]
async fn eof_error_drop_and_abandoned_supervisor_release_once() -> TestResult {
for body_error in [false, true] {
let items = if body_error {
vec![Err(
NetError::from(ErrorKind::Io).with_stage(ErrorStage::Receive)
)]
} else {
vec![]
};
let Fixture {
mut stream,
supervisor,
control,
completed,
clean_at_observer,
..
} = fixture(items, true, None, None)?;
let task = tokio::spawn(supervisor);
let next = stream.next().await;
if body_error {
check(
next.as_ref()
.and_then(|item| item.as_ref().err())
.and_then(NetError::retry_report)
== control.retry_report().as_ref(),
"body failure was delivered before its final report",
)?;
}
check(
control.retry_report().is_some(),
"EOF or body failure did not finalize its report",
)?;
check(
if body_error {
matches!(next, Some(Err(_)))
} else {
next.is_none()
},
"unexpected terminal result",
)?;
check(
clean_at_observer.load(Ordering::SeqCst),
"cleanup followed observer",
)?;
control.cancel();
check(stream.next().await.is_none(), "EOF or error did not fuse")?;
drop(stream);
task.await?;
check(
completed.load(Ordering::SeqCst) == 1,
"observer completed more than once",
)?;
}
let Fixture {
stream,
supervisor,
drops,
completed,
..
} = fixture(vec![], false, None, None)?;
drop(stream);
tokio::spawn(supervisor).await?;
check(
drops.load(Ordering::SeqCst) == 1 && completed.load(Ordering::SeqCst) == 1,
"stream drop did not terminate supervision once",
)?;
let Fixture {
mut stream,
supervisor,
drops,
..
} = fixture(vec![], false, None, None)?;
drop(supervisor);
check(
drops.load(Ordering::SeqCst) == 1,
"unpolled supervisor drop retained transport",
)?;
check(
matches!(stream.next().await, Some(Err(error)) if error.kind() == ErrorKind::Cancelled),
"abandoned supervisor did not signal cancellation",
)
}
#[tokio::test(start_paused = true)]
async fn ready_chunk_checks_cancellation_after_polling_outside_the_mutex() -> TestResult {
let Fixture {
mut stream,
supervisor,
control,
..
} = fixture(vec![], false, None, None)?;
let weak = Arc::downgrade(&stream.shared);
let unlocked = Arc::new(AtomicBool::new(false));
let poll_unlocked = Arc::clone(&unlocked);
let body = futures_util::stream::poll_fn(move |_| {
if let Some(shared) = weak.upgrade() {
poll_unlocked.store(shared.state.try_lock().is_ok(), Ordering::SeqCst);
}
control.cancel();
Poll::Ready(Some(Ok(Bytes::from_static(b"late bytes"))))
});
let previous = stream
.shared
.with_state(|state| state.body.replace(Box::pin(body)));
drop(previous);
check(
matches!(stream.next().await, Some(Err(error)) if error.kind() == ErrorKind::Cancelled),
"late bytes crossed visible cancellation",
)?;
check(
unlocked.load(Ordering::SeqCst),
"body poll ran under the shared mutex",
)?;
check(
stream.next().await.is_none(),
"late ready chunk was retained",
)?;
tokio::spawn(supervisor).await?;
Ok(())
}
}