geiserx_ts_runtime 0.57.7

tailscale runtime
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
//! Startup barrier and fail-closed supervision for security-state consumers.
//!
//! Register acknowledgements are awaited in each actor's on_start. Complete all of these
//! starts before spawning control (including cached-map replay). Never ask the broker to
//! publish: a handler waiting for publication could deadlock its own full mailbox.

use std::sync::{Arc, Mutex};

use kameo::{Actor, actor::ActorRef};
use tokio::{sync::watch, task::JoinSet};

use crate::{Error, ErrorKind};

struct FailureState {
    shutdown: watch::Sender<bool>,
    stops: Mutex<Vec<Box<dyn Fn() + Send + Sync>>>,
}

impl FailureState {
    fn stop(&self) {
        self.shutdown.send_replace(true);
        // Only nonblocking kill/abort callbacks. No actor messaging or asynchronous locks.
        for stop in self.stops.lock().unwrap().iter() {
            stop();
        }
    }
}

pub(crate) struct SecurityGuard {
    failure: Arc<FailureState>,
    monitors: JoinSet<()>,
}

impl SecurityGuard {
    pub(crate) fn new(shutdown: watch::Sender<bool>) -> Self {
        Self {
            failure: Arc::new(FailureState {
                shutdown,
                stops: Mutex::new(Vec::new()),
            }),
            monitors: JoinSet::new(),
        }
    }

    pub(crate) fn watch<A: Actor>(&mut self, actor: &ActorRef<A>) {
        // Retain a strong ref until cancellation. With weak refs, concurrent broker shutdown
        // and monitor exit could drop the last sender before kill, stranding a blocked handler.
        let stop_actor = actor.clone();
        self.failure
            .stops
            .lock()
            .unwrap()
            .push(Box::new(move || stop_actor.kill()));
        let failure = self.failure.clone();
        let mut shutdown = failure.shutdown.subscribe();
        let actor = actor.clone();
        self.monitors.spawn(async move {
            tokio::select! {
                () = actor.wait_for_shutdown() => {
                    // Drop the watch read guard before taking its write lock in stop().
                    let stopping = *shutdown.borrow();
                    if !stopping {
                        tracing::error!(actor = std::any::type_name::<A>(),
                            "security actor stopped; stopping forwarding");
                        failure.stop();
                    }
                }
                _ = async { drop(shutdown.wait_for(|stopping| *stopping).await); } => {}
            }
        });
        // Covers actors added concurrently with an earlier actor's failure.
        let stopping = *self.failure.shutdown.borrow();
        if stopping {
            self.failure.stop();
        }
    }

    pub(crate) fn packet_pump(&mut self, abort: tokio::task::AbortHandle) {
        // Abort the forwarding pump first, even if the dataplane actor is blocked in a handler.
        self.failure
            .stops
            .lock()
            .unwrap()
            .insert(0, Box::new(move || abort.abort()));
        let stopping = *self.failure.shutdown.borrow();
        if stopping {
            self.failure.stop();
        }
    }

    pub(crate) async fn ready<A: Actor>(&self, actor: &ActorRef<A>) -> Result<(), Error> {
        let mut shutdown = self.failure.shutdown.subscribe();
        let ready = tokio::select! {
            ready = actor.wait_for_startup_with_result(|result| result.is_ok()) => ready,
            _ = async { drop(shutdown.wait_for(|stopping| *stopping).await); } => false,
        };
        let stopping = *shutdown.borrow();
        if ready && !stopping {
            Ok(())
        } else {
            self.failure.stop();
            Err(Error {
                kind: ErrorKind::ActorGone,
                target_actor: Some(actor.into()),
                message_ty: None,
            })
        }
    }
}

