open-net 0.3.5

A cross-platform async networking library for Rust with HTTP and WebSocket clients, network status monitoring, configurable retries, WebSocket reconnection and heartbeats, and managed client lifecycles. Reuse common networking infrastructure across applications to avoid repeated implementation and debugging, helping save development time and AI coding tokens.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
//! Shared ownership and independent supervision for lazy HTTP response streams.
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>>;

/// Opening futures and delivered streams use the same lifecycle owner. Capacity
/// is returned before an observer can reenter the client.
pub(super) struct StreamOwner {
    #[cfg(feature = "quality-doctor")]
    pub(super) quality_body_phase: Option<QualityPhaseGuard>,
    // An uncompleted middleware continuation defers its implicit drop result
    // to the middleware lease. Explicit transport/body results still win.
    #[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);
                }
            }
            // Retry bookkeeping also publishes quality results. Carry the same
            // selection decision through it without changing retry completion.
            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,
}

/// All potentially reentrant destruction and callbacks happen after unlocking.
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 {
        // No user code runs in this critical section. Recovering a poisoned
        // mutex still allows a future cleanup to return its owned resources.
        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> {
        // Cancellation visible at the arbitration point wins every deadline.
        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 {
            // The checked-out body must be destroyed before releasing its owner.
            // Its poll guard takes responsibility for cleanup, including unwind.
            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);
    }
}

/// Own a body while transport polling runs outside the state mutex. Complete
/// deferred cleanup after dropping that body, including when polling unwinds.
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);
    }
}

/// A supervisor dropped by the runtime also releases its network resources,
/// including when it is dropped before its first poll.
struct SupervisorGuard(Arc<SharedBody>);
impl Drop for SupervisorGuard {
    fn drop(&mut self) {
        self.0.cancel();
    }
}

pub(super) struct FusedHttpBody {
    shared: Arc<SharedBody>,
}
impl FusedHttpBody {
    /// Keep the reservation with body ownership through every fallible handoff.
    /// Completion drops the slot before notifying a potentially reentrant observer.
    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 {
                // notify_one retains a permit when progress arrives between the
                // snapshot and this wait. Every wake rechecks the current budget,
                // so an old idle timer cannot terminate a newly advanced stream.
                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,
        };
        // Transport polling is outside the mutex. A concurrent supervisor may
        // commit a terminal state while poll is running; discard any late bytes.
        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)
        });
        // Destroy the checked-out body before completing the observer lifecycle.
        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 {
                                // Cancellation/destruction reentry must not wait for
                                // this observer to finish publishing the terminal item.
                                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;
            }
            // Suspend the supervisor after its terminal decision, before cleanup.
            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() {
                // Run the supervisor's cancellation while this body is checked out.
                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",
        )?;
        // A cancellation after the committed timeout cannot rewrite the result.
        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(())
    }
}