From a2d27b154b6ab9d67bb180930250b244ea2981ac Mon Sep 17 00:00:00 2001 From: Nils Andreas Svee Date: Wed, 29 Apr 2026 01:14:32 +0700 Subject: [PATCH] feat(managed): implement automatic cancellation on session drop --- crates/irtt-client/src/managed/runner.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/irtt-client/src/managed/runner.rs b/crates/irtt-client/src/managed/runner.rs index f3bdaf9..96ecdd9 100644 --- a/crates/irtt-client/src/managed/runner.rs +++ b/crates/irtt-client/src/managed/runner.rs @@ -28,7 +28,7 @@ pub struct ManagedClient; pub struct ManagedClientSession { hub: EventHub, cancellation: CancellationToken, - worker: JoinHandle>, + worker: Option>>, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -92,7 +92,7 @@ impl ManagedClient { ManagedClientSession { hub, cancellation, - worker, + worker: Some(worker), }, initial_subscription, )) @@ -108,8 +108,9 @@ impl ManagedClientSession { self.cancellation.cancel(); } - pub fn join(self) -> Result { - match self.worker.join() { + pub fn join(mut self) -> Result { + let worker = self.worker.take().expect("worker handle must be present"); + match worker.join() { Ok(outcome) => { self.hub.disconnect_all(); outcome @@ -122,6 +123,12 @@ impl ManagedClientSession { } } +impl Drop for ManagedClientSession { + fn drop(&mut self) { + self.cancellation.cancel(); + } +} + fn publish_open_outcome(hub: &EventHub, outcome: &OpenOutcome) { match outcome { OpenOutcome::Started { event, .. } | OpenOutcome::NoTestCompleted { event, .. } => { @@ -495,7 +502,7 @@ mod tests { let session = ManagedClientSession { hub, cancellation, - worker, + worker: Some(worker), }; assert!(matches!(session.join(), Err(ClientError::WorkerPanicked)));