taskvisor 0.9.0

In-process Tokio task supervisor: one job per key with queue/replace/reject admission, retries, graceful shutdown, and reliable final outcomes
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
//! Tests for registry completion and physical owner release.

use super::support::*;
use crate::controller::engine::state::SlotPhase;
use crate::controller::engine::{CompletionResult, Controller, RemovalResult, Submission};

async fn ordinary_completed_slot_waits_for_controller_completion_dispatch() {
    tokio::time::timeout(Duration::from_secs(10), async {
        let supervisor = Supervisor::new(crate::SupervisorConfig::default(), vec![]);
        let handle = supervisor.serve().expect("runtime startup");
        let bus = Bus::new(64);
        let mut events = bus.subscribe();
        let ctrl = Controller::new(ControllerConfig::default(), supervisor.core(), bus);
        let mut operations = tracked_operations(&ctrl);
        let owner = TaskId::next();
        let candidate = TaskId::next();
        let (release, released) = oneshot::channel();
        let released = Arc::new(StdMutex::new(Some(released)));
        let task_release = Arc::clone(&released);
        let owner_task: TaskRef = TaskFn::arc(move |_ctx| {
            let released = task_release
                .lock()
                .expect("release lock")
                .take()
                .expect("the ordinary owner runs once");
            async move {
                let _ = released.await;
                Ok(())
            }
        });
        let (owner_done, owner_outcome) = oneshot::channel();
        ctrl.handle_submission(
            Submission {
                id: owner,
                owned: owned_controller_spec(
                    ControllerSpec::drop_if_running(TaskSpec::once("ordinary-owner", owner_task))
                        .with_slot("ordinary-slot"),
                ),
                done: Some(owner_done),
            },
            &mut operations,
        )
        .await;
        let admission = operations
            .admissions
            .next()
            .await
            .expect("one real registry admission")
            .expect("admission operation did not panic");
        let completion = admission
            .decision
            .as_ref()
            .expect("the registry admitted the owner")
            .clone();
        ctrl.handle_admission_result(admission, &mut operations)
            .await;
        release.send(()).expect("the ordinary owner is waiting");
        assert!(matches!(
            owner_outcome.await.expect("ordinary owner outcome"),
            TaskOutcome::Completed
        ));
        completion.wait_physical().await;

        let slot = ctrl.slot("ordinary-slot").expect("owner remains in controller state");
        {
            let slot = slot.lock().await;
            assert_eq!(slot.owner_id(), Some(owner));
            assert!(matches!(slot.phase(), SlotPhase::Running { .. }));
            assert!(slot.queue.is_empty());
        }
        let candidate_starts = Arc::new(AtomicUsize::new(0));
        let starts = Arc::clone(&candidate_starts);
        let candidate_task: TaskRef = TaskFn::arc(move |_ctx| {
            starts.fetch_add(1, Ordering::AcqRel);
            async { Ok(()) }
        });
        let (candidate_done, candidate_outcome) = oneshot::channel();
        ctrl.handle_submission(
            Submission {
                id: candidate,
                owned: owned_controller_spec(
                    ControllerSpec::drop_if_running(TaskSpec::once(
                        "ordinary-candidate",
                        candidate_task,
                    ))
                    .with_slot("ordinary-slot"),
                ),
                done: Some(candidate_done),
            },
            &mut operations,
        )
        .await;
        let candidate_outcome = candidate_outcome.await.expect("candidate rejection");
        assert!(matches!(
            candidate_outcome,
            TaskOutcome::Rejected {
                kind: crate::RejectionKind::SlotBusy,
                ..
            }
        ));
        assert_eq!(candidate_starts.load(Ordering::Acquire), 0);
        let rejected = drain_events(&mut events)
            .into_iter()
            .find(|event| event.id == Some(candidate))
            .expect("typed candidate rejection event");
        assert_rejection_parity(&rejected, candidate, &candidate_outcome);

        let completed = operations
            .completions
            .next()
            .await
            .expect("one ready physical completion")
            .expect("completion operation did not panic");
        assert_eq!(completed.id, owner);
        assert_eq!(completed.slot_name.as_ref(), "ordinary-slot");
        assert_eq!(slot.lock().await.owner_id(), Some(owner));
        ctrl.handle_completion_result(completed, &mut operations)
            .await;
        assert!(ctrl.slot("ordinary-slot").is_none());
        assert!(operations.completions.is_empty());
        assert!(operations.admissions.is_empty());
        assert!(ctrl.state().watchers.is_empty());
        assert_eq!(candidate_starts.load(Ordering::Acquire), 0);
        eprintln!(
            "ordinary completion dispatch: Completed -> wait_physical ready -> Running(owner={owner:?}) -> Drop rejected(candidate={candidate:?}, starts=0) -> handle_completion_result -> slot absent"
        );
        handle.shutdown().await.expect("runtime shutdown");
    })
    .await
    .expect("ordinary completion dispatch test timed out");
}

