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
//! THE SHUTDOWN REGISTRY: who will be running when the launch returns, counted exactly.
//!
//! It is built at WIRING and for one reason: that is the moment the set of running-to-be parties is
//! known. The create walk builds values and does not know which of them get an engine; the ignition
//! that follows starts them one at a time and would be counting as it went. The wiring walk stands
//! between the two holding the whole list, so `N` is a fact rather than a running total, and the
//! shutdown report can name exactly who did not answer.
//!
//! # Two handles per party, and which way each one points
//!
//! - the WIND-DOWN, said by the framework and heard by the party. One slot per party, capacity one:
//! *stop taking new work*, which carries nothing beyond itself.
//! - the COMPLETION, fired by the party and collected by the framework. One oneshot per party: *I
//! have settled my affairs*. A party that drops without firing has NOT settled, and the deadline
//! report is what says so — the absence is the signal, so nothing here has to invent a timeout.
//!
//! [`Enrolled`] is a party's own pair and [`Party`] is the framework's; registering mints both at
//! once, which is what makes them pair by construction rather than by a caller keeping two lists
//! parallel.
//!
//! # The completion also carries the GOODBYE, and that is the whole of the teardown surgery
//!
//! A node's value is not the launch's to hold: it was moved onto an engine and it never migrates.
//! So the one moment the chain can reach it is the moment the body has finished with it — which is
//! this completion, and [`Settlement`] is what it carries. Two roads, and the ENGINE decides which:
//!
//! - a `Send` value on the async engine boxes its own goodbye and SHIPS it
//! ([`Settlement::Boxed`]). The erasure happens at the only place that still knows the type, so
//! the chain the handle walks carries no generics at all.
//! - a `!Send` value on the thread engine cannot ride a channel, and neither can its future. So it
//! STAYS: the thread parks at settle ([`Enrolled::settle_parked`]), the chain says *your turn* in
//! its own order, and only the OUTCOME — which is `Send` — comes back
//! ([`Settlement::Parked`]).
//!
//! One chain, two execution sites. The cost of the second is one turn-signal round trip per parked
//! node on the shutdown path, and what it buys is the nodes teardown exists for: a model context, an
//! FFI session, a device handle — every one of them a value that cannot cross a thread.
//!
//! # Why the announcement is a slot per party rather than a broadcast
//!
//! The ruled shape is one broadcast plus a subscriber count known at launch. This crate's transport
//! is `flume`, which is MPMC — a message reaches ONE receiver — and the crate names no async runtime
//! to borrow a broadcast primitive from. So the announcement is one capacity-one slot per party and
//! the broadcast is the walk over them; the subscriber count the ruling asks for is [`Shutdown::len`],
//! exact by construction. Nothing about the semantics differs: every party hears it, once, and a
//! second announcement coalesces into the first.
//!
//! # The party also carries whether an ENGINE was ever put under it
//!
//! [`Party::ignited`] is the third fact, and it exists for the failed-launch UNWIND: winding down
//! *the already-ignited set* means the framework has to be able to NAME that set, and mid-walk the
//! registry holds parties in both states at once — the ones whose enrolment has reached an engine
//! and the ones whose enrolment is still in the caller's hands, waiting for a walk that will now
//! never reach them. Waiting for the second kind would be waiting for something that never began.
//!
//! It is a mark on the ENROLMENT rather than a count kept by the ignition, because both engines set
//! it and only one of them is a thing the ignition keeps: a thread body it holds a handle to, and an
//! async body it composed and handed to somebody else's runtime.
//!
//! # A party FINISHES once, and the registry is woken exactly once for it
//!
//! The two handles above are LOOKS, so a shutdown that wants to wait rather than spin needs
//! somewhere to park. That is the third channel: one shared bell every enrolment rings as it goes,
//! whichever way it went — [`settle`](Enrolled::settle) fires the completion and then the enrolment
//! drops, and a party that panicked drops without firing. So [`Shutdown::park`] wakes once per party
//! FINISHED and the per-party looks say which kind of finish it was; a ring carries no identity
//! because it does not need one, the looks being the answer to *who*.
//!
//! # What is NOT here
//!
//! The ORCHESTRATION. Announcing to every party, waiting for the N completions against a deadline,
//! aborting what did not answer and naming the unanswered in three classes — un-settled nodes,
//! un-flushed engines, un-torn resources — is the ignition round's, which is also where the parties
//! that are not nodes (a change intake, a query arm, a poll ticker) enrol through
//! [`register`](Shutdown::register). This module is the registry those steps spend.
use Duration;
use ;
use Utf8Bytes;
use crate;
use ;
/// Every party that will be running when the launch returns.
///
/// Built by the wiring walk, one entry per party it wires, and handed on inside the assembly the
/// ignition starts. Its length is the ruled subscriber count: the walk knew who would run, so the
/// number of oneshots to wait for is not estimated anywhere.
/// The shared FINISH bell: one ring per party that finished, whichever way it finished.
///
/// UNBOUNDED, and that is the one thing it has to be: a ring happens while an enrolment is being
/// dropped, and a drop that could block would turn a party's own finish into a wait on whoever is
/// collecting. There is one ring per party ever, so the bound it declines is a bound on N.
/// A RING ON DEMAND, held by a party that is not an enrolment — see [`Shutdown::ringer`].
///
/// [`Finished`] one shape over, and the difference is WHEN: that one rings as it is dropped, which
/// is what makes a party that panicked wake the registry all the same; this one rings when it is
/// told to, because what it reports is not a finish.
pub >);
/// One enrolment's own ringer, which rings as the enrolment goes.
///
/// A `Drop` rather than a call, because the two ways a party can finish are a call and a drop:
/// [`Enrolled::settle`] fires the completion and then this rings as the enrolment falls out of
/// scope, and a party that panicked rings only this. So a waiter is woken exactly once per party
/// however that party ended, and [`Party::settled`] is what says which ending it was.
>);
/// One registered party, from the FRAMEWORK's side: how to tell it to wind down, and how to learn
/// that it has.
/// One registered party's OWN half: the announcement it watches, and the completion it fires.
///
/// Handed out by [`Shutdown::register`] and held by whoever runs the party — for a node, by the
/// ignition step that spawns its engine, which fires the completion when the loop returns.
/// What a party's completion carries BESIDE the fact of it — the goodbye seat.
///
/// See the module note for the two roads and why the engine picks between them.
pub
/// A goodbye that stayed on the thread that made it: the way to tell it to go, and the way to hear
/// how it went.
///
/// Minted by [`Enrolled::settle_parked`] and spent by the teardown chain. Dropping one without
/// spending it is what tells the parked thread that nobody is coming — the whole abandonment
/// protocol, and it costs no timeout anywhere.
pub
/// One enrolment's IGNITED mark, held by the ignition step that hands the enrolment to an engine.
///
/// Minted by [`Enrolled::mark`] and set once the engine exists.
pub ;