async-runtime 0.3.1

A priority-aware native async runtime for the smol ecosystem with host-driven local domains
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
use crate::error::{ShutdownError, ShutdownOutcome, SpawnError};
use crate::priority::{Priority, PriorityWeights};
use crate::scheduler::Scheduler;
use crate::task::Task;
use crate::worker;
use async_task::Builder as TaskBuilder;
use std::future::Future;
use std::io;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex, Weak};
use std::thread::{self, JoinHandle, ThreadId};
use std::time::Duration;

pub struct RuntimeBuilder {
    worker_threads: NonZeroUsize,
    weights: PriorityWeights,
}

impl RuntimeBuilder {
    /// Creates a builder for an explicit, non-zero worker count.
    pub fn new(worker_threads: NonZeroUsize) -> Self {
        Self {
            worker_threads,
            weights: PriorityWeights::default(),
        }
    }
    /// Replaces the default `8:4:1` priority weights.
    #[must_use]
    pub fn priority_weights(mut self, weights: PriorityWeights) -> Self {
        self.weights = weights;
        self
    }
    /// Starts all worker threads and returns the owning runtime handle.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if any worker thread cannot be created. Workers
    /// created earlier in the same build attempt are stopped and joined.
    pub fn build(self) -> io::Result<Runtime> {
        let (scheduler, worker_queues) = Scheduler::new(self.worker_threads.get());
        let state = Arc::new(RuntimeState {
            scheduler,
            gate: Mutex::new(Gate::Running),
            accepted_tasks: AtomicUsize::new(0),
            drain_lock: Mutex::new(()),
            drained: Condvar::new(),
            worker_ids: Mutex::new(Vec::with_capacity(self.worker_threads.get())),
            #[cfg(test)]
            admission_pause: Mutex::new(None),
            #[cfg(test)]
            last_completion_pause: Mutex::new(None),
        });
        let mut workers = Vec::with_capacity(self.worker_threads.get());
        for (number, queues) in worker_queues.into_iter().enumerate() {
            let worker_state = Arc::clone(&state);
            match thread::Builder::new()
                .name(format!("async-runtime-{number}"))
                .spawn(move || worker::run(&worker_state, number, queues, self.weights))
            {
                Ok(handle) => workers.push(handle),
                Err(error) => {
                    state.request_stop();
                    for handle in workers {
                        let _ = handle.join();
                    }
                    return Err(error);
                }
            }
        }
        Ok(Runtime {
            state,
            workers: Mutex::new(workers),
            closed: false,
        })
    }
}

pub struct Runtime {
    pub(crate) state: Arc<RuntimeState>,
    workers: Mutex<Vec<JoinHandle<()>>>,
    closed: bool,
}

impl Runtime {
    /// Returns a weak capability for submitting work to this runtime.
    pub fn spawner(&self) -> Spawner {
        Spawner {
            state: Arc::downgrade(&self.state),
        }
    }
    /// Submits a task to one priority queue.
    ///
    /// # Errors
    ///
    /// Returns [`SpawnError::Closed`] after shutdown begins.
    ///
    /// A successful return means the task was admitted into the runtime's
    /// lifecycle. A concurrent forced or timed shutdown may cancel it before
    /// its first poll; graceful shutdown still waits for it to reach a terminal
    /// state.
    pub fn spawn<F, T>(&self, priority: Priority, future: F) -> Result<Task<T>, SpawnError>
    where
        F: Future<Output = T> + Send + 'static,
        T: Send + 'static,
    {
        self.state.spawn(priority, future)
    }