#[tokio::test(flavor = "current_thread")]
async fn ordinary_completed_slot_waits_for_dispatch_current_thread() {
    ordinary_completed_slot_waits_for_controller_completion_dispatch().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn ordinary_completed_slot_waits_for_dispatch_multi_thread() {
    ordinary_completed_slot_waits_for_controller_completion_dispatch().await;
}

#[tokio::test]
async fn stale_completion_does_not_free_current_owner() {
    let ctrl = make_controller(ControllerConfig::default(), Bus::new(64));
    let current_id = TaskId::next();
    let stale_id = TaskId::next();
    let slot_arc = ctrl.get_or_create_slot("s");
    {
        let mut slot = slot_arc.lock().await;
        *slot = running_slot(current_id);
    }

    let mut operations = tracked_operations(&ctrl);
    ctrl.handle_completion_result(
        CompletionResult {
            id: stale_id,
            slot_name: Arc::from("s"),
        },
        &mut operations,
    )
    .await;

    let slot = slot_arc.lock().await;
    assert_eq!(slot.owner_id(), Some(current_id));
    assert!(matches!(slot.phase(), SlotPhase::Running { .. }));
}

#[tokio::test]
async fn removal_not_claimed_keeps_terminating_until_reliable_completion() {
    let sup = Supervisor::new(crate::SupervisorConfig::default(), vec![]);
    let bus = Bus::new(64);
    let mut events = bus.subscribe();
    let ctrl = Controller::new(ControllerConfig::default(), sup.core(), bus);
    let owner = TaskId::next();
    let queued = TaskId::next();
    let slot_arc = ctrl.get_or_create_slot("s");
    {
        let mut slot = slot_arc.lock().await;
        *slot = terminating_slot(owner);
        slot.queue
            .push_back(pending(queued, waiting_spec("after-unclaimed-removal")));
    }

    ctrl.handle_removal_result(RemovalResult {
        id: owner,
        slot_name: Arc::from("s"),
        decision: Ok(false),
    })
    .await;

    {
        let slot = slot_arc.lock().await;
        assert_eq!(slot.owner_id(), Some(owner));
        assert!(matches!(slot.phase(), SlotPhase::Terminating { .. }));
        assert_eq!(slot.queue.front().map(|pending| pending.id), Some(queued));
    }
    assert!(
        events.try_recv().is_err(),
        "Ok(false) is not a removal failure diagnostic"
    );

    let mut operations = tracked_operations(&ctrl);
    ctrl.handle_completion_result(
        CompletionResult {
            id: owner,
            slot_name: Arc::from("s"),
        },
        &mut operations,
    )
    .await;

    {
        let slot = slot_arc.lock().await;
        assert_eq!(slot.owner_id(), Some(queued));
        assert!(matches!(
            slot.phase(),
            SlotPhase::Admitting { owner, .. } if owner == queued
        ));
        assert!(slot.queue.is_empty());
    }
    assert_eq!(operations.admissions.len(), 1);
    abort_and_drain(&mut operations.admissions).await;
}

#[tokio::test]
async fn removal_error_preserves_owner_and_queue_and_emits_one_diagnostic() {
    let bus = Bus::new(64);
    let mut events = bus.subscribe();
    let ctrl = make_controller(ControllerConfig::default(), bus);
    let owner = TaskId::next();
    let queued = TaskId::next();
    let slot_arc = ctrl.get_or_create_slot("s");
    {
        let mut slot = slot_arc.lock().await;
        *slot = terminating_slot(owner);
        slot.queue
            .push_back(pending(queued, waiting_spec("after-failed-removal")));
    }

    ctrl.handle_removal_result(RemovalResult {
        id: owner,
        slot_name: Arc::from("s"),
        decision: Err(RuntimeError::CommandQueueFull),
    })
    .await;

    let event = events
        .try_recv()
        .expect("the current owner's removal error must be observable");
    assert_eq!(event.kind, EventKind::RuntimeFailure);
    assert_eq!(event.id, Some(owner));
    assert_eq!(event.task.as_deref(), Some("controller"));
    assert!(event.reason.as_deref().is_some_and(|reason| {
        reason.starts_with("remove_failed slot=s:") && reason.contains("queue is full")
    }));
    assert!(
        events.try_recv().is_err(),
        "one failed result must publish exactly one diagnostic"
    );

    let slot = slot_arc.lock().await;
    assert_eq!(slot.owner_id(), Some(owner));
    assert!(matches!(slot.phase(), SlotPhase::Terminating { .. }));
    assert_eq!(slot.queue.front().map(|pending| pending.id), Some(queued));
}

#[tokio::test]
async fn stale_removal_error_does_not_publish_or_mutate_new_owner() {
    let bus = Bus::new(64);
    let mut events = bus.subscribe();
    let ctrl = make_controller(ControllerConfig::default(), bus);
    let stale = TaskId::next();
    let current = TaskId::next();
    let queued = TaskId::next();
    let slot_arc = ctrl.get_or_create_slot("s");
    {
        let mut slot = slot_arc.lock().await;
        *slot = running_slot(current);
        slot.queue
            .push_back(pending(queued, waiting_spec("new-owner-queued")));
    }

    ctrl.handle_removal_result(RemovalResult {
        id: stale,
        slot_name: Arc::from("s"),
        decision: Err(RuntimeError::CommandQueueFull),
    })
    .await;

    assert!(events.try_recv().is_err());
    let slot = slot_arc.lock().await;
    assert_eq!(slot.owner_id(), Some(current));
    assert!(matches!(slot.phase(), SlotPhase::Running { .. }));
    assert_eq!(slot.queue.front().map(|pending| pending.id), Some(queued));
}

#[tokio::test]
async fn duplicate_completion_does_not_start_queued_owner_twice() {
    let sup = Supervisor::new(crate::SupervisorConfig::default(), vec![]);
    let ctrl = Controller::new(ControllerConfig::default(), sup.core(), Bus::new(64));
    let completed_id = TaskId::next();
    let next_id = TaskId::next();
    let slot_arc = ctrl.get_or_create_slot("s");
    {
        let mut slot = slot_arc.lock().await;
        *slot = running_slot(completed_id);
        slot.queue
            .push_back(pending(next_id, waiting_spec("duplicate-completion-next")));
    }

    let mut operations = tracked_operations(&ctrl);
    for _ in 0..2 {
        ctrl.handle_completion_result(
            CompletionResult {
                id: completed_id,
                slot_name: Arc::from("s"),
            },
            &mut operations,
        )
        .await;
    }

    let slot = slot_arc.lock().await;
    assert_eq!(slot.owner_id(), Some(next_id));
    assert!(matches!(
        slot.phase(),
        SlotPhase::Admitting { owner, .. } if owner == next_id
    ));
    assert!(slot.queue.is_empty());
    assert_eq!(
        operations.admissions.len(),
        1,
        "a duplicate completion must not commit the queued Add twice"
    );
    drop(slot);
    abort_and_drain(&mut operations.admissions).await;
}

#[tokio::test(flavor = "current_thread")]
async fn reliable_completion_reuses_task_name_without_task_removed() {
    let sup = Supervisor::new(crate::SupervisorConfig::default(), vec![]);
    let handle = sup.serve().expect("runtime startup");
    let ctrl = Controller::new(ControllerConfig::default(), sup.core(), Bus::new(1));
    let token = CancellationToken::new();
    let runner = start_controller_loop(&ctrl, &token).await;

    let log = Arc::new(StdMutex::new(Vec::new()));
    let (release, released) = oneshot::channel();
    let released = Arc::new(StdMutex::new(Some(released)));
    let first_log = Arc::clone(&log);
    let first_release = Arc::clone(&released);
    let first: TaskRef = TaskFn::arc(move |_ctx: TaskContext| {
        let released = first_release
            .lock()
            .expect("release lock poisoned")
            .take()
            .expect("the first task runs once");
        let log = Arc::clone(&first_log);
        async move {
            let _ = released.await;
            log.lock().expect("log lock poisoned").push("first");
            Ok(())
        }
    });
    let second_log = Arc::clone(&log);
    let second: TaskRef = TaskFn::arc(move |_ctx: TaskContext| {
        let log = Arc::clone(&second_log);
        async move {
            log.lock().expect("log lock poisoned").push("second");
            Ok(())
        }
    });

    let (first_id, first_outcome) = ctrl
        .handle()
        .submit_and_watch(
            ControllerSpec::queue(TaskSpec::once("same-runtime-name", first)).with_slot("s"),
        )
        .await
        .expect("the first submission must enter controller intake");
    assert!(
        poll_until(Duration::from_secs(2), || async {
            let Some(slot) = ctrl.slot("s") else {
                return false;
            };
            let slot = slot.lock().await;
            slot.owner_id() == Some(first_id) && matches!(slot.phase(), SlotPhase::Running { .. })
        })
        .await,
        "the first task must own the slot before queueing the second"
    );

    let (second_id, second_outcome) = ctrl
        .handle()
        .submit_and_watch(
            ControllerSpec::queue(TaskSpec::once("same-runtime-name", second)).with_slot("s"),
        )
        .await
        .expect("the second submission must enter controller intake");
    assert!(
        poll_until(Duration::from_secs(2), || async {
            let Some(slot) = ctrl.slot("s") else {
                return false;
            };
            slot.lock().await.queue.front().map(|pending| pending.id) == Some(second_id)
        })
        .await,
        "the second task must wait behind the first"
    );

    release.send(()).expect("the first task is waiting");
    let first_outcome = tokio::time::timeout(Duration::from_secs(2), first_outcome)
        .await
        .expect("the first outcome must arrive")
        .expect("the registry must send the first outcome");
    let second_outcome = tokio::time::timeout(Duration::from_secs(2), second_outcome)
        .await
        .expect("reliable completion must start the queued task")
        .expect("the registry must send the second outcome");
    assert!(matches!(first_outcome, TaskOutcome::Completed));
    assert!(matches!(second_outcome, TaskOutcome::Completed));
    assert_eq!(
        log.lock().expect("log lock poisoned").as_slice(),
        ["first", "second"]
    );
    assert!(
        poll_until(Duration::from_secs(2), || async {
            ctrl.slot("s").is_none()
        })
        .await,
        "the empty slot must be collected after the second completion"
    );

    stop_controller_loop(token, runner).await;
    let _ = handle.shutdown().await;
}