otel-arrow-dfe-engine 0.61.0

Async pipeline engine
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//! Extension lifecycle holder for an extension-hosting runtime.
//!
//! Owns spawned active/background extension tasks, shutdown senders,
//! and passive extension wrappers. Encapsulates "extensions start
//! first, shut down last" on the normal drain path.
//!
//! TODO: on internal node/extension failure the orderly drain
//! protocol is skipped; remaining data-path tasks are force-cancelled
//! by `LocalSet` drop, so the stop-last guarantee currently applies
//! only to the normal drain path. Fix in a follow-up PR by making
//! pipeline shutdown orchestrated.

use crate::context::ExtensionContext;
use crate::control::{ExtensionShutdownChannel, ShutdownPayload};
use crate::error::Error;
use crate::extension::ExtensionWrapper;
use crate::extension_monitor::{
    ExtensionKey, ExtensionLifecycleEvent, ExtensionMetricsMonitor, ExtensionOutcome,
};
use crate::terminal_state::TerminalMetricsDeadline;
use futures::FutureExt;
use futures::stream::{FuturesUnordered, StreamExt};
use otel_arrow_dfe_telemetry::otel_warn;
use otel_arrow_dfe_telemetry::registry::EntityKey;
use otel_arrow_dfe_telemetry::reporter::MetricsReporter;
use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
use tokio::task::{self, JoinError, JoinHandle, LocalSet};
use tokio::time::Instant as TokioInstant;

/// Cleanup window granted to extensions after the data path has drained.
pub(crate) const EXTENSION_SHUTDOWN_GRACE: Duration = Duration::from_secs(5);

/// Slack past `EXTENSION_SHUTDOWN_GRACE` before the runtime hard-stops draining.
pub(crate) const EXTENSION_SHUTDOWN_DRAIN_SLACK: Duration = Duration::from_millis(500);

const DEFAULT_SHUTDOWN_REASON: &str = "host data-path drained";

pub(crate) enum LifecycleEvent {
    Completion(Result<Result<(), Error>, JoinError>),
    MonitorTick(Instant),
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum LifecyclePhase {
    Running,
    ShuttingDown { deadline: Instant },
}

pub(crate) struct ExtensionLifecycle {
    futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>>,
    task_id_to_key: HashMap<task::Id, ExtensionKey>,
    shutdown_channels: Vec<(ExtensionKey, ExtensionShutdownChannel)>,
    _passive: Vec<ExtensionWrapper>,
    phase: LifecyclePhase,
    monitor: ExtensionMetricsMonitor,
    started_rx: mpsc::UnboundedReceiver<ExtensionKey>,
    // Keys we have not yet seen a spawn signal for. Each event (start signal
    // or task completion) calls `remove`, which is idempotent -- so a task
    // that signals and then completes can never under-count its slot.
    pending_starts: HashSet<ExtensionKey>,
    extension_readiness_probes: Vec<(ExtensionKey, crate::extension::ReadinessProbe)>,
}

impl ExtensionLifecycle {
    /// Spawn active+background extensions, stash passive ones, register each
    /// non-passive wrapper with `monitor`.
    pub fn spawn(
        extensions: Vec<(ExtensionWrapper, EntityKey)>,
        local_tasks: &LocalSet,
        metrics_reporter: MetricsReporter,
        terminal_metrics_deadline: TerminalMetricsDeadline,
        ext_ctx: &ExtensionContext,
        mut monitor: ExtensionMetricsMonitor,
    ) -> Self {
        let futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>> =
            FuturesUnordered::new();
        let mut task_id_to_key: HashMap<task::Id, ExtensionKey> = HashMap::new();
        let mut shutdown_channels: Vec<(ExtensionKey, ExtensionShutdownChannel)> = Vec::new();
        let mut passive = Vec::new();
        let mut pending_starts: HashSet<ExtensionKey> = HashSet::new();
        let mut extension_readiness_probes: Vec<(ExtensionKey, crate::extension::ReadinessProbe)> =
            Vec::new();
        let (started_tx, started_rx) = mpsc::unbounded_channel::<ExtensionKey>();

        for (mut ext_wrapper, entity_key) in extensions {
            if ext_wrapper.is_passive() {
                passive.push(ext_wrapper);
                continue;
            }
            let ext_id = ext_wrapper.name();
            let key = ExtensionKey::new(ext_id.clone(), ext_wrapper.variant());
            let control_sender = ext_wrapper.extension_control_sender();
            let shutdown_channel = ext_wrapper.take_shutdown_sender();
            let readiness_probe = ext_wrapper.take_readiness_probe();
            let telemetry_guard = ext_wrapper.take_telemetry_guard();
            monitor.register(ext_ctx, key.clone(), entity_key, control_sender.as_ref());
            if let Some(channel) = shutdown_channel {
                shutdown_channels.push((key.clone(), channel));
            }
            if let Some(probe) = readiness_probe {
                extension_readiness_probes.push((key.clone(), probe));
            }
            let ext_metrics_reporter = metrics_reporter.clone();
            let ext_terminal_metrics_deadline = terminal_metrics_deadline.clone();
            let task_key = key.clone();
            let started_tx = started_tx.clone();
            let fut = async move {
                let _ = started_tx.send(task_key.clone());
                let res = match ext_wrapper.start(ext_metrics_reporter.clone()).await {
                    Ok(terminal_state) => {
                        crate::runtime_pipeline::report_terminal_metrics(
                            &ext_metrics_reporter,
                            terminal_state,
                            &ext_terminal_metrics_deadline,
                        )
                        .await;
                        Ok(())
                    }
                    Err(e) => {
                        if let Err(flush_error) = ext_metrics_reporter
                            .flush_until(ext_terminal_metrics_deadline.get())
                            .await
                        {
                            otel_warn!(
                                "extension.metrics.flush.fail",
                                extension = task_key.id.as_ref(),
                                error = flush_error.to_string(),
                            );
                        }
                        otel_warn!(
                            "extension.task.error",
                            extension = task_key.id.as_ref(),
                            error = format!("{e}"),
                        );
                        Err(e)
                    }
                };
                drop(telemetry_guard);
                drop(control_sender);
                (task_key, res)
            };
            let handle = local_tasks.spawn_local(fut);
            let _ = task_id_to_key.insert(handle.id(), key.clone());
            futures.push(handle);
            let _ = pending_starts.insert(key);
        }
        // Drop the seed so `started_rx.recv()` returns None once all clones drop.
        drop(started_tx);

        Self {
            futures,
            task_id_to_key,
            shutdown_channels,
            _passive: passive,
            phase: LifecyclePhase::Running,
            monitor,
            started_rx,
            pending_starts,
            extension_readiness_probes,
        }
    }

    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.futures.is_empty()
    }

    /// Yields the next extension completion or monitor tick. Spawn-handshake
    /// signals on `started_rx` are drained silently -- they only exist to
    /// power `wait_all_spawned`.
    pub async fn next_event(&mut self) -> LifecycleEvent {
        loop {
            let shutdown_initiated = matches!(self.phase, LifecyclePhase::ShuttingDown { .. });
            let Self {
                futures,
                task_id_to_key,
                shutdown_channels,
                monitor,
                started_rx,
                pending_starts,
                ..
            } = self;
            if futures.is_empty() {
                tokio::select! {
                    biased;
                    Some(key) = started_rx.recv() => {
                        let _ = pending_starts.remove(&key);
                        continue;
                    }
                    now = monitor.next_tick() => return LifecycleEvent::MonitorTick(now),
                }
            }
            tokio::select! {
                biased;
                Some(key) = started_rx.recv() => {
                    let _ = pending_starts.remove(&key);
                    continue;
                }
                Some(joined) = futures.next() => {
                    if let Some(k) = Self::pending_key_for(&joined, task_id_to_key) {
                        let _ = pending_starts.remove(&k);
                    }
                    return LifecycleEvent::Completion(
                        Self::route_joined(monitor, task_id_to_key, shutdown_channels, shutdown_initiated, joined)
                    );
                }
                now = monitor.next_tick() => return LifecycleEvent::MonitorTick(now),
            }
        }
    }