impl Drop for SecurityGuard {
    fn drop(&mut self) {
        // Also cleans up a partially constructed runtime when any startup step returns Err.
        self.failure.stop();
        self.monitors.abort_all();
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use kameo::{
        actor::Spawn,
        message::{Context, Message},
    };
    use tokio::sync::{Notify, mpsc};

    use super::*;
    use crate::{
        Env,
        dataplane::{DataplaneActor, NewOverlayTransport, PacketPumpAbortHandle},
        env::ForwarderConfig,
        forwarder_actor::ForwarderActor,
    };

    fn environment() -> (Env, watch::Sender<bool>) {
        let (shutdown, rx) = watch::channel(false);
        let env = Env::new(
            ts_keys::NodeState::generate(),
            rx,
            ForwarderConfig::from_control_config(&ts_control::Config::default()),
        );
        (env, shutdown)
    }

    struct Probe {
        env: Env,
        seen: mpsc::UnboundedSender<u32>,
        pause: Option<(Arc<Notify>, Arc<Notify>)>,
    }

    impl Actor for Probe {
        type Args = (
            Env,
            mpsc::UnboundedSender<u32>,
            Option<(Arc<Notify>, Arc<Notify>)>,
            Option<Arc<Notify>>,
        );
        type Error = Error;

        async fn on_start(
            (env, seen, pause, start): Self::Args,
            slf: ActorRef<Self>,
        ) -> Result<Self, Error> {
            if let Some(start) = start {
                start.notified().await;
            }
            env.subscribe::<u32>(&slf).await?;
            Ok(Self { env, seen, pause })
        }
    }

    impl Message<u32> for Probe {
        type Reply = ();
        async fn handle(&mut self, value: u32, _: &mut Context<Self, ()>) {
            if value == 0
                && let Some((entered, release)) = self.pause.take()
            {
                entered.notify_one();
                release.notified().await;
                // The mailbox is full and the broker is waiting for this actor to drain it.
                // Waiting for broker handling here (or using a bounded broker) would deadlock.
                self.env.publish(10_000_u32).await.unwrap();
            }
            self.seen.send(value).unwrap();
        }
    }

    #[tokio::test]
    async fn saturated_subscriber_can_publish_from_handler_and_keeps_order() {
        tokio::time::timeout(Duration::from_secs(5), async {
            let (env, shutdown) = environment();
            let mut guard = SecurityGuard::new(shutdown);
            guard.watch(&env.bus);
            let (seen, mut rx) = mpsc::unbounded_channel();
            let entered = Arc::new(Notify::new());
            let release = Arc::new(Notify::new());
            let actor = Probe::spawn_with_mailbox(
                (
                    env.clone(),
                    seen,
                    Some((entered.clone(), release.clone())),
                    None,
                ),
                kameo::mailbox::bounded(1),
            );
            guard.watch(&actor);
            guard.ready(&actor).await.unwrap();
            env.publish(0_u32).await.unwrap();
            entered.notified().await;
            // More than even the old default broker capacity; publishing must finish while
            // the subscriber is blocked. The one-slot subscriber necessarily fills too.
            for n in 1..=128_u32 {
                env.publish(n).await.unwrap();
            }
            while actor.mailbox_sender().capacity() != Some(0) {
                tokio::task::yield_now().await;
            }
            // A harmless broker request stays queued: the broker is actually blocked,
            // not merely scheduled behind the publisher on this single-threaded runtime.
            assert!(
                tokio::time::timeout(
                    Duration::from_millis(20),
                    env.bus
                        .ask(kameo_actors::message_bus::Unregister::<u64>::new(
                            actor.id()
                        ))
                )
                .await
                .is_err()
            );
            release.notify_one();
            for n in 0..=128_u32 {
                assert_eq!(rx.recv().await, Some(n));
            }
            assert_eq!(rx.recv().await, Some(10_000));
        })
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn late_registration_ack_orders_future_publications_without_replay() {
        tokio::time::timeout(Duration::from_secs(5), async {
            let (env, shutdown) = environment();
            let mut guard = SecurityGuard::new(shutdown);
            guard.watch(&env.bus);
            env.publish(7_u32).await.unwrap();
            let (seen, mut rx) = mpsc::unbounded_channel();
            let start = Arc::new(Notify::new());
            let actor = Probe::spawn((env.clone(), seen, None, Some(start.clone())));
            guard.watch(&actor);
            let ready = guard.ready(&actor);
            tokio::pin!(ready);
            assert!(
                tokio::time::timeout(Duration::from_millis(20), &mut ready)
                    .await
                    .is_err()
            );
            start.notify_one();
            ready.await.unwrap();
            // Same rule as runtime cached-map replay: publish only after the startup barrier.
            env.publish(8_u32).await.unwrap();
            assert_eq!(rx.recv().await, Some(8));
            assert!(rx.try_recv().is_err());
        })
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn subscribe_waits_for_registration_behind_a_blocked_broker() {
        tokio::time::timeout(Duration::from_secs(5), async {
            let (env, shutdown) = environment();
            let mut guard = SecurityGuard::new(shutdown);
            guard.watch(&env.bus);
            let (seen, _rx) = mpsc::unbounded_channel();
            let entered = Arc::new(Notify::new());
            let release = Arc::new(Notify::new());
            let busy = Probe::spawn_with_mailbox(
                (
                    env.clone(),
                    seen,
                    Some((entered.clone(), release.clone())),
                    None,
                ),
                kameo::mailbox::bounded(1),
            );
            guard.watch(&busy);
            guard.ready(&busy).await.unwrap();
            env.publish(0_u32).await.unwrap();
            entered.notified().await;
            env.publish(1_u32).await.unwrap();
            env.publish(2_u32).await.unwrap();
            while busy.mailbox_sender().capacity() != Some(0) {
                tokio::task::yield_now().await;
            }
            let (seen, mut rx) = mpsc::unbounded_channel();
            let late = Probe::spawn((env.clone(), seen, None, None));
            guard.watch(&late);
            // A tell-based Register would return before the broker installs it and wrongly
            // allow this actor through its startup barrier while delivery is blocked.
            assert!(
                tokio::time::timeout(Duration::from_millis(20), guard.ready(&late))
                    .await
                    .is_err()
            );
            release.notify_one();
            guard.ready(&late).await.unwrap();
            env.publish(3_u32).await.unwrap();
            assert_eq!(rx.recv().await, Some(10_000));
            assert_eq!(rx.recv().await, Some(3));
            assert!(rx.try_recv().is_err());
        })
        .await
        .unwrap();
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn critical_subscriber_death_aborts_packet_pump_and_other_actors() {
        for kill in [false, true] {
            tokio::time::timeout(Duration::from_secs(5), async {
                let (env, shutdown) = environment();
                let mut guard = SecurityGuard::new(shutdown.clone());
                guard.watch(&env.bus);
                let dataplane = DataplaneActor::spawn(env.clone());
                guard.watch(&dataplane);
                guard.ready(&dataplane).await.unwrap();
                let pump = dataplane.ask(PacketPumpAbortHandle).await.unwrap();
                guard.packet_pump(pump.clone());
                let (_, up, down) = dataplane.ask(NewOverlayTransport).await.unwrap();
                let forwarder = ForwarderActor::spawn((env.clone(), Default::default(), up, down));
                guard.watch(&forwarder);
                guard.ready(&forwarder).await.unwrap();
                let (seen, _rx) = mpsc::unbounded_channel();
                let entered = Arc::new(Notify::new());
                let release = Arc::new(Notify::new());
                let pause = kill.then(|| (entered.clone(), release));
                let actor = Probe::spawn_with_mailbox(
                    (env.clone(), seen, pause, None),
                    kameo::mailbox::bounded(1),
                );
                guard.watch(&actor);
                guard.ready(&actor).await.unwrap();
                assert!(!pump.is_finished());
                if kill {
                    env.publish(0_u32).await.unwrap();
                    entered.notified().await;
                    for n in 1..=128_u32 {
                        env.publish(n).await.unwrap();
                    }
                    while actor.mailbox_sender().capacity() != Some(0) {
                        tokio::task::yield_now().await;
                    }
                    actor.kill();
                } else {
                    actor.stop_gracefully().await.unwrap();
                }
                forwarder.wait_for_shutdown().await;
                dataplane.wait_for_shutdown().await;
                env.bus.wait_for_shutdown().await;
                assert!(*shutdown.borrow());
                while !pump.is_finished() {
                    tokio::task::yield_now().await;
                }
            })
            .await
            .unwrap();
        }
    }

    struct FailedStart;

    impl Actor for FailedStart {
        type Args = ();
        type Error = Error;
        async fn on_start(_: (), _: ActorRef<Self>) -> Result<Self, Error> {
            Err(Error {
                kind: ErrorKind::ActorGone,
                target_actor: None,
                message_ty: None,
            })
        }
    }

    #[tokio::test]
    async fn failed_startup_cannot_pass_security_barrier() {
        let (env, shutdown) = environment();
        let mut guard = SecurityGuard::new(shutdown.clone());
        guard.watch(&env.bus);
        let actor = FailedStart::spawn(());
        guard.watch(&actor);
        assert_eq!(
            tokio::time::timeout(Duration::from_secs(5), guard.ready(&actor))
                .await
                .unwrap()
                .unwrap_err()
                .kind,
            ErrorKind::ActorGone
        );
        env.bus.wait_for_shutdown().await;
        assert!(*shutdown.borrow());
    }
}