    /// Returns a relaxed snapshot of scheduler observability counters.
    #[cfg(feature = "stats")]
    pub fn stats(&self) -> crate::RuntimeStats {
        self.state.scheduler.stats()
    }
    /// Rejects new tasks, drains every accepted task, and joins all workers.
    ///
    /// # Errors
    ///
    /// Returns [`ShutdownError::CalledFromWorker`] when invoked by this
    /// runtime's worker, or [`ShutdownError::WorkerPanicked`] if a joined
    /// worker panicked.
    pub fn shutdown_graceful(mut self) -> Result<(), ShutdownError> {
        if self.state.is_current_worker() {
            self.state.begin_close();
            self.state.request_stop();
            self.closed = true;
            let _ = self.join_workers();
            self.state.finish_close();
            return Err(ShutdownError::CalledFromWorker);
        }
        self.state.begin_close();
        self.state.wait_for_drain();
        self.state.request_stop();
        self.closed = true;
        let result = self.join_workers();
        self.state.finish_close();
        result
    }
    /// Drains accepted tasks until `timeout`, then cancels the remainder.
    ///
    /// # Errors
    ///
    /// Returns [`ShutdownError::CalledFromWorker`] when invoked by this
    /// runtime's worker, or [`ShutdownError::WorkerPanicked`] if a joined
    /// worker panicked.
    pub fn shutdown_timeout(mut self, timeout: Duration) -> Result<ShutdownOutcome, ShutdownError> {
        if self.state.is_current_worker() {
            self.state.begin_close();
            self.state.request_stop();
            self.closed = true;
            let _ = self.join_workers();
            self.state.finish_close();
            return Err(ShutdownError::CalledFromWorker);
        }
        self.state.begin_close();
        let outcome = if self.state.wait_for_drain_timeout(timeout) {
            ShutdownOutcome::Completed
        } else {
            ShutdownOutcome::TimedOut {
                remaining_tasks: self.state.accepted_tasks.load(Ordering::Acquire),
            }
        };
        self.state.request_stop();
        self.closed = true;
        let result = self.join_workers();
        self.state.finish_close();
        result?;
        Ok(outcome)
    }
    /// Cancels remaining work and joins all workers.
    ///
    /// # Errors
    ///
    /// Returns [`ShutdownError::CalledFromWorker`] when invoked by this
    /// runtime's worker, or [`ShutdownError::WorkerPanicked`] if a joined
    /// worker panicked.
    pub fn shutdown_now(mut self) -> Result<(), ShutdownError> {
        let called_from_worker = self.state.is_current_worker();
        self.state.begin_close();
        self.state.request_stop();
        self.closed = true;
        let result = self.join_workers();
        self.state.finish_close();
        if called_from_worker {
            Err(ShutdownError::CalledFromWorker)
        } else {
            result
        }
    }
    fn join_workers(&self) -> Result<(), ShutdownError> {
        let current = thread::current().id();
        let workers = {
            let mut workers = self.workers.lock().expect("runtime worker list poisoned");
            std::mem::take(&mut *workers)
        };
        let mut panicked = false;
        for worker in workers {
            if worker.thread().id() == current {
                continue;
            }
            if worker.join().is_err() {
                panicked = true;
            }
        }
        if panicked {
            Err(ShutdownError::WorkerPanicked)
        } else {
            Ok(())
        }
    }
}

impl Drop for Runtime {
    fn drop(&mut self) {
        if !self.closed {
            self.state.begin_close();
            self.state.request_stop();
            let _ = self.join_workers();
            self.state.finish_close();
            self.closed = true;
        }
    }
}