    /// Returns once every non-passive extension has been polled at least once
    /// (its task body sent the spawn handshake). Tasks that complete or panic
    /// before signalling are routed through the normal completion path; the
    /// first surfaced error is returned. A task that signals and then
    /// completes (success or failure) before the barrier observes the
    /// completion is also surfaced as an error -- `Ok(())` upgrades to
    /// `ExtensionExitedBeforeShutdown` via `route_joined`.
    pub async fn wait_all_spawned(&mut self) -> Result<(), Error> {
        loop {
            let Self {
                futures,
                task_id_to_key,
                shutdown_channels,
                monitor,
                started_rx,
                pending_starts,
                ..
            } = self;

            // Surface any ready completion before considering the barrier
            // satisfied. During the barrier every completion is an error,
            // so a task that signaled and then died must not be masked by
            // pending_starts having already been cleared by the signal.
            if let Some(Some(joined)) = futures.next().now_or_never() {
                if let Some(k) = Self::pending_key_for(&joined, task_id_to_key) {
                    let _ = pending_starts.remove(&k);
                }
                match Self::route_joined(monitor, task_id_to_key, shutdown_channels, false, joined)
                {
                    Ok(Ok(())) => unreachable!(
                        "route_joined(shutdown_initiated=false) must upgrade Ok(()) to ExtensionExitedBeforeShutdown",
                    ),
                    Ok(Err(e)) => return Err(e),
                    Err(e) => {
                        return Err(Error::JoinTaskError {
                            is_canceled: e.is_cancelled(),
                            is_panic: e.is_panic(),
                            error: e.to_string(),
                        });
                    }
                }
            }

            if pending_starts.is_empty() {
                return Ok(());
            }
            tokio::select! {
                biased;
                Some(key) = started_rx.recv() => {
                    let _ = pending_starts.remove(&key);
                }
                Some(joined) = futures.next(), if !futures.is_empty() => {
                    if let Some(k) = Self::pending_key_for(&joined, task_id_to_key) {
                        let _ = pending_starts.remove(&k);
                    }
                    match Self::route_joined(monitor, task_id_to_key, shutdown_channels, false, joined) {
                        Ok(Ok(())) => unreachable!(
                            "route_joined(shutdown_initiated=false) must upgrade Ok(()) to ExtensionExitedBeforeShutdown",
                        ),
                        Ok(Err(e)) => return Err(e),
                        Err(e) => {
                            return Err(Error::JoinTaskError {
                                is_canceled: e.is_cancelled(),
                                is_panic: e.is_panic(),
                                error: e.to_string(),
                            });
                        }
                    }
                }
                else => return Ok(()),
            }
        }
    }

    /// Await every opted-in readiness probe in parallel, with each
    /// probe's own timeout. Returns on the first failure with a
    /// named-laggard error
    /// ([`Error::ExtensionReadinessSignallerDropped`] or
    /// [`Error::ExtensionReadinessTimeout`]).
    ///
    /// While probes are pending, extension task completions are watched too:
    /// an extension that exits or fails during the readiness window is
    /// surfaced as an error rather than letting the gate pass, mirroring
    /// the task-failure handling in [`wait_all_spawned`](Self::wait_all_spawned).
    pub async fn wait_all_ready(&mut self) -> Result<(), Error> {
        let probes = std::mem::take(&mut self.extension_readiness_probes);
        if probes.is_empty() {
            return Ok(());
        }

        let mut waiters = FuturesUnordered::new();
        for (key, probe) in probes {
            let timeout = probe.timeout();
            waiters.push(async move {
                let outcome = tokio::time::timeout(timeout, probe.wait_ready()).await;
                (key, timeout, outcome)
            });
        }

        while !waiters.is_empty() {
            let Self {
                futures,
                task_id_to_key,
                shutdown_channels,
                monitor,
                ..
            } = self;

            tokio::select! {
                biased;
                // A task completing before the gate is satisfied means an
                // extension died during startup; surface it instead of
                // starting data-path nodes against a lost extension.
                Some(joined) = futures.next(), if !futures.is_empty() => {
                    match Self::route_joined(monitor, task_id_to_key, shutdown_channels, false, joined) {
                        Ok(Ok(())) => unreachable!(
                            "route_joined(shutdown_initiated=false) must upgrade Ok(()) to ExtensionExitedBeforeShutdown",
                        ),
                        Ok(Err(e)) => return Err(e),
                        Err(e) => {
                            return Err(Error::JoinTaskError {
                                is_canceled: e.is_cancelled(),
                                is_panic: e.is_panic(),
                                error: e.to_string(),
                            });
                        }
                    }
                }
                Some((key, timeout, outcome)) = waiters.next() => {
                    match outcome {
                        Ok(Ok(())) => {}
                        Ok(Err(crate::extension::ReadinessProbeError::SignallerDropped)) => {
                            return Err(Error::ExtensionReadinessSignallerDropped {
                                extension: key.id.to_string(),
                                variant: key.variant.as_str().to_owned(),
                            });
                        }
                        Err(_elapsed) => {
                            return Err(Error::ExtensionReadinessTimeout {
                                extension: key.id.to_string(),
                                variant: key.variant.as_str().to_owned(),
                                timeout,
                            });
                        }
                    }
                }
            }
        }
        Ok(())
    }

    /// Returns the `ExtensionKey` associated with a `joined` outcome so the
    /// caller can prune `pending_starts`. Falls back to `task_id_to_key`
    /// when the task panicked before signalling (and thus before its body
    /// could embed the key in the `Ok` payload).
    fn pending_key_for(
        joined: &Result<(ExtensionKey, Result<(), Error>), JoinError>,
        task_id_to_key: &HashMap<task::Id, ExtensionKey>,
    ) -> Option<ExtensionKey> {
        match joined {
            Ok((k, _)) => Some(k.clone()),
            Err(e) => task_id_to_key.get(&e.id()).cloned(),
        }
    }

