Skip to main content

a2a_protocol_client/delegation/
drop.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! What dropping a [`Delegation`] does. Moved out of `mod.rs` when resuming
5//! took that file past the 500-line ratchet; nothing else changed.
6
7use super::Delegation;
8
9impl Drop for Delegation {
10    /// Cancels an unsettled child in the background, on the current Tokio
11    /// runtime. Outside a runtime nothing can be sent, and the drop is only
12    /// logged.
13    fn drop(&mut self) {
14        if !self.armed || self.settled.is_some() {
15            return;
16        }
17        let Ok(rt) = tokio::runtime::Handle::try_current() else {
18            trace_warn!(
19                task_id = self.task_id.as_deref().unwrap_or_default(),
20                "delegation dropped outside a Tokio runtime; its child was not cancelled"
21            );
22            return;
23        };
24        let rest = Self {
25            client: std::sync::Arc::clone(&self.client),
26            tenant: self.tenant.take(),
27            stream: self.stream.take(),
28            task_id: self.task_id.take(),
29            settled: None,
30            armed: false,
31            id_wait: self.id_wait,
32            renewal: None,
33            budget: None,
34            idempotency_key: self.idempotency_key.take(),
35            origin: self.origin.clone(),
36            resume_policy: super::ResumePolicy::none(),
37            last_event_id: self.last_event_id.take(),
38            streak: 0,
39            resumed_at: None,
40            opened_at: None,
41            events_since_open: 0,
42            message: None,
43            context_id: None,
44            handed_back: false,
45            continued: 0,
46            hand_off_policy: super::HandOffPolicy::default(),
47            // The cancel the drop sends settles the record too.
48            journal: self.journal.take(),
49        };
50        let origin = self.origin.clone();
51        rt.spawn(origin.carry(async move {
52            let done = rest.cancel().await;
53            trace_debug!(
54                task_id = done.task_id.as_deref().unwrap_or_default(),
55                outcome = ?done.outcome,
56                "delegation dropped before its child settled"
57            );
58            let _ = done;
59        }));
60    }
61}