#[derive(Clone)]
pub struct Spawner {
    state: Weak<RuntimeState>,
}
impl Spawner {
    /// Submits a task to one priority queue.
    ///
    /// A successful return means the task was admitted into the runtime's
    /// lifecycle. A concurrent forced or timed shutdown may cancel it before
    /// its first poll; graceful shutdown still waits for it to reach a terminal
    /// state.
    ///
    /// # Errors
    ///
    /// Returns [`SpawnError::Closed`] if the runtime no longer exists or has
    /// begun shutting down.
    pub fn spawn<F, T>(&self, priority: Priority, future: F) -> Result<Task<T>, SpawnError>
    where
        F: Future<Output = T> + Send + 'static,
        T: Send + 'static,
    {
        self.state
            .upgrade()
            .ok_or(SpawnError::Closed)?
            .spawn(priority, future)
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Gate {
    Running,
    Closing,
    Closed,
}

/// Shared worker state. Its gate serializes task acceptance with transition to closing.
pub(crate) struct RuntimeState {
    pub(crate) scheduler: Arc<Scheduler>,
    gate: Mutex<Gate>,
    pub(crate) accepted_tasks: AtomicUsize,
    drain_lock: Mutex<()>,
    drained: Condvar,
    worker_ids: Mutex<Vec<ThreadId>>,
    #[cfg(test)]
    admission_pause: Mutex<Option<Arc<TestPause>>>,
    #[cfg(test)]
    last_completion_pause: Mutex<Option<Arc<TestPause>>>,
}

impl RuntimeState {
    fn spawn<F, T>(self: &Arc<Self>, priority: Priority, future: F) -> Result<Task<T>, SpawnError>
    where
        F: Future<Output = T> + Send + 'static,
        T: Send + 'static,
    {
        let gate = self.gate.lock().expect("runtime lifecycle gate poisoned");
        if *gate != Gate::Running {
            return Err(SpawnError::Closed);
        }
        self.accepted_tasks.fetch_add(1, Ordering::AcqRel);
        // This token begins at admission, not at the first poll. It is moved
        // into the task immediately so construction unwind, cancellation,
        // forced shutdown, and normal completion all retire the admission
        // exactly once.
        let completion = CompletionGuard {
            // Tasks are owned by the scheduler inside this state. Keeping only a weak
            // reference here is essential: a queued task must not keep the scheduler
            // (and therefore itself) alive during shutdown_now or Runtime::drop.
            state: Arc::downgrade(self),
        };
        drop(gate);
        #[cfg(test)]
        self.pause_after_admission();
        let tracked = async move {
            let _completion = completion;
            future.await
        };
        let scheduler = Arc::downgrade(&self.scheduler);
        let schedule = move |runnable| {
            if let Some(scheduler) = scheduler.upgrade() {
                scheduler.schedule(priority, runnable);
            }
        };
        let (runnable, task) = TaskBuilder::new()
            .propagate_panic(true)
            .spawn(|()| tracked, schedule);
        self.scheduler
            .record_spawn(self.scheduler.is_current_worker());
        // A successful return means the runtime owns the queued task or its
        // cancellation cleanup. Initial scheduling follows the same local vs
        // global routing rule as every later wake.
        runnable.schedule();
        Ok(Task::direct(task))
    }
    pub(crate) fn register_worker(&self, id: ThreadId) {
        self.worker_ids
            .lock()
            .expect("runtime worker id list poisoned")
            .push(id);
    }
    fn is_current_worker(&self) -> bool {
        self.worker_ids
            .lock()
            .expect("runtime worker id list poisoned")
            .contains(&thread::current().id())
    }
    fn begin_close(&self) {
        let mut gate = self.gate.lock().expect("runtime lifecycle gate poisoned");
        if *gate == Gate::Running {
            *gate = Gate::Closing;
        }
    }
    fn finish_close(&self) {
        *self.gate.lock().expect("runtime lifecycle gate poisoned") = Gate::Closed;
    }
    pub(crate) fn request_stop(&self) {
        self.scheduler.stop();
    }
    fn wait_for_drain(&self) {
        let guard = self.drain_lock.lock().expect("runtime drain lock poisoned");
        drop(
            self.drained
                .wait_while(guard, |()| self.accepted_tasks.load(Ordering::Acquire) != 0)
                .expect("runtime drain lock poisoned"),
        );
    }
    fn wait_for_drain_timeout(&self, timeout: Duration) -> bool {
        let guard = self.drain_lock.lock().expect("runtime drain lock poisoned");
        let (guard, _) = self
            .drained
            .wait_timeout_while(guard, timeout, |()| {
                self.accepted_tasks.load(Ordering::Acquire) != 0
            })
            .expect("runtime drain lock poisoned");
        let drained = self.accepted_tasks.load(Ordering::Acquire) == 0;
        drop(guard);
        drained
    }
    fn complete_task(&self) {
        let previous = self.accepted_tasks.fetch_sub(1, Ordering::AcqRel);
        assert!(previous > 0, "runtime accepted task count underflow");
        if previous == 1 {
            #[cfg(test)]
            self.pause_before_last_completion_notify();
            // The waiter checks the zero predicate while holding this same
            // mutex. Locking before notify closes both interleavings: either
            // the waiter observes zero, or it has atomically begun waiting.
            let _guard = self.drain_lock.lock().expect("runtime drain lock poisoned");
            self.drained.notify_all();
        }
    }

    #[cfg(test)]
    fn pause_after_admission(&self) {
        let pause = self
            .admission_pause
            .lock()
            .expect("runtime test hook poisoned")
            .clone();
        if let Some(pause) = pause {
            pause.pause();
        }
    }

    #[cfg(test)]
    fn pause_before_last_completion_notify(&self) {
        let pause = self
            .last_completion_pause
            .lock()
            .expect("runtime test hook poisoned")
            .clone();
        if let Some(pause) = pause {
            pause.pause();
        }
    }
}

struct CompletionGuard {
    state: Weak<RuntimeState>,
}
impl Drop for CompletionGuard {
    fn drop(&mut self) {
        // Graceful shutdown keeps the runtime state alive, so every accepted task
        // contributes to its drain count. During forced teardown the state may
        // already be gone; there is then no waiter left to notify.
        if let Some(state) = self.state.upgrade() {
            state.complete_task();
        }
    }
}

#[cfg(test)]
struct TestPause {
    reached: std::sync::Barrier,
    released: std::sync::Barrier,
}

#[cfg(test)]
impl TestPause {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            reached: std::sync::Barrier::new(2),
            released: std::sync::Barrier::new(2),
        })
    }

    fn pause(&self) {
        self.reached.wait();
        self.released.wait();
    }

    fn wait_until_reached(&self) {
        self.reached.wait();
    }

    fn release(&self) {
        self.released.wait();
    }
}