    /// Records the completion in the monitor, prunes the shutdown channel for
    /// this extension, and upgrades an early `Ok(())` to
    /// `ExtensionExitedBeforeShutdown` when shutdown hasn't been initiated.
    fn route_joined(
        monitor: &mut ExtensionMetricsMonitor,
        task_id_to_key: &mut HashMap<task::Id, ExtensionKey>,
        shutdown_channels: &mut Vec<(ExtensionKey, ExtensionShutdownChannel)>,
        shutdown_initiated: bool,
        joined: Result<(ExtensionKey, Result<(), Error>), JoinError>,
    ) -> Result<Result<(), Error>, JoinError> {
        match joined {
            Ok((key, res)) => {
                task_id_to_key.retain(|_, k| k != &key);
                shutdown_channels.retain(|(k, _)| k != &key);
                let res = match res {
                    Ok(()) if !shutdown_initiated => Err(Error::ExtensionExitedBeforeShutdown {
                        extension: key.id.to_string(),
                    }),
                    other => other,
                };
                let outcome = match &res {
                    Ok(()) => ExtensionOutcome::Ok,
                    Err(e) => ExtensionOutcome::Err(e.to_string()),
                };
                monitor.apply_event(ExtensionLifecycleEvent::Completed { key, outcome });
                Ok(res)
            }
            Err(e) => {
                let task_id = e.id();
                if let Some(key) = task_id_to_key.remove(&task_id) {
                    shutdown_channels.retain(|(k, _)| k != &key);
                    let outcome = if e.is_cancelled() {
                        ExtensionOutcome::JoinCancelled
                    } else {
                        ExtensionOutcome::JoinPanic
                    };
                    monitor.apply_event(ExtensionLifecycleEvent::Completed { key, outcome });
                } else {
                    otel_warn!(
                        "extension.task.join_error.unknown_task",
                        is_canceled = e.is_cancelled(),
                        is_panic = e.is_panic(),
                        task_id = format!("{task_id}"),
                    );
                }
                Err(e)
            }
        }
    }

    pub fn monitor_tick(&mut self, now: Instant, reporter: &mut MetricsReporter) {
        self.monitor.tick(now, reporter);
    }

    pub(crate) async fn finish_metrics_reporting_until(
        &mut self,
        reporter: &MetricsReporter,
        deadline: Instant,
    ) -> Result<(), otel_arrow_dfe_telemetry::error::Error> {
        self.monitor
            .finish_reporting_until(reporter, deadline)
            .await
    }

    #[allow(dead_code)]
    pub fn shutdown_initiated(&self) -> bool {
        matches!(self.phase, LifecyclePhase::ShuttingDown { .. })
    }

    /// Broadcasts `Shutdown` to every active+background extension via its
    /// priority oneshot channel. Single-shot: subsequent calls are no-ops.
    pub fn initiate_shutdown(&mut self, reason: Option<&str>) {
        if matches!(self.phase, LifecyclePhase::ShuttingDown { .. }) {
            return;
        }
        if self.shutdown_channels.is_empty() {
            return;
        }

        let deadline = Instant::now() + EXTENSION_SHUTDOWN_GRACE;
        self.phase = LifecyclePhase::ShuttingDown { deadline };

        let reason = reason.unwrap_or(DEFAULT_SHUTDOWN_REASON).to_string();
        for (key, channel) in self.shutdown_channels.drain(..) {
            let payload = ShutdownPayload {
                deadline,
                reason: reason.clone(),
            };
            match channel.sender.send(payload) {
                Ok(()) => {
                    self.monitor
                        .apply_event(ExtensionLifecycleEvent::ShutdownSent { key });
                }
                Err(_payload) => {
                    otel_warn!(
                        "extension.shutdown.receiver_dropped",
                        extension = channel.name.as_ref(),
                    );
                }
            }
        }
    }