#[cfg(test)]
mod tests {
    use super::{Gate, RuntimeBuilder, ShutdownOutcome, TestPause};
    use crate::Priority;
    use futures_lite::future;
    use std::num::NonZeroUsize;
    use std::sync::atomic::Ordering;
    use std::sync::{mpsc, Arc};
    use std::time::Duration;

    fn runtime() -> super::Runtime {
        RuntimeBuilder::new(NonZeroUsize::new(1).expect("non-zero worker count"))
            .build()
            .expect("runtime builds")
    }

    fn wait_until_not_running(state: &super::RuntimeState) {
        for _ in 0..10_000 {
            if *state.gate.lock().expect("runtime lifecycle gate poisoned") != Gate::Running {
                return;
            }
            std::thread::yield_now();
        }
        panic!("shutdown did not close the admission gate");
    }

    #[test]
    fn graceful_shutdown_waits_for_admitted_task_before_initial_schedule() {
        let runtime = runtime();
        let state = Arc::clone(&runtime.state);
        let pause = TestPause::new();
        *state.admission_pause.lock().expect("test hook poisoned") = Some(Arc::clone(&pause));
        let spawner = runtime.spawner();
        let (ran_tx, ran_rx) = mpsc::channel();
        let producer = std::thread::spawn(move || {
            spawner
                .spawn(Priority::Normal, async move {
                    ran_tx.send(()).expect("test receiver remains alive");
                })
                .expect("admitted spawn succeeds")
                .detach();
        });
        pause.wait_until_reached();

        let (shutdown_tx, shutdown_rx) = mpsc::channel();
        let shutdown = std::thread::spawn(move || {
            shutdown_tx
                .send(runtime.shutdown_graceful())
                .expect("test receiver remains alive");
        });
        wait_until_not_running(&state);
        assert!(shutdown_rx.try_recv().is_err());

        pause.release();
        producer.join().expect("producer does not panic");
        shutdown_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("graceful shutdown finishes after scheduling")
            .expect("graceful shutdown succeeds");
        shutdown.join().expect("shutdown thread does not panic");
        ran_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("admitted task runs before graceful shutdown returns");
        assert_eq!(state.accepted_tasks.load(Ordering::Acquire), 0);
    }

    #[test]
    fn forced_shutdown_cancels_admitted_task_before_initial_schedule() {
        let runtime = runtime();
        let state = Arc::clone(&runtime.state);
        let pause = TestPause::new();
        *state.admission_pause.lock().expect("test hook poisoned") = Some(Arc::clone(&pause));
        let spawner = runtime.spawner();
        let (task_tx, task_rx) = mpsc::channel();
        let producer = std::thread::spawn(move || {
            let task = spawner
                .spawn(Priority::Normal, async { 7_u8 })
                .expect("spawn was admitted before shutdown");
            task_tx.send(task).expect("test receiver remains alive");
        });
        pause.wait_until_reached();

        runtime.shutdown_now().expect("forced shutdown succeeds");
        pause.release();
        producer.join().expect("producer does not panic");
        let task = task_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("spawn returns its cancelled task handle");
        assert_eq!(future::block_on(task.fallible()), None);
        assert_eq!(state.accepted_tasks.load(Ordering::Acquire), 0);
    }

    #[test]
    fn timed_shutdown_cancels_admitted_task_before_initial_schedule() {
        let runtime = runtime();
        let state = Arc::clone(&runtime.state);
        let pause = TestPause::new();
        *state.admission_pause.lock().expect("test hook poisoned") = Some(Arc::clone(&pause));
        let spawner = runtime.spawner();
        let (task_tx, task_rx) = mpsc::channel();
        let producer = std::thread::spawn(move || {
            let task = spawner
                .spawn(Priority::Normal, async { 9_u8 })
                .expect("spawn was admitted before shutdown");
            task_tx.send(task).expect("test receiver remains alive");
        });
        pause.wait_until_reached();

        assert!(matches!(
            runtime
                .shutdown_timeout(Duration::ZERO)
                .expect("timed shutdown succeeds"),
            ShutdownOutcome::TimedOut { remaining_tasks: 1 }
        ));
        pause.release();
        producer.join().expect("producer does not panic");
        let task = task_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("spawn returns its cancelled task handle");
        assert_eq!(future::block_on(task.fallible()), None);
        assert_eq!(state.accepted_tasks.load(Ordering::Acquire), 0);
    }

    #[test]
    fn waiter_observes_zero_when_last_completion_precedes_notification() {
        let runtime = runtime();
        let state = Arc::clone(&runtime.state);
        let pause = TestPause::new();
        *state
            .last_completion_pause
            .lock()
            .expect("test hook poisoned") = Some(Arc::clone(&pause));
        let task = runtime
            .spawn(Priority::Normal, async {})
            .expect("spawn succeeds");
        pause.wait_until_reached();
        assert_eq!(state.accepted_tasks.load(Ordering::Acquire), 0);

        let (wait_tx, wait_rx) = mpsc::channel();
        let wait_state = Arc::clone(&state);
        let waiter = std::thread::spawn(move || {
            wait_state.wait_for_drain();
            wait_tx.send(()).expect("test receiver remains alive");
        });
        wait_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("waiter sees zero without needing the pending notification");

        pause.release();
        future::block_on(task);
        waiter.join().expect("waiter does not panic");
        runtime.shutdown_graceful().expect("shutdown succeeds");
    }
}