    /// Drains remaining extension tasks, bounded by the shutdown deadline.
    pub async fn drain_until_deadline(&mut self) {
        if self.futures.is_empty() {
            return;
        }
        let deadline = match self.phase {
            LifecyclePhase::ShuttingDown { deadline } => deadline,
            LifecyclePhase::Running => {
                let deadline = Instant::now() + EXTENSION_SHUTDOWN_GRACE;
                self.phase = LifecyclePhase::ShuttingDown { deadline };
                deadline
            }
        };
        let drain_deadline = TokioInstant::from_std(deadline + EXTENSION_SHUTDOWN_DRAIN_SLACK);

        let futures = &mut self.futures;
        let task_id_to_key = &mut self.task_id_to_key;
        let shutdown_channels = &mut self.shutdown_channels;
        let monitor = &mut self.monitor;
        let drain = async {
            while let Some(result) = futures.next().await {
                match result {
                    Ok((key, Ok(()))) => {
                        task_id_to_key.retain(|_, k| k != &key);
                        shutdown_channels.retain(|(k, _)| k != &key);
                        monitor.apply_event(ExtensionLifecycleEvent::Completed {
                            key,
                            outcome: ExtensionOutcome::Ok,
                        });
                    }
                    Ok((key, Err(e))) => {
                        task_id_to_key.retain(|_, k| k != &key);
                        shutdown_channels.retain(|(k, _)| k != &key);
                        otel_warn!("extension.shutdown.task.error", error = format!("{e}"));
                        monitor.apply_event(ExtensionLifecycleEvent::Completed {
                            key,
                            outcome: ExtensionOutcome::Err(e.to_string()),
                        });
                    }
                    Err(e) => {
                        let task_id = e.id();
                        otel_warn!(
                            "extension.shutdown.task.join_error",
                            is_canceled = e.is_cancelled(),
                            is_panic = e.is_panic(),
                            error = e.to_string()
                        );
                        if let Some(key) = task_id_to_key.remove(&task_id) {
                            shutdown_channels.retain(|(k, _)| k != &key);
                            let outcome = if e.is_cancelled() {
                                ExtensionOutcome::JoinCancelled
                            } else {
                                ExtensionOutcome::JoinPanic
                            };
                            monitor
                                .apply_event(ExtensionLifecycleEvent::Completed { key, outcome });
                        }
                    }
                }
            }
        };

        if tokio::time::timeout_at(drain_deadline, drain)
            .await
            .is_err()
        {
            otel_warn!(
                "extension.shutdown.timeout",
                grace_secs = EXTENSION_SHUTDOWN_GRACE.as_secs(),
                remaining = self.futures.len()
            );
            self.monitor.mark_stragglers_as_timeout();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::control::{
        ExtensionControlMsg, ExtensionControlSender, ExtensionShutdownChannel, ShutdownPayload,
    };
    use crate::extension::wrapper::ExtensionVariant;
    use tokio::sync::oneshot;

    fn make_shutdown_channel(
        id: &'static str,
    ) -> (
        ExtensionKey,
        ExtensionShutdownChannel,
        oneshot::Receiver<ShutdownPayload>,
    ) {
        let key = ExtensionKey::new(id.into(), ExtensionVariant::Local);
        let (tx, rx) = oneshot::channel();
        let channel = ExtensionShutdownChannel {
            name: id.into(),
            sender: tx,
        };
        (key, channel, rx)
    }

    #[test]
    fn drain_until_deadline_is_bounded_for_stuck_extension() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let key = ExtensionKey::new("stuck".into(), ExtensionVariant::Local);
            let futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>> =
                FuturesUnordered::new();
            let handle = local_tasks.spawn_local({
                let key = key.clone();
                async move {
                    std::future::pending::<()>().await;
                    (key, Ok(()))
                }
            });
            let mut task_id_to_key = HashMap::new();
            let _ = task_id_to_key.insert(handle.id(), key);
            futures.push(handle);

            let injected_deadline = Instant::now() + Duration::from_millis(100);
            let (_started_tx, started_rx) = mpsc::unbounded_channel();
            let mut lifecycle = ExtensionLifecycle {
                futures,
                task_id_to_key,
                shutdown_channels: Vec::new(),
                _passive: Vec::new(),
                phase: LifecyclePhase::ShuttingDown {
                    deadline: injected_deadline,
                },
                monitor: ExtensionMetricsMonitor::disabled(ext_ctx),
                started_rx,
                pending_starts: HashSet::new(),
                extension_readiness_probes: Vec::new(),
            };

            let start = Instant::now();
            lifecycle.drain_until_deadline().await;
            let elapsed = start.elapsed();

            let upper_bound = Duration::from_millis(100)
                + EXTENSION_SHUTDOWN_DRAIN_SLACK
                + Duration::from_secs(1);
            assert!(
                elapsed < upper_bound,
                "drain_until_deadline did not honor the deadline: elapsed={:?}, upper_bound={:?}",
                elapsed,
                upper_bound,
            );
            assert!(
                !lifecycle.futures.is_empty(),
                "stuck extension should still be present after the bounded drain timed out",
            );
        }));
    }

    /// A panicking extension task must surface as a terminal `Failed`
    /// state and bump the `completed_panic` counter.
    #[test]
    fn panicking_extension_task_reports_failed_terminal_state() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let key = ExtensionKey::new("boom".into(), ExtensionVariant::Local);
            let entity_key =
                ext_ctx.register_extension_entity("boom".into(), ExtensionVariant::Local);

            let mut monitor = ExtensionMetricsMonitor::new(
                ext_ctx.clone(),
                Duration::from_millis(50),
                Duration::from_millis(50),
            );
            monitor.register(&ext_ctx, key.clone(), entity_key, None);

            let futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>> =
                FuturesUnordered::new();
            let handle = local_tasks.spawn_local({
                let task_key = key.clone();
                async move {
                    task::yield_now().await;
                    panic!("simulated extension panic");
                    #[allow(unreachable_code)]
                    {
                        let _: ExtensionKey = task_key;
                        (
                            ExtensionKey::new("never".into(), ExtensionVariant::Local),
                            Ok(()),
                        )
                    }
                }
            });
            let mut task_id_to_key = HashMap::new();
            let _ = task_id_to_key.insert(handle.id(), key.clone());
            futures.push(handle);

            let (_started_tx, started_rx) = mpsc::unbounded_channel();
            let mut lifecycle = ExtensionLifecycle {
                futures,
                task_id_to_key,
                shutdown_channels: Vec::new(),
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor,
                started_rx,
                pending_starts: HashSet::new(),
                extension_readiness_probes: Vec::new(),
            };

            let prev_hook = std::panic::take_hook();
            std::panic::set_hook(Box::new(|_| {}));

            let result = lifecycle.next_event().await;

            std::panic::set_hook(prev_hook);

            match result {
                LifecycleEvent::Completion(Err(e)) => {
                    assert!(e.is_panic(), "expected panic-style JoinError, got {e:?}");
                }
                LifecycleEvent::Completion(Ok(inner)) => panic!(
                    "expected Completion(Err(JoinError)) for a panicking task, got Completion(Ok({inner:?}))"
                ),
                LifecycleEvent::MonitorTick(_) => panic!(
                    "expected the panicking task to surface as a Completion before any monitor tick"
                ),
            }

            assert_eq!(
                lifecycle.monitor.state_for(&key),
                Some(crate::extension_monitor::ExtensionRuntimeState::Failed),
                "panicking extension must surface as Failed in the monitor",
            );
            assert_eq!(
                lifecycle.monitor.completed_panic_count(&key),
                Some(1),
                "completed_panic counter must increment exactly once",
            );
        }));
    }

    /// `initiate_shutdown` is single-shot: a second call must be a no-op and
    /// leave the phase in `ShuttingDown`.
    #[test]
    fn initiate_shutdown_is_single_shot() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();

            let (key_a, channel_a, mut rx_a) = make_shutdown_channel("a");
            let (key_b, channel_b, mut rx_b) = make_shutdown_channel("b");

            let mut lifecycle = ExtensionLifecycle {
                futures: FuturesUnordered::new(),
                task_id_to_key: HashMap::new(),
                shutdown_channels: vec![(key_a, channel_a), (key_b, channel_b)],
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor: ExtensionMetricsMonitor::disabled(ext_ctx),
                started_rx: mpsc::unbounded_channel().1,
                pending_starts: HashSet::new(),
                extension_readiness_probes: Vec::new(),
            };

            lifecycle.initiate_shutdown(Some("first"));
            lifecycle.initiate_shutdown(Some("second"));

            assert!(
                lifecycle.shutdown_initiated(),
                "phase must transition to ShuttingDown on the first call"
            );
            assert!(
                lifecycle.shutdown_channels.is_empty(),
                "oneshot senders must be drained after the first call \
                 so the second call is a structural no-op"
            );

            let payload_a = rx_a
                .try_recv()
                .expect("a must receive exactly one Shutdown");
            assert_eq!(payload_a.reason, "first");
            let payload_b = rx_b
                .try_recv()
                .expect("b must receive exactly one Shutdown");
            assert_eq!(payload_b.reason, "first");
        }));
    }

    /// Passive extensions never spawn a task and must not consume a
    /// monitor entry: monitor only tracks the live-task population.
    #[test]
    fn passive_extensions_are_not_registered_in_monitor() {
        use crate::extension::ExtensionWrapper;

        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();

            let passive_cfg = crate::config::ExtensionConfig::new("passive_ext");
            let user =
                std::sync::Arc::new(otel_arrow_dfe_config::extension::ExtensionUserConfig::new(
                    "urn:otap:extension:test".into(),
                    serde_json::Value::Null,
                ));
            let mut passive_bundle =
                ExtensionWrapper::builder("passive_ext".into(), user.clone(), &passive_cfg)
                    .passive()
                    .cloned()
                    .shared("state".to_string())
                    .build()
                    .unwrap();
            let passive_w = passive_bundle.take_shared().unwrap();
            let passive_entity =
                ext_ctx.register_extension_entity("passive_ext".into(), ExtensionVariant::Shared);

            #[derive(Clone)]
            struct ActiveExt;
            #[async_trait::async_trait(?Send)]
            impl crate::local::extension::Extension for ActiveExt {
                async fn start(
                    self: std::rc::Rc<Self>,
                    mut ctrl: crate::local::extension::ControlChannel,
                    _eh: crate::extension::wrapper::EffectHandler,
                ) -> Result<crate::terminal_state::TerminalState, Error> {
                    while let Ok(msg) = ctrl.recv().await {
                        if matches!(msg, ExtensionControlMsg::Shutdown { .. }) {
                            break;
                        }
                    }
                    Ok(Default::default())
                }
            }
            let active_cfg = crate::config::ExtensionConfig::new("active_ext");
            let mut active_bundle =
                ExtensionWrapper::builder("active_ext".into(), user, &active_cfg)
                    .active()
                    .local(std::rc::Rc::new(ActiveExt))
                    .build()
                    .unwrap();
            let active_w = active_bundle.take_local().unwrap();
            let active_entity =
                ext_ctx.register_extension_entity("active_ext".into(), ExtensionVariant::Local);

            let monitor = ExtensionMetricsMonitor::new(
                ext_ctx.clone(),
                Duration::from_millis(50),
                Duration::from_millis(50),
            );

            let (tx, _rx) = flume::bounded(1);
            let reporter = MetricsReporter::new(tx);
            let lifecycle = ExtensionLifecycle::spawn(
                vec![(passive_w, passive_entity), (active_w, active_entity)],
                &local_tasks,
                reporter,
                TerminalMetricsDeadline::default(),
                &ext_ctx,
                monitor,
            );

            let passive_key = ExtensionKey::new("passive_ext".into(), ExtensionVariant::Shared);
            let active_key = ExtensionKey::new("active_ext".into(), ExtensionVariant::Local);
            assert!(
                lifecycle.monitor.state_for(&passive_key).is_none(),
                "passive extension must not appear in the monitor"
            );
            assert!(
                lifecycle.monitor.state_for(&active_key).is_some(),
                "active extension must appear in the monitor"
            );

            drop(lifecycle);
        }));
    }

    /// Two scopes (today: pipelines) each own a distinct `ExtensionLifecycle`;
    /// a shutdown in one must not reach the other's extensions.
    #[test]
    fn initiate_shutdown_in_one_scope_does_not_reach_another_scope() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ctx_a, _reg_a) = crate::testing::test_extension_ctx();
            let (ctx_b, _reg_b) = crate::testing::test_extension_ctx();

            let (key_a, channel_a, mut rx_a) = make_shutdown_channel("a");
            let (key_b, channel_b, mut rx_b) = make_shutdown_channel("b");

            let mut life_a = ExtensionLifecycle {
                futures: FuturesUnordered::new(),
                task_id_to_key: HashMap::new(),
                shutdown_channels: vec![(key_a, channel_a)],
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor: ExtensionMetricsMonitor::disabled(ctx_a),
                started_rx: mpsc::unbounded_channel().1,
                pending_starts: HashSet::new(),
                extension_readiness_probes: Vec::new(),
            };
            let life_b = ExtensionLifecycle {
                futures: FuturesUnordered::new(),
                task_id_to_key: HashMap::new(),
                shutdown_channels: vec![(key_b, channel_b)],
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor: ExtensionMetricsMonitor::disabled(ctx_b),
                started_rx: mpsc::unbounded_channel().1,
                pending_starts: HashSet::new(),
                extension_readiness_probes: Vec::new(),
            };

            life_a.initiate_shutdown(Some("a-only"));

            assert!(
                rx_a.try_recv().is_ok(),
                "scope A's extension must receive its own shutdown"
            );
            assert!(
                rx_b.try_recv().is_err(),
                "scope B's extension must NOT receive scope A's shutdown"
            );
            assert!(
                !life_b.shutdown_initiated(),
                "scope B's phase must remain Running"
            );
        }));
    }

    /// `CollectTelemetry` fanout from one scope's monitor must not reach
    /// extensions in another scope.
    #[test]
    fn collect_telemetry_fanout_is_scoped_to_owning_monitor() {
        use crate::message::Sender;
        use otel_arrow_dfe_channel::mpsc;

        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ctx_a, _reg_a) = crate::testing::test_extension_ctx();
            let (ctx_b, _reg_b) = crate::testing::test_extension_ctx();

            let mut monitor_a = ExtensionMetricsMonitor::new(
                ctx_a.clone(),
                Duration::from_millis(50),
                Duration::from_millis(50),
            );
            let mut monitor_b = ExtensionMetricsMonitor::new(
                ctx_b.clone(),
                Duration::from_millis(50),
                Duration::from_millis(50),
            );

            let (tx_a, rx_a) = mpsc::Channel::new(8);
            let (tx_b, rx_b) = mpsc::Channel::new(8);
            let sender_a = ExtensionControlSender {
                sender: Sender::new_local_mpsc_sender(tx_a),
            };
            let sender_b = ExtensionControlSender {
                sender: Sender::new_local_mpsc_sender(tx_b),
            };

            let key_a = ExtensionKey::new("a".into(), ExtensionVariant::Local);
            let key_b = ExtensionKey::new("b".into(), ExtensionVariant::Local);
            let ent_a = ctx_a.register_extension_entity("a".into(), ExtensionVariant::Local);
            let ent_b = ctx_b.register_extension_entity("b".into(), ExtensionVariant::Local);
            monitor_a.register(&ctx_a, key_a.clone(), ent_a, Some(&sender_a));
            monitor_b.register(&ctx_b, key_b.clone(), ent_b, Some(&sender_b));

            let (rep_tx, _rep_rx) = flume::bounded(8);
            let mut reporter = MetricsReporter::new(rep_tx);

            monitor_a.tick(Instant::now(), &mut reporter);

            assert!(
                rx_a.try_recv().is_ok(),
                "scope A's extension must receive its scope's CollectTelemetry"
            );
            assert!(
                rx_b.try_recv().is_err(),
                "scope B's extension must NOT receive scope A's CollectTelemetry"
            );
        }));
    }

    /// On task completion, the extension must be pruned from
    /// `shutdown_channels` so a subsequent `initiate_shutdown` does not signal
    /// a dropped oneshot receiver.
    #[test]
    fn completed_extensions_are_pruned_from_shutdown_channels() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();

            let (key_a, channel_a, _rx_a) = make_shutdown_channel("a");
            let (key_b, channel_b, _rx_b) = make_shutdown_channel("b");

            let futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>> =
                FuturesUnordered::new();
            let handle = local_tasks.spawn_local({
                let key = key_a.clone();
                async move { (key, Ok(())) }
            });
            let mut task_id_to_key = HashMap::new();
            let _ = task_id_to_key.insert(handle.id(), key_a.clone());
            futures.push(handle);

            let (_started_tx, started_rx) = mpsc::unbounded_channel();
            let mut lifecycle = ExtensionLifecycle {
                futures,
                task_id_to_key,
                shutdown_channels: vec![(key_a.clone(), channel_a), (key_b.clone(), channel_b)],
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor: ExtensionMetricsMonitor::disabled(ext_ctx),
                started_rx,
                pending_starts: HashSet::new(),
                extension_readiness_probes: Vec::new(),
            };

            match lifecycle.next_event().await {
                LifecycleEvent::Completion(Ok(Err(Error::ExtensionExitedBeforeShutdown {
                    extension,
                }))) => {
                    assert_eq!(extension, "a");
                }
                LifecycleEvent::Completion(other) => {
                    panic!(
                        "expected Completion(Ok(Err(ExtensionExitedBeforeShutdown))) for A, got {other:?}"
                    )
                }
                LifecycleEvent::MonitorTick(_) => {
                    panic!("expected Completion before any monitor tick")
                }
            }

            assert!(
                !lifecycle.shutdown_channels.iter().any(|(k, _)| k == &key_a),
                "completed extension A must be pruned from `shutdown_channels`"
            );
            assert!(
                lifecycle.shutdown_channels.iter().any(|(k, _)| k == &key_b),
                "non-completed extension B must remain in `shutdown_channels`"
            );
        }));
    }

    /// After a completion is routed, `initiate_shutdown` must not signal the
    /// completed extension's oneshot -- even if its receiver is still alive.
    #[test]
    fn initiate_shutdown_skips_already_completed_extension() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();

            let (key_a, channel_a, mut rx_a) = make_shutdown_channel("a");
            let (key_b, channel_b, mut rx_b) = make_shutdown_channel("b");

            let futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>> =
                FuturesUnordered::new();
            let handle = local_tasks.spawn_local({
                let key = key_a.clone();
                async move { (key, Ok(())) }
            });
            let mut task_id_to_key = HashMap::new();
            let _ = task_id_to_key.insert(handle.id(), key_a.clone());
            futures.push(handle);

            let (_started_tx, started_rx) = mpsc::unbounded_channel();
            let mut lifecycle = ExtensionLifecycle {
                futures,
                task_id_to_key,
                shutdown_channels: vec![(key_a.clone(), channel_a), (key_b.clone(), channel_b)],
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor: ExtensionMetricsMonitor::disabled(ext_ctx),
                started_rx,
                pending_starts: HashSet::new(),
                extension_readiness_probes: Vec::new(),
            };

            let _ = lifecycle.next_event().await;
            lifecycle.initiate_shutdown(Some("after completion"));

            assert!(
                rx_a.try_recv().is_err(),
                "initiate_shutdown must not signal completed extension A"
            );
            assert!(
                rx_b.try_recv().is_ok(),
                "initiate_shutdown must still reach non-completed extension B"
            );
        }));
    }

    #[test]
    fn wait_all_spawned_returns_after_extensions_signal_spawned() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();

            let (started_tx, started_rx) = mpsc::unbounded_channel::<ExtensionKey>();
            let futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>> =
                FuturesUnordered::new();
            let mut task_id_to_key = HashMap::new();
            let mut pending_starts: HashSet<ExtensionKey> = HashSet::new();

            for name in ["x", "y"] {
                let key = ExtensionKey::local(name);
                let started_tx = started_tx.clone();
                let task_key = key.clone();
                let handle = local_tasks.spawn_local(async move {
                    let _ = started_tx.send(task_key.clone());
                    std::future::pending::<()>().await;
                    (task_key, Ok::<(), Error>(()))
                });
                let _ = task_id_to_key.insert(handle.id(), key.clone());
                futures.push(handle);
                let _ = pending_starts.insert(key);
            }
            drop(started_tx);

            let mut lifecycle = ExtensionLifecycle {
                futures,
                task_id_to_key,
                shutdown_channels: Vec::new(),
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor: ExtensionMetricsMonitor::disabled(ext_ctx),
                started_rx,
                pending_starts,

                extension_readiness_probes: Vec::new(),
            };

            let outcome =
                tokio::time::timeout(Duration::from_secs(1), lifecycle.wait_all_spawned()).await;

            assert!(outcome.is_ok(), "wait_all_spawned must not hang");
            assert!(outcome.unwrap().is_ok());
            assert!(lifecycle.pending_starts.is_empty());
        }));
    }

    #[test]
    fn wait_all_spawned_surfaces_completion_when_outstanding_signals_reach_zero() {
        // When several tasks signal before the barrier observes them and one
        // of those tasks has *also* already completed with an error, the
        // barrier must surface that error rather than returning Ok just
        // because pending_starts emptied. Otherwise the caller starts node
        // tasks against an already-dead extension.
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();

            let (started_tx, started_rx) = mpsc::unbounded_channel::<ExtensionKey>();
            let futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>> =
                FuturesUnordered::new();
            let mut task_id_to_key = HashMap::new();
            let mut pending_starts: HashSet<ExtensionKey> = HashSet::new();

            // A: signals, then fails immediately.
            let a_key = ExtensionKey::local("a-fast-fail");
            {
                let started_tx = started_tx.clone();
                let task_key = a_key.clone();
                let handle = local_tasks.spawn_local(async move {
                    let _ = started_tx.send(task_key.clone());
                    (
                        task_key,
                        Err::<(), Error>(Error::ExtensionExitedBeforeShutdown {
                            extension: "a-fast-fail".into(),
                        }),
                    )
                });
                let _ = task_id_to_key.insert(handle.id(), a_key.clone());
                futures.push(handle);
                let _ = pending_starts.insert(a_key);
            }

            // B: signals, then parks forever.
            let b_key = ExtensionKey::local("b-pending");
            {
                let started_tx = started_tx.clone();
                let task_key = b_key.clone();
                let handle = local_tasks.spawn_local(async move {
                    let _ = started_tx.send(task_key.clone());
                    std::future::pending::<()>().await;
                    (task_key, Ok::<(), Error>(()))
                });
                let _ = task_id_to_key.insert(handle.id(), b_key.clone());
                futures.push(handle);
                let _ = pending_starts.insert(b_key);
            }

            drop(started_tx);

            let mut lifecycle = ExtensionLifecycle {
                futures,
                task_id_to_key,
                shutdown_channels: Vec::new(),
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor: ExtensionMetricsMonitor::disabled(ext_ctx),
                started_rx,
                pending_starts,

                extension_readiness_probes: Vec::new(),
            };

            let outcome =
                tokio::time::timeout(Duration::from_millis(200), lifecycle.wait_all_spawned())
                    .await;

            let res = outcome.expect("barrier must not hang");
            assert!(
                res.is_err(),
                "barrier must surface a completed-with-error task even when all signals have already been consumed; got Ok",
            );
        }));
    }

    #[test]
    fn wait_all_spawned_surfaces_panic_before_signal() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();

            let (_started_tx, started_rx) = mpsc::unbounded_channel::<ExtensionKey>();
            let futures: FuturesUnordered<JoinHandle<(ExtensionKey, Result<(), Error>)>> =
                FuturesUnordered::new();
            let mut task_id_to_key = HashMap::new();

            let key = ExtensionKey::local("paniker");
            let handle = local_tasks.spawn_local(async {
                panic!("synthetic first-poll panic");
                #[allow(unreachable_code)]
                (ExtensionKey::local("paniker"), Ok::<(), Error>(()))
            });
            let _ = task_id_to_key.insert(handle.id(), key.clone());
            futures.push(handle);

            let mut lifecycle = ExtensionLifecycle {
                futures,
                task_id_to_key,
                shutdown_channels: Vec::new(),
                _passive: Vec::new(),
                phase: LifecyclePhase::Running,
                monitor: ExtensionMetricsMonitor::disabled(ext_ctx),
                started_rx,
                pending_starts: HashSet::from([key]),

                extension_readiness_probes: Vec::new(),
            };

            let prev_hook = std::panic::take_hook();
            std::panic::set_hook(Box::new(|_| {}));

            let outcome =
                tokio::time::timeout(Duration::from_secs(1), lifecycle.wait_all_spawned()).await;

            std::panic::set_hook(prev_hook);

            let res = outcome.expect("barrier must not hang on panic-before-signal");
            match res {
                Err(Error::JoinTaskError { is_panic, .. }) => assert!(is_panic),
                other => panic!("expected JoinTaskError(is_panic), got {other:?}"),
            }
            assert!(lifecycle.pending_starts.is_empty());
        }));
    }

    use crate::channel_metrics::ChannelMetricsRegistry;
    use crate::extension::ExtensionWrapper;

    #[derive(Clone)]
    struct ShutdownAwaitExt {
        observed_close: std::rc::Rc<std::cell::Cell<bool>>,
        observed_shutdown: std::rc::Rc<std::cell::Cell<bool>>,
    }

    #[async_trait::async_trait(?Send)]
    impl crate::local::extension::Extension for ShutdownAwaitExt {
        async fn start(
            self: std::rc::Rc<Self>,
            mut ctrl: crate::local::extension::ControlChannel,
            _eh: crate::extension::wrapper::EffectHandler,
        ) -> Result<crate::terminal_state::TerminalState, Error> {
            loop {
                match ctrl.recv().await {
                    Ok(ExtensionControlMsg::Shutdown { .. }) => {
                        self.observed_shutdown.set(true);
                        break;
                    }
                    Ok(_) => continue,
                    Err(_) => {
                        self.observed_close.set(true);
                        return Err(Error::ExtensionExitedBeforeShutdown {
                            extension: "shutdown-await".into(),
                        });
                    }
                }
            }
            Ok(crate::terminal_state::TerminalState::default())
        }
    }

    #[test]
    fn extension_control_channel_stays_open_with_pipeline_metrics_disabled() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();

            let observed_close = std::rc::Rc::new(std::cell::Cell::new(false));
            let observed_shutdown = std::rc::Rc::new(std::cell::Cell::new(false));
            let ext = std::rc::Rc::new(ShutdownAwaitExt {
                observed_close: observed_close.clone(),
                observed_shutdown: observed_shutdown.clone(),
            });

            let cfg = crate::config::ExtensionConfig::new("disabled-monitor");
            let user =
                std::sync::Arc::new(otel_arrow_dfe_config::extension::ExtensionUserConfig::new(
                    "urn:otap:extension:test".into(),
                    serde_json::Value::Null,
                ));
            let wrapper = ExtensionWrapper::builder("disabled-monitor".into(), user, &cfg)
                .active()
                .local(ext)
                .build()
                .unwrap()
                .take_local()
                .unwrap();
            let entity_key = ext_ctx
                .register_extension_entity("disabled-monitor".into(), ExtensionVariant::Local);

            let monitor = ExtensionMetricsMonitor::disabled(ext_ctx.clone());
            let (tx, _rx) = flume::bounded(1);
            let reporter = MetricsReporter::new(tx);

            let mut lifecycle = ExtensionLifecycle::spawn(
                vec![(wrapper, entity_key)],
                &local_tasks,
                reporter,
                TerminalMetricsDeadline::default(),
                &ext_ctx,
                monitor,
            );

            let barrier =
                tokio::time::timeout(Duration::from_secs(1), lifecycle.wait_all_spawned())
                    .await
                    .expect("spawn barrier must not hang");
            barrier.expect("spawn barrier must succeed");

            tokio::time::sleep(Duration::from_millis(50)).await;
            assert!(
                !observed_close.get(),
                "extension's control channel must not close before Shutdown is delivered"
            );

            lifecycle.initiate_shutdown(Some("test"));
            lifecycle.drain_until_deadline().await;

            assert!(
                observed_shutdown.get(),
                "extension must have observed exactly one Shutdown"
            );
            assert!(
                !observed_close.get(),
                "extension must not have seen RecvError::Closed before Shutdown"
            );
        }));
    }

    #[test]
    fn extension_telemetry_guard_held_for_full_extension_lifetime() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, registry) = crate::testing::test_extension_ctx();

            let observed_close = std::rc::Rc::new(std::cell::Cell::new(false));
            let observed_shutdown = std::rc::Rc::new(std::cell::Cell::new(false));
            let ext = std::rc::Rc::new(ShutdownAwaitExt {
                observed_close: observed_close.clone(),
                observed_shutdown: observed_shutdown.clone(),
            });

            let cfg = crate::config::ExtensionConfig::new("guarded");
            let user = std::sync::Arc::new(otel_arrow_dfe_config::extension::ExtensionUserConfig::new(
                "urn:otap:extension:test".into(),
                serde_json::Value::Null,
            ));
            let mut bundle = ExtensionWrapper::builder("guarded".into(), user, &cfg)
                .active()
                .local(ext)
                .build()
                .unwrap();

            let mut channel_metrics = ChannelMetricsRegistry::default();
            let keys = bundle.wire_telemetry(
                "guarded".into(),
                &ext_ctx,
                &mut channel_metrics,
                false,
            );
            let entity_key = keys
                .local
                .expect("wire_telemetry must register the local variant's entity");
            let wrapper = bundle.take_local().unwrap();

            assert!(
                registry.visit_entity(entity_key, |_| ()).is_some(),
                "baseline: entity must be registered before spawn"
            );

            let monitor = ExtensionMetricsMonitor::disabled(ext_ctx.clone());
            let (tx, _rx) = flume::bounded(1);
            let reporter = MetricsReporter::new(tx);

            let mut lifecycle = ExtensionLifecycle::spawn(
                vec![(wrapper, entity_key)],
                &local_tasks,
                reporter,
                TerminalMetricsDeadline::default(),
                &ext_ctx,
                monitor,
            );

            tokio::time::timeout(
                Duration::from_secs(1),
                lifecycle.wait_all_spawned(),
            )
            .await
            .expect("spawn barrier must not hang")
            .expect("spawn barrier must succeed");

            tokio::time::sleep(Duration::from_millis(30)).await;
            assert!(
                registry.visit_entity(entity_key, |_| ()).is_some(),
                "entity must remain registered for the entire duration of the extension's start().await",
            );

            lifecycle.initiate_shutdown(Some("test"));
            lifecycle.drain_until_deadline().await;

            assert!(observed_shutdown.get(), "extension must have observed Shutdown");
            assert!(!observed_close.get(), "extension control channel must not have closed early");

            drop(lifecycle);

            assert!(
                registry.visit_entity(entity_key, |_| ()).is_none(),
                "EntityTelemetryGuard must unregister the entity after start() returns",
            );
        }));
    }

    // wait_all_ready tests: exercise the readiness gate in isolation
    // (no ExtensionWrapper). Probes are injected directly into an
    // otherwise empty lifecycle.

    fn empty_lifecycle(ext_ctx: ExtensionContext) -> ExtensionLifecycle {
        let (_started_tx, started_rx) = mpsc::unbounded_channel::<ExtensionKey>();
        ExtensionLifecycle {
            futures: FuturesUnordered::new(),
            task_id_to_key: HashMap::new(),
            shutdown_channels: Vec::new(),
            _passive: Vec::new(),
            phase: LifecyclePhase::Running,
            monitor: ExtensionMetricsMonitor::disabled(ext_ctx),
            started_rx,
            pending_starts: HashSet::new(),
            extension_readiness_probes: Vec::new(),
        }
    }

    #[test]
    fn wait_all_ready_is_zero_cost_noop_when_no_extension_opted_in() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            let outcome =
                tokio::time::timeout(Duration::from_millis(50), lifecycle.wait_all_ready()).await;
            assert!(matches!(outcome, Ok(Ok(()))));
        }));
    }

    #[test]
    fn wait_all_ready_resolves_when_signaller_fires() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            let (sig, probe) = crate::extension::ReadinessSignaller::pair(Duration::from_secs(60));
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new("opt-in-ext".into(), ExtensionVariant::Local),
                probe,
            ));

            // Fire on a child task so the gate observably awaits.
            drop(task::spawn_local(async move {
                task::yield_now().await;
                sig.ready();
            }));

            let outcome =
                tokio::time::timeout(Duration::from_secs(1), lifecycle.wait_all_ready()).await;
            assert!(matches!(outcome, Ok(Ok(()))), "got {outcome:?}");
        }));
    }

    #[test]
    fn wait_all_ready_surfaces_signaller_dropped_with_named_extension() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            let (sig, probe) = crate::extension::ReadinessSignaller::pair(Duration::from_secs(60));
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new("dropped-ext".into(), ExtensionVariant::Shared),
                probe,
            ));
            drop(sig);

            let outcome =
                tokio::time::timeout(Duration::from_secs(1), lifecycle.wait_all_ready()).await;
            match outcome {
                Ok(Err(Error::ExtensionReadinessSignallerDropped { extension, variant })) => {
                    assert_eq!(extension, "dropped-ext");
                    assert_eq!(variant, "shared");
                }
                other => panic!("expected ExtensionReadinessSignallerDropped; got {other:?}"),
            }
        }));
    }

    #[test]
    fn wait_all_ready_surfaces_timeout_with_named_extension() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            let probe_timeout = Duration::from_millis(40);
            let (sig, probe) = crate::extension::ReadinessSignaller::pair(probe_timeout);
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new("slow-ext".into(), ExtensionVariant::Local),
                probe,
            ));

            let outcome =
                tokio::time::timeout(Duration::from_secs(1), lifecycle.wait_all_ready()).await;
            // Hold sig across the await so drop-detect can't race the timeout.
            drop(sig);

            match outcome {
                Ok(Err(Error::ExtensionReadinessTimeout {
                    extension,
                    variant,
                    timeout,
                })) => {
                    assert_eq!(extension, "slow-ext");
                    assert_eq!(variant, "local");
                    assert_eq!(timeout, probe_timeout);
                }
                other => panic!("expected ExtensionReadinessTimeout; got {other:?}"),
            }
        }));
    }

    #[test]
    fn wait_all_ready_max_wait_is_bounded_by_longest_timeout_not_sum() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            // Virtual time: the gate's wait is exact, so the assertion can use
            // a tight margin regardless of how far the serial sum exceeds the
            // longest timeout.
            tokio::time::pause();

            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            // Three fast extensions that signal immediately, plus a laggard
            // with the longest timeout that never signals. The gate must wait
            // for the laggard, so the worst-case wait is the longest timeout --
            // and, because probes are awaited in parallel, never the sum.
            let fast = Duration::from_millis(500);
            let longest = Duration::from_secs(1);
            let serial_sum = fast * 3 + longest;

            for key in ["fast-a", "fast-b", "fast-c"] {
                let (sig, probe) = crate::extension::ReadinessSignaller::pair(fast);
                lifecycle.extension_readiness_probes.push((
                    ExtensionKey::new(key.into(), ExtensionVariant::Local),
                    probe,
                ));
                drop(task::spawn_local(async move {
                    task::yield_now().await;
                    sig.ready();
                }));
            }

            let (laggard_sig, laggard_probe) = crate::extension::ReadinessSignaller::pair(longest);
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new("laggard".into(), ExtensionVariant::Shared),
                laggard_probe,
            ));

            let started = TokioInstant::now();
            let outcome = lifecycle.wait_all_ready().await;
            let elapsed = started.elapsed();
            drop(laggard_sig);

            match outcome {
                Err(Error::ExtensionReadinessTimeout {
                    extension, timeout, ..
                }) => {
                    assert_eq!(extension, "laggard");
                    assert_eq!(timeout, longest);
                }
                other => panic!("expected laggard timeout; got {other:?}"),
            }

            let bound = longest + Duration::from_millis(100);
            assert!(
                elapsed < bound,
                "gate waited {elapsed:?}, exceeding longest timeout + margin {bound:?} \
                 (serial sum would be {serial_sum:?})",
            );
        }));
    }

    #[test]
    fn wait_all_ready_fails_fast_at_shortest_timeout_not_longest() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            // Virtual time: the fail-fast instant is exact, so the assertion
            // can use a tight margin even with a far longer probe present.
            tokio::time::pause();

            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            // One extension times out quickly; another has a much longer
            // timeout. Neither signals. The gate must fail as soon as the
            // shortest timeout elapses, never waiting for the longest.
            let shortest = Duration::from_millis(100);
            let longest = Duration::from_secs(2);

            // Register the long-timeout probe FIRST: a buggy sequential
            // awaiter would block on it for 2s, so failing fast (and naming
            // "short") genuinely proves the probes are awaited concurrently.
            let (long_sig, long_probe) = crate::extension::ReadinessSignaller::pair(longest);
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new("long".into(), ExtensionVariant::Shared),
                long_probe,
            ));
            let (short_sig, short_probe) = crate::extension::ReadinessSignaller::pair(shortest);
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new("short".into(), ExtensionVariant::Local),
                short_probe,
            ));

            let started = TokioInstant::now();
            let outcome = lifecycle.wait_all_ready().await;
            let elapsed = started.elapsed();
            drop(short_sig);
            drop(long_sig);

            match outcome {
                Err(Error::ExtensionReadinessTimeout {
                    extension, timeout, ..
                }) => {
                    assert_eq!(extension, "short");
                    assert_eq!(timeout, shortest);
                }
                other => panic!("expected shortest-timeout failure; got {other:?}"),
            }

            let bound = shortest + Duration::from_millis(50);
            assert!(
                elapsed < bound,
                "gate took {elapsed:?} to fail, exceeding shortest timeout + margin {bound:?} \
                 (longest timeout was {longest:?})",
            );
        }));
    }

    #[test]
    fn wait_all_ready_surfaces_extension_task_failure_while_probe_pending() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            let (sig, probe) = crate::extension::ReadinessSignaller::pair(Duration::from_secs(60));
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new("pending-ext".into(), ExtensionVariant::Local),
                probe,
            ));

            let dead_key = ExtensionKey::new("dead-ext".into(), ExtensionVariant::Shared);
            lifecycle.futures.push(task::spawn_local(async move {
                (
                    dead_key,
                    Err(Error::InternalError {
                        message: "extension died during startup".into(),
                    }),
                )
            }));

            let outcome =
                tokio::time::timeout(Duration::from_secs(1), lifecycle.wait_all_ready()).await;
            drop(sig);

            match outcome {
                Ok(Err(Error::InternalError { message })) => {
                    assert_eq!(message, "extension died during startup");
                }
                other => panic!("expected task failure surfaced by the gate; got {other:?}"),
            }
        }));
    }

    #[test]
    fn wait_all_ready_is_idempotent_after_drain() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            let (sig, probe) = crate::extension::ReadinessSignaller::pair(Duration::from_secs(60));
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new("once-ext".into(), ExtensionVariant::Local),
                probe,
            ));
            sig.ready();

            let first =
                tokio::time::timeout(Duration::from_secs(1), lifecycle.wait_all_ready()).await;
            assert!(matches!(first, Ok(Ok(()))));
            assert!(lifecycle.extension_readiness_probes.is_empty());

            let second =
                tokio::time::timeout(Duration::from_millis(50), lifecycle.wait_all_ready()).await;
            assert!(matches!(second, Ok(Ok(()))), "got {second:?}");
        }));
    }

    #[test]
    fn wait_all_ready_partial_signal_in_dual_variant_lifecycle_names_laggard() {
        let (rt, local_tasks) = crate::testing::setup_test_runtime();
        rt.block_on(local_tasks.run_until(async {
            let (ext_ctx, _registry) = crate::testing::test_extension_ctx();
            let mut lifecycle = empty_lifecycle(ext_ctx);

            let shared_timeout = Duration::from_secs(60);
            let (shared_sig, shared_probe) =
                crate::extension::ReadinessSignaller::pair(shared_timeout);
            let local_timeout = Duration::from_millis(60);
            let (local_sig, local_probe) =
                crate::extension::ReadinessSignaller::pair(local_timeout);

            let ext_id_str = "dual-ext";
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new(ext_id_str.into(), ExtensionVariant::Shared),
                shared_probe,
            ));
            lifecycle.extension_readiness_probes.push((
                ExtensionKey::new(ext_id_str.into(), ExtensionVariant::Local),
                local_probe,
            ));

            shared_sig.ready();

            let started = Instant::now();
            let outcome =
                tokio::time::timeout(Duration::from_secs(5), lifecycle.wait_all_ready()).await;
            let elapsed = started.elapsed();
            // Keep both signallers alive through the await so drop-detect
            // never wins the race against the local timeout.
            drop(shared_sig);
            drop(local_sig);

            match outcome {
                Ok(Err(Error::ExtensionReadinessTimeout {
                    extension,
                    variant,
                    timeout,
                })) => {
                    assert_eq!(extension, "dual-ext");
                    assert_eq!(variant, "local");
                    assert_eq!(timeout, local_timeout);
                }
                other => panic!("expected ExtensionReadinessTimeout for local; got {other:?}"),
            }

            assert!(
                elapsed < local_timeout * 10,
                "elapsed {elapsed:?} suggests serial wait or wrong timeout"
            );
        }));
    }
}