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
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
//! Scope API for spawning work within a region.
//!
//! A `Scope` provides the API for spawning tasks, creating child regions,
//! and registering finalizers.
//!
//! # Execution Tiers and Soundness Rules
//!
//! Asupersync defines two execution tiers with different constraints:
//!
//! ## Fiber Tier (Phase 0)
//!
//! - Single-thread, borrow-friendly execution
//! - Can capture borrowed references (`&T`) since no migration
//! - Implemented via `spawn_local` (currently requires Send bounds; relaxed in Phase 1+)
//!
//! ## Task Tier (Phase 1+)
//!
//! - Multi-threaded, `Send` tasks that may migrate across workers
//! - **Must capture only `Send + 'static` data** by construction
//! - Can reference region-owned data via [`RRef<T>`](crate::types::rref::RRef)
//!
//! # Soundness Rules for Send Tasks
//!
//! The [`spawn`](Scope::spawn) method enforces the following bounds:
//!
//! | Component | Bound | Rationale |
//! |-----------|-------|-----------|
//! | Factory | `F: Send + 'static` | Factory may be called on any worker |
//! | Future | `Fut: Send + 'static` | Task may migrate between polls |
//! | Output | `Fut::Output: Send + 'static` | Result sent to potentially different thread |
//!
//! ## What Can Be Captured
//!
//! **Allowed captures in Send tasks:**
//! - Owned `'static` data that is `Send` (e.g., `String`, `Vec<T>`, `Arc<T>`)
//! - [`RRef<T>`](crate::types::rref::RRef) handles to region-heap-allocated data
//! - Atomic types (`AtomicU64`, etc.)
//! - Clone'd `Cx` (the capability context)
//!
//! **Disallowed captures:**
//! - Borrowed references (`&T`, `&mut T`) - not `'static`
//! - `Rc<T>`, `RefCell<T>` - not `Send`
//! - Raw pointers (unless wrapped in a `Send` type)
//! - References to stack-local data
//!
//! ## RRef for Region-Owned Data
//!
//! When tasks need to share data within a region without cloning, use the region
//! heap and [`RRef<T>`](crate::types::rref::RRef):
//!
//! ```ignore
//! // Allocate in region heap
//! let index = region.heap_alloc(expensive_data);
//! let rref = RRef::<ExpensiveData>::new(region_id, index);
//!
//! // Pass RRef to task - it's Copy + Send
//! scope.spawn(state, &cx, move |cx| async move {
//! // Access via region record (requires runtime lookup)
//! let data = rref.get_via_region(®ion_record)?;
//! process(data).await
//! });
//! ```
//!
//! # Compile-Time Enforcement
//!
//! The bounds are enforced at compile time. Attempting to capture non-Send
//! or non-static data will result in a compilation error:
//!
//! ```compile_fail
//! use std::rc::Rc;
//! use asupersync::cx::Scope;
//!
//! fn try_capture_rc(scope: &Scope, state: &mut RuntimeState, cx: &Cx) {
//! let rc = Rc::new(42); // Rc is !Send
//! scope.spawn(state, cx, move |_| async move {
//! println!("{}", rc); // ERROR: Rc is not Send
//! });
//! }
//! ```
//!
//! ```compile_fail
//! use asupersync::cx::Scope;
//!
//! fn try_capture_borrow(scope: &Scope, state: &mut RuntimeState, cx: &Cx) {
//! let local = 42;
//! let reference = &local; // Borrowed, not 'static
//! scope.spawn(state, cx, move |_| async move {
//! println!("{}", reference); // ERROR: borrowed data not 'static
//! });
//! }
//! ```
//!
//! # Lab Runtime Compatibility
//!
//! The Send bounds do not affect lab runtime determinism. The lab runtime
//! simulates multi-worker scheduling deterministically (same seed = same
//! execution), regardless of whether tasks are actually migrated.
use crate::channel::oneshot;
use crate::combinator::{Either, Select};
use crate::cx::{Cx, cap};
use crate::record::{AdmissionError, TaskRecord};
use crate::runtime::task_handle::{JoinError, TaskHandle};
use crate::runtime::{RegionCreateError, RuntimeState, SpawnError, StoredTask};
use crate::tracing_compat::{debug, debug_span};
use crate::types::{Budget, CancelReason, Outcome, PanicPayload, Policy, RegionId, TaskId};
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
/// A scope for spawning work within a region.
///
/// The scope provides methods for:
/// - Spawning tasks
/// - Creating child regions
/// - Registering finalizers
/// - Cancelling all children
pub struct Scope<'r, P: Policy = crate::types::policy::FailFast> {
/// The region this scope belongs to.
pub(crate) region: RegionId,
/// The budget for this scope.
pub(crate) budget: Budget,
/// Phantom data for the policy type.
pub(crate) _policy: PhantomData<&'r P>,
}
#[pin_project::pin_project]
pub(crate) struct CatchUnwind<F> {
#[pin]
pub(crate) inner: F,
}
impl<F: Future> Future for CatchUnwind<F> {
type Output = std::thread::Result<F::Output>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
this.inner.as_mut().poll(cx)
}));
match result {
Ok(Poll::Pending) => Poll::Pending,
Ok(Poll::Ready(v)) => Poll::Ready(Ok(v)),
Err(payload) => Poll::Ready(Err(payload)),
}
}
}
pub(crate) fn payload_to_string(payload: &Box<dyn std::any::Any + Send>) -> String {
payload
.downcast_ref::<&str>()
.map(ToString::to_string)
.or_else(|| payload.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "unknown panic".to_string())
}
struct RegionRunner<'a, Fut> {
fut: Pin<&'a mut CatchUnwind<Fut>>,
state: Option<&'a mut RuntimeState>,
child_region: RegionId,
}
impl<'a, Fut: Future> Future for RegionRunner<'a, Fut> {
type Output = (std::thread::Result<Fut::Output>, &'a mut RuntimeState);
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
match this.fut.as_mut().poll(cx) {
Poll::Ready(res) => {
let state = this.state.take().expect("polled after ready");
Poll::Ready((res, state))
}
Poll::Pending => Poll::Pending,
}
}
}
impl<Fut> Drop for RegionRunner<'_, Fut> {
fn drop(&mut self) {
if let Some(state) = self.state.take() {
let reason = CancelReason::fail_fast().with_region(self.child_region);
let _ = state.cancel_request(self.child_region, &reason, None);
if let Some(region) = state.region(self.child_region) {
region.begin_close(None);
}
state.advance_region_state(self.child_region);
}
}
}
struct RegionCloseFuture {
state: Arc<parking_lot::Mutex<crate::record::region::RegionCloseState>>,
}
impl Future for RegionCloseFuture {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let mut state = self.state.lock();
if state.closed {
Poll::Ready(())
} else {
if !state
.waker
.as_ref()
.is_some_and(|w| w.will_wake(cx.waker()))
{
state.waker = Some(cx.waker().clone());
}
Poll::Pending
}
}
}
impl Drop for RegionCloseFuture {
fn drop(&mut self) {
let mut state = self.state.lock();
state.waker = None;
}
}
impl<P: Policy> Scope<'_, P> {
/// Creates a new scope (internal use).
#[must_use]
#[allow(dead_code)]
#[cfg_attr(feature = "test-internals", visibility::make(pub))]
pub(crate) fn new(region: RegionId, budget: Budget) -> Self {
Self {
region,
budget,
_policy: PhantomData,
}
}
/// Returns the region ID for this scope.
#[must_use]
pub fn region_id(&self) -> RegionId {
self.region
}
/// Returns the budget for this scope.
#[must_use]
pub fn budget(&self) -> Budget {
self.budget
}
// =========================================================================
// Task Spawning
// =========================================================================
/// Spawns a new task within this scope's region.
///
/// This is the **Task Tier** spawn method for parallel execution. The task
/// may migrate between worker threads, so all captured data must be thread-safe.
///
/// The task will be owned by the region and will be cancelled if the
/// region is cancelled. The returned `TaskHandle` can be used to await
/// the task's result.
///
/// # Arguments
///
/// * `state` - The runtime state
/// * `cx` - The capability context (used for tracing/authorization)
/// * `f` - A closure that produces the future, receiving the new task's `Cx`
///
/// # Returns
///
/// A `TaskHandle<T>` that can be used to await the task's result.
///
/// # Soundness Rules (Type Bounds)
///
/// The following bounds encode the soundness rules for Send tasks:
///
/// * `F: FnOnce(Cx) -> Fut + Send + 'static` - Factory called on any worker
/// * `Fut: Future + Send + 'static` - Task may migrate between polls
/// * `Fut::Output: Send + 'static` - Result crosses thread boundary
///
/// These bounds ensure captured data can safely cross thread boundaries.
/// Use [`RRef<T>`](crate::types::rref::RRef) for region-heap-allocated data.
///
/// # Allowed Captures
///
/// | Type | Allowed | Reason |
/// |------|---------|--------|
/// | `String`, `Vec<T>`, owned data | ✅ | Send + 'static by ownership |
/// | `Arc<T>` where T: Send + Sync | ✅ | Thread-safe shared ownership |
/// | `RRef<T>` | ✅ | Region-heap reference, Copy + Send |
/// | `Cx` (cloned) | ✅ | Capability context is Send + Sync |
/// | `Rc<T>`, `RefCell<T>` | ❌ | Not Send |
/// | `&T`, `&mut T` | ❌ | Not 'static |
///
/// # Example
///
/// ```ignore
/// let handle = scope.spawn(&mut state, &cx, |cx| async move {
/// cx.trace("Child task running");
/// compute_value().await
/// });
///
/// let result = handle.join(&cx).await?;
/// ```
///
/// # Example with RRef
///
/// ```ignore
/// // Allocate expensive data in region heap
/// let index = region_record.heap_alloc(vec![1, 2, 3, 4, 5]);
/// let rref = RRef::<Vec<i32>>::new(region_id, index);
///
/// // RRef is Copy + Send, can be captured by multiple tasks
/// scope.spawn(&mut state, &cx, move |cx| async move {
/// // Would access via runtime state in real code
/// process_data(rref).await
/// });
/// ```
///
/// # Compile-Time Errors
///
/// Attempting to capture `!Send` types fails at compile time:
///
/// ```compile_fail,E0277
/// # // This test demonstrates that Rc cannot be captured
/// use std::rc::Rc;
/// fn require_send<T: Send>(_: &T) {}
/// fn test_rc_rejected<'r, P: asupersync::types::Policy>(
/// scope: &asupersync::cx::Scope<'r, P>,
/// state: &mut asupersync::runtime::RuntimeState,
/// cx: &asupersync::cx::Cx,
/// ) {
/// let rc = Rc::new(42);
/// require_send(&rc);
/// let _ = scope.spawn(state, cx, move |_| async move {
/// let _ = rc; // Rc<i32> is not Send
/// });
/// }
/// ```
///
/// Attempting to capture non-`'static` references fails:
///
/// ```compile_fail,E0597
/// # // This test demonstrates that borrowed data cannot be captured
/// fn require_static<T: 'static>(_: T) {}
/// fn test_borrow_rejected<'r, P: asupersync::types::Policy>(
/// scope: &asupersync::cx::Scope<'r, P>,
/// state: &mut asupersync::runtime::RuntimeState,
/// cx: &asupersync::cx::Cx,
/// ) {
/// let local = 42;
/// let borrow = &local;
/// require_static(borrow);
/// let _ = scope.spawn(state, cx, move |_| async move {
/// let _ = borrow; // &i32 is not 'static
/// });
/// }
/// ```
pub fn spawn<F, Fut, Caps>(
&self,
state: &mut RuntimeState,
cx: &Cx<Caps>,
f: F,
) -> Result<(TaskHandle<Fut::Output>, StoredTask), SpawnError>
where
Caps: cap::HasSpawn + Send + Sync + 'static,
F: FnOnce(Cx<Caps>) -> Fut + Send + 'static,
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
// Create oneshot channel for result delivery
let (tx, rx) = oneshot::channel::<Result<Fut::Output, JoinError>>();
// Create task record
let task_id = self.create_task_record(state)?;
// Trace task spawn event
let _span = debug_span!(
"task_spawn",
task_id = ?task_id,
region_id = ?self.region,
initial_state = "Created",
budget_deadline = ?self.budget.deadline,
budget_poll_quota = self.budget.poll_quota,
budget_cost_quota = ?self.budget.cost_quota,
budget_priority = self.budget.priority,
budget_source = "scope"
)
.entered();
debug!(
task_id = ?task_id,
region_id = ?self.region,
initial_state = "Created",
budget_deadline = ?self.budget.deadline,
budget_poll_quota = self.budget.poll_quota,
budget_cost_quota = ?self.budget.cost_quota,
budget_priority = self.budget.priority,
budget_source = "scope",
"task spawned"
);
let (child_cx, child_cx_full) = self.build_child_task_cx(state, cx, task_id);
// Create the TaskHandle
let handle = TaskHandle::new(task_id, rx, Arc::downgrade(&child_cx.inner));
// Set the shared inner state in the TaskRecord
// This links the user-facing Cx to the runtime's TaskRecord
if let Some(record) = state.task_mut(task_id) {
record.set_cx_inner(child_cx.inner.clone());
record.set_cx(child_cx_full.clone());
}
// Capture child_cx for result sending
let cx_for_send = child_cx_full;
// Instantiate the future with the child context.
// We use a guard to rollback task creation if the factory panics.
// This prevents zombie tasks (recorded but never started) which would
// cause the region to never close (deadlock).
let future = {
struct TaskCreationGuard<'a> {
state: &'a mut RuntimeState,
task_id: TaskId,
region_id: RegionId,
committed: bool,
}
impl Drop for TaskCreationGuard<'_> {
fn drop(&mut self) {
if !self.committed {
// Rollback task creation
if let Some(region) = self.state.region_mut(self.region_id) {
region.remove_task(self.task_id);
}
self.state.remove_task(self.task_id);
}
}
}
let mut guard = TaskCreationGuard {
state,
task_id,
region_id: self.region,
committed: false,
};
let fut = f(child_cx);
guard.committed = true;
fut
};
// Wrap the future to send its result through the channel
// We use CatchUnwind to ensure panics are propagated as JoinError::Panicked
// rather than silent channel closure (which looks like cancellation).
let wrapped = async move {
let result_result = CatchUnwind { inner: future }.await;
match result_result {
Ok(result) => {
let _ = tx.send(&cx_for_send, Ok(result));
crate::types::Outcome::Ok(())
}
Err(payload) => {
let msg = payload_to_string(&payload);
let panic_payload = PanicPayload::new(msg);
let _ = tx.send(
&cx_for_send,
Err(JoinError::Panicked(panic_payload.clone())),
);
crate::types::Outcome::Panicked(panic_payload)
}
}
};
// Create stored task with task_id for poll tracing
let stored = StoredTask::new_with_id(wrapped, task_id);
Ok((handle, stored))
}
/// Spawns a Send task (explicit Task Tier API).
///
/// This is an explicit alias for [`spawn`](Self::spawn) that makes the
/// execution tier clear in the API. Use this when you want to emphasize
/// that the task may migrate between workers.
///
/// # Type Bounds (Soundness Rules)
///
/// Same as [`spawn`](Self::spawn):
/// - `F: FnOnce(Cx) -> Fut + Send + 'static`
/// - `Fut: Future + Send + 'static`
/// - `Fut::Output: Send + 'static`
///
/// # Example
///
/// ```ignore
/// // Explicit task tier spawn
/// let (handle, stored) = scope.spawn_task(&mut state, &cx, |cx| async move {
/// // This task may run on any worker
/// compute_parallel().await
/// })?;
/// ```
#[inline]
pub fn spawn_task<F, Fut, Caps>(
&self,
state: &mut RuntimeState,
cx: &Cx<Caps>,
f: F,
) -> Result<(TaskHandle<Fut::Output>, StoredTask), SpawnError>
where
Caps: cap::HasSpawn + Send + Sync + 'static,
F: FnOnce(Cx<Caps>) -> Fut + Send + 'static,
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
self.spawn(state, cx, f)
}
/// Spawns a task and registers it with the runtime state.
///
/// This is a convenience method that combines `spawn()` with
/// `RuntimeState::store_spawned_task()`. It's the primary method
/// used by the `spawn!` macro.
///
/// # Arguments
///
/// * `state` - The runtime state (for storing the task)
/// * `cx` - The capability context (for creating child context)
/// * `f` - A closure that produces the future, receiving the new task's `Cx`
///
/// # Returns
///
/// A `TaskHandle<T>` for awaiting the task's result.
///
/// # Example
///
/// ```ignore
/// let handle = scope.spawn_registered(&mut state, &cx, |cx| async move {
/// cx.trace("Child task running");
/// compute_value().await
/// })?;
///
/// let result = handle.join(&cx).await?;
/// ```
pub fn spawn_registered<F, Fut, Caps>(
&self,
state: &mut RuntimeState,
cx: &Cx<Caps>,
f: F,
) -> Result<TaskHandle<Fut::Output>, SpawnError>
where
Caps: cap::HasSpawn + Send + Sync + 'static,
F: FnOnce(Cx<Caps>) -> Fut + Send + 'static,
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
let (handle, stored) = self.spawn(state, cx, f)?;
state.store_spawned_task(handle.task_id(), stored);
Ok(handle)
}
/// Spawns a local (non-Send) task within this scope's region (**Fiber Tier**).
///
/// This is the **Fiber Tier** spawn method. Local tasks are pinned to the
/// current worker thread and cannot be stolen by other workers. This enables
/// borrow-friendly execution with `!Send` types like `Rc` or `RefCell`.
///
/// # Execution Tier: Fiber
///
/// | Property | Value |
/// |----------|-------|
/// | Migration | Never (thread-pinned) |
/// | Send bound | Not required |
/// | Borrowing | Requires `'static` (no local `&T`) |
/// | Use case | `!Send` types, borrowed data |
///
/// # Arguments
///
/// * `state` - The runtime state
/// * `cx` - The capability context
/// * `f` - A closure that produces the future, receiving the new task's `Cx`
///
/// # Panics
///
/// Panics if called from a blocking thread (spawn_blocking context).
///
/// # Example
///
/// ```ignore
/// use std::rc::Rc;
/// use std::cell::RefCell;
///
/// let counter = Rc::new(RefCell::new(0));
/// let counter_clone = counter.clone();
///
/// let handle = scope.spawn_local(&mut state, &cx, |cx| async move {
/// // Rc<RefCell<_>> is !Send but allowed in local tasks
/// *counter_clone.borrow_mut() += 1;
/// });
/// ```
#[allow(clippy::too_many_lines)]
pub fn spawn_local<F, Fut, Caps>(
&self,
state: &mut RuntimeState,
cx: &Cx<Caps>,
f: F,
) -> Result<TaskHandle<Fut::Output>, SpawnError>
where
Caps: cap::HasSpawn + Send + Sync + 'static,
F: FnOnce(Cx<Caps>) -> Fut + 'static,
Fut: Future + 'static,
Fut::Output: Send + 'static,
{
use crate::runtime::stored_task::LocalStoredTask;
use crate::runtime::task_handle::JoinError;
// Create oneshot channel for result delivery
let (result_tx, rx) = oneshot::channel::<Result<Fut::Output, JoinError>>();
// Create task record
let task_id = self.create_task_record(state)?;
// Trace task spawn event
let _span = debug_span!(
"task_spawn",
task_id = ?task_id,
region_id = ?self.region,
initial_state = "Created",
budget_deadline = ?self.budget.deadline,
budget_poll_quota = self.budget.poll_quota,
budget_cost_quota = ?self.budget.cost_quota,
budget_priority = self.budget.priority,
budget_source = "scope_local"
)
.entered();
debug!(
task_id = ?task_id,
region_id = ?self.region,
initial_state = "Created",
budget_deadline = ?self.budget.deadline,
budget_poll_quota = self.budget.poll_quota,
budget_cost_quota = ?self.budget.cost_quota,
budget_priority = self.budget.priority,
budget_source = "scope_local",
"local task spawned"
);
let (child_cx, child_cx_full) = self.build_child_task_cx(state, cx, task_id);
// Create the TaskHandle
let handle = TaskHandle::new(task_id, rx, Arc::downgrade(&child_cx.inner));
// Set the shared inner state in the TaskRecord
if let Some(record) = state.task_mut(task_id) {
record.set_cx_inner(child_cx.inner.clone());
record.set_cx(child_cx_full.clone());
}
// Capture child_cx for result sending
let cx_for_send = child_cx_full;
// Instantiate the future with the child context.
// We use a guard to rollback task creation if the factory panics.
let future = {
struct TaskCreationGuard<'a> {
state: &'a mut RuntimeState,
task_id: TaskId,
region_id: RegionId,
committed: bool,
}
impl Drop for TaskCreationGuard<'_> {
fn drop(&mut self) {
if !self.committed {
// Rollback task creation
if let Some(region) = self.state.region_mut(self.region_id) {
region.remove_task(self.task_id);
}
self.state.remove_task(self.task_id);
}
}
}
let mut guard = TaskCreationGuard {
state,
task_id,
region_id: self.region,
committed: false,
};
let fut = f(child_cx);
guard.committed = true;
fut
};
// Wrap the future to send its result through the channel
let wrapped = async move {
let result_result = CatchUnwind { inner: future }.await;
match result_result {
Ok(result) => {
let _ = result_tx.send(&cx_for_send, Ok(result));
crate::types::Outcome::Ok(())
}
Err(payload) => {
let msg = payload_to_string(&payload);
let panic_payload = PanicPayload::new(msg);
let _ = result_tx.send(
&cx_for_send,
Err(JoinError::Panicked(panic_payload.clone())),
);
crate::types::Outcome::Panicked(panic_payload)
}
}
};
// Create local stored task
let stored = LocalStoredTask::new_with_id(wrapped, task_id);
// Store in thread-local storage
crate::runtime::local::store_local_task(task_id, stored);
// Mark the task record as local so that safety guards in the scheduler
// (inject_ready panic, try_steal debug_assert) can detect accidental
// cross-thread migration of !Send futures.
if let Some(record) = state.task_mut(task_id) {
if let Some(worker_id) = crate::runtime::scheduler::three_lane::current_worker_id() {
record.pin_to_worker(worker_id);
} else {
record.mark_local();
}
record.wake_state.notify();
}
// Schedule the task on the current worker's NON-STEALABLE local scheduler.
// spawn_local tasks MUST NOT be stealable.
let scheduled = crate::runtime::scheduler::three_lane::schedule_local_task(task_id);
if scheduled {
if let Some(record) = state.task(task_id) {
let _ = record.wake_state.notify();
}
return Ok(handle);
}
// No local scheduler available: rollback to avoid a permanently parked task.
let _ = crate::runtime::local::remove_local_task(task_id);
if let Some(region) = state.region(self.region) {
region.remove_task(task_id);
}
state.remove_task(task_id);
Err(SpawnError::LocalSchedulerUnavailable)
}
/// Spawns a blocking operation on a dedicated thread pool.
///
/// This is used for CPU-bound or legacy synchronous operations that
/// should not block async workers. The closure runs on a separate
/// thread pool designed for blocking work.
///
/// # Arguments
///
/// * `state` - The runtime state
/// * `cx` - The capability context
/// * `f` - The blocking closure to run, receiving a context
///
/// # Type Bounds
///
/// * `F: FnOnce(Cx) -> R + Send + 'static` - The closure must be Send
/// * `R: Send + 'static` - The result must be Send
///
/// # Example
///
/// ```ignore
/// let (handle, stored) = scope.spawn_blocking(&mut state, &cx, |cx| {
/// cx.trace("Starting blocking work");
/// // CPU-intensive work
/// expensive_computation()
/// });
///
/// let result = handle.join(&cx).await?;
/// ```
///
/// # Note
///
/// In Phase 0 (single-threaded), blocking operations run inline.
/// A proper blocking pool is implemented in Phase 1+.
pub fn spawn_blocking<F, R, Caps>(
&self,
state: &mut RuntimeState,
cx: &Cx<Caps>, // Parent Cx
f: F,
) -> Result<(TaskHandle<R>, StoredTask), SpawnError>
where
Caps: cap::HasSpawn + Send + Sync + 'static,
F: FnOnce(Cx<Caps>) -> R + Send + 'static,
R: Send + 'static,
{
// Create oneshot channel for result delivery
let (tx, rx) = oneshot::channel::<Result<R, JoinError>>();
// Create task record
let task_id = self.create_task_record(state)?;
// Trace task spawn event
debug!(
task_id = ?task_id,
region_id = ?self.region,
initial_state = "Created",
poll_quota = self.budget.poll_quota,
spawn_kind = "blocking",
"blocking task spawned"
);
let (child_cx, child_cx_full) = self.build_child_task_cx(state, cx, task_id);
// Create the TaskHandle
let handle = TaskHandle::new(task_id, rx, Arc::downgrade(&child_cx.inner));
// Set the shared inner state in the TaskRecord
if let Some(record) = state.task_mut(task_id) {
record.set_cx_inner(child_cx.inner.clone());
record.set_cx(child_cx_full.clone());
}
// Capture child_cx for result sending
let cx_for_send = child_cx_full;
// For Phase 0, we run blocking code as an async task
// In Phase 1+, this would spawn on a blocking thread pool
let wrapped = async move {
// Execute the blocking closure with child context
// Catch panics to report them correctly
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(child_cx)));
match result {
Ok(res) => {
let _ = tx.send(&cx_for_send, Ok(res));
crate::types::Outcome::Ok(())
}
Err(payload) => {
let msg = payload_to_string(&payload);
let panic_payload = PanicPayload::new(msg);
let _ = tx.send(
&cx_for_send,
Err(JoinError::Panicked(panic_payload.clone())),
);
crate::types::Outcome::Panicked(panic_payload)
}
}
};
let stored = StoredTask::new_with_id(wrapped, task_id);
Ok((handle, stored))
}
// =========================================================================
// Child Regions
// =========================================================================
/// Creates a child region and runs the provided future within a child scope.
///
/// The child region inherits the parent's budget by default. Use
/// [`Scope::region_with_budget`] to tighten constraints for the child.
///
/// The returned outcome is the result of the body future. After the body
/// completes, the child region begins its close sequence and advances until
/// it can close (assuming all child tasks have completed and obligations are resolved).
///
/// # Errors
///
/// Returns [`RegionCreateError`] if the parent is closed, missing, or at capacity.
pub async fn region<P2, F, Fut, T, Caps>(
&self,
state: &mut RuntimeState,
cx: &Cx<Caps>,
policy: P2,
f: F,
) -> Result<Outcome<T, P2::Error>, RegionCreateError>
where
P2: Policy,
F: FnOnce(Scope<'_, P2>, &mut RuntimeState) -> Fut,
Fut: Future<Output = Outcome<T, P2::Error>>,
{
self.region_with_budget(state, cx, self.budget, policy, f)
.await
}
/// Creates a child region with an explicit budget (met with the parent budget).
///
/// The effective budget is `parent.meet(child)` to ensure nested scopes can
/// never relax constraints.
pub async fn region_with_budget<P2, F, Fut, T, Caps>(
&self,
state: &mut RuntimeState,
_cx: &Cx<Caps>,
budget: Budget,
_policy: P2,
f: F,
) -> Result<Outcome<T, P2::Error>, RegionCreateError>
where
P2: Policy,
F: FnOnce(Scope<'_, P2>, &mut RuntimeState) -> Fut,
Fut: Future<Output = Outcome<T, P2::Error>>,
{
let child_region = state.create_child_region(self.region, budget)?;
let child_budget = state
.region(child_region)
.map_or(self.budget, crate::record::RegionRecord::budget);
let child_scope = Scope::<P2>::new(child_region, child_budget);
let fut_result =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(child_scope, &mut *state)));
let fut = match fut_result {
Ok(fut) => fut,
Err(payload) => {
let reason = CancelReason::fail_fast().with_region(child_region);
let _ = state.cancel_request(child_region, &reason, None);
if let Some(region) = state.region(child_region) {
region.begin_close(None);
}
state.advance_region_state(child_region);
std::panic::resume_unwind(payload);
}
};
let pinned_fut = std::pin::pin!(CatchUnwind { inner: fut });
let runner = RegionRunner {
fut: pinned_fut,
state: Some(state),
child_region,
};
let (result, state) = runner.await;
let outcome = match result {
Ok(outcome) => outcome,
Err(payload) => {
let msg = payload_to_string(&payload);
Outcome::Panicked(PanicPayload::new(msg))
}
};
match &outcome {
Outcome::Ok(_) => {
if let Some(region) = state.region(child_region) {
region.begin_close(None);
}
}
Outcome::Cancelled(reason) => {
let _ = state.cancel_request(child_region, reason, None);
if let Some(region) = state.region(child_region) {
region.begin_close(None);
}
}
Outcome::Err(_) | Outcome::Panicked(_) => {
let reason = CancelReason::fail_fast().with_region(child_region);
let _ = state.cancel_request(child_region, &reason, None);
if let Some(region) = state.region(child_region) {
region.begin_close(None);
}
}
}
let close_notify = state.region(child_region).map(|r| r.close_notify.clone());
state.advance_region_state(child_region);
if let Some(notify) = close_notify {
RegionCloseFuture { state: notify }.await;
}
Ok(outcome)
}
// =========================================================================
// Combinators
// =========================================================================
/// Joins two tasks, waiting for both to complete.
///
/// This method waits for both tasks to complete, regardless of their outcome.
/// It returns a tuple of results.
///
/// # Example
/// ```ignore
/// let (h1, _) = scope.spawn(...);
/// let (h2, _) = scope.spawn(...);
/// let (r1, r2) = scope.join(cx, h1, h2).await;
/// ```
pub async fn join<T1, T2>(
&self,
cx: &Cx,
mut h1: TaskHandle<T1>,
mut h2: TaskHandle<T2>,
) -> (Result<T1, JoinError>, Result<T2, JoinError>) {
let mut f1 = h1.join(cx);
let mut f2 = h2.join(cx);
let r1 = std::pin::Pin::new(&mut f1).await;
let r2 = std::pin::Pin::new(&mut f2).await;
(r1, r2)
}
/// Races two tasks, waiting for the first to complete.
///
/// The loser is cancelled and drained (awaited until it completes cancellation).
///
/// # Example
/// ```ignore
/// let (h1, _) = scope.spawn(...);
/// let (h2, _) = scope.spawn(...);
/// match scope.race(cx, h1, h2).await {
/// Ok(val) => println!("Winner result: {val}"),
/// Err(e) => println!("Race failed: {e}"),
/// }
/// ```
fn best_effort_poll_loser_join<T>(cx: &Cx, handle: &mut TaskHandle<T>) {
let mut drain = std::pin::pin!(handle.join(cx));
let waker = std::task::Waker::noop();
let mut poll_cx = std::task::Context::from_waker(waker);
let _ = drain.as_mut().poll(&mut poll_cx);
}
/// Races two task handles and returns the winner while draining the loser.
pub async fn race<T>(
&self,
cx: &Cx,
mut h1: TaskHandle<T>,
mut h2: TaskHandle<T>,
) -> Result<T, JoinError> {
let winner = {
let f1 = h1.join_with_drop_reason(cx, CancelReason::race_loser());
let mut f1 = std::pin::pin!(f1);
let f2 = h2.join_with_drop_reason(cx, CancelReason::race_loser());
let mut f2 = std::pin::pin!(f2);
Select::new(f1.as_mut(), f2.as_mut())
.await
.map_err(|_| JoinError::PolledAfterCompletion)?
};
match winner {
Either::Left(res) => {
if matches!(&res, Err(JoinError::Panicked(_)))
&& crate::runtime::scheduler::three_lane::current_worker_id().is_none()
{
// In direct block_on tests there is no scheduler driving the
// loser task after the winner panic surfaces. Best-effort poll
// once so a cooperative loser can observe cancellation, then
// preserve the winner panic without deadlocking the test.
Self::best_effort_poll_loser_join(cx, &mut h2);
return res;
}
let loser_res = h2.join(cx).await;
if let Err(JoinError::Panicked(p)) = res {
Err(JoinError::Panicked(p))
} else if let Err(JoinError::Panicked(p)) = loser_res {
Err(JoinError::Panicked(p))
} else {
res
}
}
Either::Right(res) => {
if matches!(&res, Err(JoinError::Panicked(_)))
&& crate::runtime::scheduler::three_lane::current_worker_id().is_none()
{
// See the left-branch comment above.
Self::best_effort_poll_loser_join(cx, &mut h1);
return res;
}
let loser_res = h1.join(cx).await;
if let Err(JoinError::Panicked(p)) = res {
Err(JoinError::Panicked(p))
} else if let Err(JoinError::Panicked(p)) = loser_res {
Err(JoinError::Panicked(p))
} else {
res
}
}
}
}
/// Hedges a primary operation with a backup operation.
///
/// 1. Spawns the primary task immediately.
/// 2. Waits for the delay.
/// 3. If primary finishes before delay: returns primary result.
/// 4. If delay fires: spawns backup task and races them.
///
/// The loser is cancelled and drained.
///
/// # Arguments
/// * `state` - The runtime state
/// * `cx` - The capability context
/// * `delay` - The hedge delay
/// * `primary` - The primary future factory
/// * `backup` - The backup future factory
///
/// # Returns
/// `Ok(T)` if successful, `Err(JoinError)` if failed/cancelled.
pub async fn hedge<F1, Fut1, F2, Fut2, T>(
&self,
state: &mut RuntimeState,
cx: &Cx,
delay: std::time::Duration,
primary: F1,
backup: F2,
) -> Result<T, JoinError>
where
F1: FnOnce(Cx) -> Fut1 + Send + 'static,
Fut1: Future<Output = T> + Send + 'static,
F2: FnOnce(Cx) -> Fut2 + Send + 'static,
Fut2: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
use crate::combinator::Either;
use crate::combinator::select::Select;
// 1. Spawn primary
let mut h1 = self
.spawn_registered(state, cx, primary)
.map_err(|_| JoinError::Cancelled(CancelReason::resource_unavailable()))?;
// 2. Race primary vs delay.
// Scope the pinned join future so we can safely reuse h1 afterwards.
let primary_or_delay = {
let f1_primary = h1.join(cx);
let mut f1_primary = std::pin::pin!(f1_primary);
let now = cx
.timer_driver()
.map_or_else(crate::time::wall_now, |d| d.now());
let sleep_fut = crate::time::sleep(now, delay);
let mut sleep_pinned = std::pin::pin!(sleep_fut);
let res = Select::new(f1_primary.as_mut(), sleep_pinned.as_mut())
.await
.map_err(|_| JoinError::PolledAfterCompletion)?;
if matches!(res, Either::Right(())) {
f1_primary.defuse_drop_abort();
}
res
};
match primary_or_delay {
Either::Left(res) => {
// Primary finished first
res
}
Either::Right(()) => {
// Timeout fired. Spawn backup.
let Ok(mut h2) = self.spawn_registered(state, cx, backup) else {
// Backup admission failed after primary already started.
// Request cancellation on primary to avoid orphaned work.
h1.abort_with_reason(CancelReason::resource_unavailable());
if crate::runtime::scheduler::three_lane::current_worker_id().is_some() {
// In scheduler-backed runtime execution, fully drain the
// cancelled primary before returning.
match h1.join(cx).await {
Ok(res) => return Ok(res),
Err(JoinError::Panicked(p)) => return Err(JoinError::Panicked(p)),
Err(JoinError::Cancelled(_) | JoinError::PolledAfterCompletion) => {}
}
} else {
// In no-scheduler contexts (e.g. direct unit-test block_on),
// full join can deadlock because nothing drives stored tasks.
// Keep this as best-effort and return promptly.
let mut drain = std::pin::pin!(h1.join(cx));
let waker = std::task::Waker::noop();
let mut poll_cx = Context::from_waker(waker);
match drain.as_mut().poll(&mut poll_cx) {
std::task::Poll::Ready(Ok(res)) => return Ok(res),
std::task::Poll::Ready(Err(JoinError::Panicked(p))) => {
return Err(JoinError::Panicked(p));
}
_ => {}
}
}
return Err(JoinError::Cancelled(CancelReason::resource_unavailable()));
};
// Now race h1 and h2 with bounded future borrows.
let race_outcome = {
let f1_race = h1.join_with_drop_reason(cx, CancelReason::race_loser());
let mut f1_race = std::pin::pin!(f1_race);
let f2_race = h2.join_with_drop_reason(cx, CancelReason::race_loser());
let mut f2_race = std::pin::pin!(f2_race);
Select::new(f1_race.as_mut(), f2_race.as_mut())
.await
.map_err(|_| JoinError::PolledAfterCompletion)?
};
match race_outcome {
Either::Left(res) => {
if matches!(&res, Err(JoinError::Panicked(_)))
&& crate::runtime::scheduler::three_lane::current_worker_id().is_none()
{
Self::best_effort_poll_loser_join(cx, &mut h2);
return res;
}
let loser_res = h2.join(cx).await;
if let Err(JoinError::Panicked(p)) = res {
Err(JoinError::Panicked(p))
} else if let Err(JoinError::Panicked(p)) = loser_res {
Err(JoinError::Panicked(p))
} else {
res
}
}
Either::Right(res) => {
if matches!(&res, Err(JoinError::Panicked(_)))
&& crate::runtime::scheduler::three_lane::current_worker_id().is_none()
{
Self::best_effort_poll_loser_join(cx, &mut h1);
return res;
}
let loser_res = h1.join(cx).await;
if let Err(JoinError::Panicked(p)) = res {
Err(JoinError::Panicked(p))
} else if let Err(JoinError::Panicked(p)) = loser_res {
Err(JoinError::Panicked(p))
} else {
res
}
}
}
}
}
}
/// Races multiple tasks, waiting for the first to complete.
///
/// The winner's result is returned. Losers are cancelled and drained.
///
/// # Arguments
/// * `cx` - The capability context
/// * `handles` - Vector of task handles to race
///
/// # Returns
/// `Ok((value, index))` if the winner succeeded.
/// `Err(e)` if the winner failed (error/cancel/panic).
pub async fn race_all<T>(
&self,
cx: &Cx,
handles: Vec<TaskHandle<T>>,
) -> Result<(T, usize), JoinError> {
let mut handles = handles;
if handles.is_empty() {
return std::future::pending().await;
}
let mut futures: Vec<_> = handles
.iter_mut()
.map(|h| h.join_with_drop_reason(cx, CancelReason::race_loser()))
.collect();
let mut ready_results: Vec<Option<Result<T, JoinError>>> = std::iter::repeat_with(|| None)
.take(futures.len())
.collect();
// Poll every candidate in each round and keep all same-round ready
// outcomes. This prevents losing loser panic outcomes when multiple
// tasks become ready in the same poll.
let winner_idx = std::future::poll_fn(|poll_cx| {
let mut newly_ready = Vec::new();
for (i, future) in futures.iter_mut().enumerate() {
if ready_results[i].is_some() {
continue;
}
if let std::task::Poll::Ready(res) = std::pin::Pin::new(future).poll(poll_cx) {
ready_results[i] = Some(res);
newly_ready.push(i);
}
}
if newly_ready.is_empty() {
std::task::Poll::Pending
} else {
// Fairly select a winner among all that became ready in this round
let chosen = newly_ready[cx.random_usize(newly_ready.len())];
std::task::Poll::Ready(chosen)
}
})
.await;
let winner_result = ready_results[winner_idx]
.take()
.expect("winner index must have a ready result");
// Release mutable borrows of handles held by JoinFuture values before
// explicit loser cancellation/join.
drop(futures);
// Drain completed losers first so terminal panic outcomes are not
// obscured by strengthening cancellation reasons on already-finished tasks.
let mut loser_panic = None;
let mut pending_loser_indices = Vec::new();
for (i, handle) in handles.iter_mut().enumerate() {
if i == winner_idx {
continue;
}
if let Some(res) = ready_results[i].take() {
if let Err(JoinError::Panicked(p)) = res {
if loser_panic.is_none() {
loser_panic = Some(p);
}
}
} else if handle.is_finished() {
let res = handle.join(cx).await;
if let Err(JoinError::Panicked(p)) = res {
if loser_panic.is_none() {
loser_panic = Some(p);
}
}
} else {
pending_loser_indices.push(i);
}
}
// Cancel and drain unfinished losers.
// Note: Losers may also already have a race-loser reason from dropped
// join futures; strengthening keeps attribution deterministic.
for &idx in &pending_loser_indices {
handles[idx].abort_with_reason(CancelReason::race_loser());
}
if matches!(&winner_result, Err(JoinError::Panicked(_)))
&& crate::runtime::scheduler::three_lane::current_worker_id().is_none()
{
// In direct block_on tests there is no scheduler driving pending
// losers after the winner panic surfaces. Best-effort poll each
// loser once so cooperative tasks can observe cancellation, then
// preserve the winner panic without deadlocking the test.
for idx in pending_loser_indices {
Self::best_effort_poll_loser_join(cx, &mut handles[idx]);
}
return winner_result.map(|val| (val, winner_idx));
}
for idx in pending_loser_indices {
let res = handles[idx].join(cx).await;
if let Err(JoinError::Panicked(p)) = res {
if loser_panic.is_none() {
loser_panic = Some(p);
}
}
}
let winner_result = winner_result.map(|val| (val, winner_idx));
if matches!(&winner_result, Err(JoinError::Panicked(_))) {
return winner_result;
}
loser_panic.map_or(winner_result, |panic_payload| {
Err(JoinError::Panicked(panic_payload))
})
}
/// Joins multiple tasks, waiting for all to complete.
///
/// Returns a vector of results in the same order as the input handles.
pub async fn join_all<T>(
&self,
cx: &Cx,
mut handles: Vec<TaskHandle<T>>,
) -> Vec<Result<T, JoinError>> {
let mut futures: Vec<_> = handles.iter_mut().map(|h| h.join(cx)).collect();
let mut results = Vec::with_capacity(futures.len());
for fut in &mut futures {
results.push(std::pin::Pin::new(fut).await);
}
results
}
pub(crate) fn build_child_task_cx<Caps>(
&self,
state: &RuntimeState,
parent_cx: &Cx<Caps>,
task_id: TaskId,
) -> (Cx<Caps>, Cx<cap::All>) {
let child_observability = parent_cx.child_observability(self.region, task_id);
let child_entropy = parent_cx.child_entropy(task_id);
let io_driver = state.io_driver_handle();
let timer_driver = state.timer_driver_handle();
let logical_clock = state
.logical_clock_mode()
.build_handle(timer_driver.clone());
let child_cx = Cx::<Caps>::new_with_drivers(
self.region,
task_id,
self.budget,
Some(child_observability),
io_driver,
parent_cx.io_cap_handle(),
timer_driver,
Some(child_entropy),
)
.with_logical_clock(logical_clock)
.with_registry_handle(parent_cx.registry_handle())
.with_remote_cap_handle(parent_cx.remote_cap_handle())
.with_blocking_pool_handle(parent_cx.blocking_pool_handle())
.with_evidence_sink(parent_cx.evidence_sink_handle())
.with_macaroon_handle(parent_cx.macaroon_handle());
let child_cx = if let Some(pressure) = parent_cx.pressure_handle() {
child_cx.with_pressure(pressure)
} else {
child_cx
};
child_cx.set_trace_buffer(state.trace_handle());
let child_cx_full = child_cx.retype::<cap::All>();
(child_cx, child_cx_full)
}
/// Creates a task record in the runtime state.
///
/// This is a helper method used by all spawn variants.
pub(crate) fn create_task_record(
&self,
state: &mut RuntimeState,
) -> Result<TaskId, SpawnError> {
use crate::util::ArenaIndex;
let now = state
.timer_driver()
.map_or(state.now, crate::time::TimerDriverHandle::now);
// Create placeholder task record
let idx = state.insert_task(TaskRecord::new_with_time(
TaskId::from_arena(ArenaIndex::new(0, 0)), // placeholder ID
self.region,
self.budget,
now,
));
// Get the real task ID from the arena index
let task_id = TaskId::from_arena(idx);
// Update the task record with the correct ID
if let Some(record) = state.task_mut(task_id) {
record.id = task_id;
}
// Add task to the owning region
if let Some(region) = state.region(self.region) {
if let Err(err) = region.add_task(task_id) {
// Rollback task creation
state.remove_task(task_id);
return Err(match err {
AdmissionError::Closed => SpawnError::RegionClosed(self.region),
AdmissionError::LimitReached { limit, live, .. } => {
SpawnError::RegionAtCapacity {
region: self.region,
limit,
live,
}
}
});
}
} else {
// Rollback task creation
state.remove_task(task_id);
return Err(SpawnError::RegionNotFound(self.region));
}
state.record_task_spawn(task_id, self.region);
Ok(task_id)
}
// =========================================================================
// Finalizer Registration
// =========================================================================
/// Registers a synchronous finalizer to run when the region closes.
///
/// Finalizers are stored in LIFO order and executed during the Finalizing
/// phase, after all children have completed. Use this for lightweight
/// cleanup that doesn't need to await.
///
/// # Arguments
/// * `state` - The runtime state
/// * `f` - The synchronous cleanup function
///
/// # Returns
/// `true` if the finalizer was registered successfully.
///
/// # Example
/// ```ignore
/// scope.defer_sync(&mut state, || {
/// println!("Cleaning up!");
/// });
/// ```
pub fn defer_sync<F>(&self, state: &mut RuntimeState, f: F) -> bool
where
F: FnOnce() + Send + 'static,
{
state.register_sync_finalizer(self.region, f)
}
/// Registers an asynchronous finalizer to run when the region closes.
///
/// Async finalizers run under a cancel mask to prevent interruption.
/// They are driven to completion with a bounded budget. Use this for
/// cleanup that needs to perform async operations (e.g., closing
/// connections, flushing buffers).
///
/// # Arguments
/// * `state` - The runtime state
/// * `future` - The async cleanup future
///
/// # Returns
/// `true` if the finalizer was registered successfully.
///
/// # Example
/// ```ignore
/// scope.defer_async(&mut state, async {
/// close_connection().await;
/// });
/// ```
pub fn defer_async<F>(&self, state: &mut RuntimeState, future: F) -> bool
where
F: Future<Output = ()> + Send + 'static,
{
state.register_async_finalizer(self.region, future)
}
}
impl<P: Policy> std::fmt::Debug for Scope<'_, P> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Scope")
.field("region", &self.region)
.field("budget", &self.budget)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::record::RegionLimits;
use crate::runtime::RuntimeState;
use crate::types::{CancelKind, Outcome, Time};
use crate::util::ArenaIndex;
use futures_lite::future::block_on;
use std::sync::Arc;
fn test_cx() -> Cx {
Cx::new(
RegionId::from_arena(ArenaIndex::new(0, 0)),
TaskId::from_arena(ArenaIndex::new(0, 0)),
Budget::INFINITE,
)
}
fn test_scope(region: RegionId, budget: Budget) -> Scope<'static> {
Scope::new(region, budget)
}
#[test]
fn spawn_creates_task_record() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (handle, _stored) = scope.spawn(&mut state, &cx, |_| async { 42_i32 }).unwrap();
// Task should exist in state
let task = state.task(handle.task_id());
assert!(task.is_some());
// Task should be owned by the region
let task = task.unwrap();
assert_eq!(task.owner, region);
}
#[test]
fn spawn_inherits_registry_and_remote_capabilities() {
use crate::cx::registry::RegistryHandle;
use crate::remote::{NodeId, RemoteCap};
use std::task::Context;
let mut state = RuntimeState::new();
let registry = crate::cx::NameRegistry::new();
let registry_handle = RegistryHandle::new(Arc::new(registry));
let parent_registry_arc = registry_handle.as_arc();
let cx = test_cx()
.with_registry_handle(Some(registry_handle))
.with_remote_cap(RemoteCap::new().with_local_node(NodeId::new("origin-test")));
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let mut handle = scope
.spawn_registered(&mut state, &cx, move |cx| async move {
let child_registry = cx.registry_handle().expect("child must inherit registry");
let child_registry_arc = child_registry.as_arc();
let same_registry = Arc::ptr_eq(&child_registry_arc, &parent_registry_arc);
let child_remote = cx.remote().expect("child must inherit remote cap");
let origin = child_remote.local_node().as_str().to_owned();
(same_registry, origin)
})
.unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
let stored = state
.get_stored_future(handle.task_id())
.expect("spawn_registered must store the task");
assert!(stored.poll(&mut poll_cx).is_ready());
let mut join_fut = std::pin::pin!(handle.join(&cx));
match join_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(Ok((same_registry, origin))) => {
assert!(
same_registry,
"child should observe the same RegistryCap instance"
);
assert_eq!(origin, "origin-test");
}
other => unreachable!("Expected Ready(Ok(_)), got {other:?}"),
}
}
#[test]
fn spawn_inherits_runtime_timer_driver() {
use std::task::Context;
let mut state = RuntimeState::new();
let clock = Arc::new(crate::time::VirtualClock::new());
state.set_timer_driver(crate::time::TimerDriverHandle::with_virtual_clock(clock));
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (mut handle, mut stored) = scope
.spawn(&mut state, &cx, |cx| async move { cx.has_timer() })
.expect("spawn should succeed");
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
assert!(stored.poll(&mut poll_cx).is_ready());
let mut join_fut = std::pin::pin!(handle.join(&cx));
match join_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(Ok(has_timer)) => assert!(has_timer),
other => unreachable!("Expected Ready(Ok(_)), got {other:?}"),
}
}
#[test]
fn create_task_record_uses_runtime_timer_driver_time() {
let mut state = RuntimeState::new();
let clock = Arc::new(crate::time::VirtualClock::starting_at(Time::from_millis(
11,
)));
state.set_timer_driver(crate::time::TimerDriverHandle::with_virtual_clock(
clock.clone(),
));
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
clock.advance(Time::from_millis(7).as_nanos());
let task_id = scope
.create_task_record(&mut state)
.expect("task record should be created");
let task = state.task(task_id).expect("task record");
assert_eq!(task.created_at, Time::from_millis(18));
}
#[test]
fn spawn_blocking_inherits_runtime_timer_driver() {
use std::task::Context;
let mut state = RuntimeState::new();
let clock = Arc::new(crate::time::VirtualClock::new());
state.set_timer_driver(crate::time::TimerDriverHandle::with_virtual_clock(clock));
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (mut handle, mut stored) = scope
.spawn_blocking(&mut state, &cx, |cx| cx.has_timer())
.expect("spawn_blocking should succeed");
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
assert!(stored.poll(&mut poll_cx).is_ready());
let mut join_fut = std::pin::pin!(handle.join(&cx));
match join_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(Ok(has_timer)) => assert!(has_timer),
other => unreachable!("Expected Ready(Ok(_)), got {other:?}"),
}
}
#[test]
fn spawn_registered_stores_task() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// spawn_registered should both create and store the task
let handle = scope
.spawn_registered(&mut state, &cx, |_| async { 42_i32 })
.unwrap();
// Task record should exist
let task = state.task(handle.task_id());
assert!(task.is_some());
assert_eq!(task.unwrap().owner, region);
// StoredTask should be registered (can be retrieved for polling)
let stored = state.get_stored_future(handle.task_id());
assert!(stored.is_some(), "spawn_registered should store the task");
}
#[test]
fn spawn_registered_task_can_be_polled() {
use std::task::Context;
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let mut handle = scope
.spawn_registered(&mut state, &cx, |_| async { 42_i32 })
.unwrap();
// Get the stored future and poll it
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
let stored = state.get_stored_future(handle.task_id()).unwrap();
let poll_result = stored.poll(&mut poll_cx);
assert!(
poll_result.is_ready(),
"Simple async should complete in one poll"
);
// Join should now have the result
let mut join_fut = std::pin::pin!(handle.join(&cx));
match join_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(Ok(val)) => assert_eq!(val, 42),
other => unreachable!("Expected Ready(Ok(42)), got {other:?}"),
}
}
#[test]
fn spawn_blocking_creates_task_record() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (handle, _stored) = scope.spawn_blocking(&mut state, &cx, |_| 42_i32).unwrap();
// Task should exist
let task = state.task(handle.task_id());
assert!(task.is_some());
assert_eq!(task.unwrap().owner, region);
}
#[test]
fn spawn_local_creates_task_record() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let local_ready = Arc::new(parking_lot::Mutex::new(std::collections::VecDeque::new()));
let _local_ready_guard =
crate::runtime::scheduler::three_lane::ScopedLocalReady::new(Arc::clone(&local_ready));
let _worker_guard = crate::runtime::scheduler::three_lane::ScopedWorkerId::new(1);
// In Phase 0, spawn_local requires Send bounds
// In Phase 1+, this will work with !Send futures
let handle = scope
.spawn_local(&mut state, &cx, |_| async move { 42_i32 })
.unwrap();
// Task should exist
let task = state.task(handle.task_id());
assert!(task.is_some());
assert_eq!(task.unwrap().owner, region);
}
#[test]
fn spawn_local_without_scheduler_fails_and_rolls_back() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let result = scope.spawn_local(&mut state, &cx, |_| async move { 5_i32 });
assert!(matches!(result, Err(SpawnError::LocalSchedulerUnavailable)));
// Task should not exist
assert!(state.tasks_is_empty());
let region_record = state.region(region).unwrap();
assert!(region_record.task_ids().is_empty());
}
#[test]
fn spawn_local_makes_progress_via_local_ready() {
use std::sync::Arc;
use std::task::Context;
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let local_ready = Arc::new(parking_lot::Mutex::new(std::collections::VecDeque::new()));
let _local_ready_guard =
crate::runtime::scheduler::three_lane::ScopedLocalReady::new(Arc::clone(&local_ready));
let _worker_guard = crate::runtime::scheduler::three_lane::ScopedWorkerId::new(1);
let mut handle = scope
.spawn_local(&mut state, &cx, |_| async move { 7_i32 })
.unwrap();
let queued = {
let queue = local_ready.lock();
queue.contains(&handle.task_id())
};
assert!(queued, "spawn_local should enqueue into local_ready");
let task_id = {
let mut queue = local_ready.lock();
queue
.pop_front()
.expect("local_ready should contain spawned task")
};
let mut join_fut = std::pin::pin!(handle.join(&cx));
let waker = std::task::Waker::noop().clone();
let mut ctx = Context::from_waker(&waker);
assert!(join_fut.as_mut().poll(&mut ctx).is_pending());
let mut local_task =
crate::runtime::local::remove_local_task(task_id).expect("local task missing");
assert!(local_task.poll(&mut ctx).is_ready());
match join_fut.as_mut().poll(&mut ctx) {
Poll::Ready(Ok(val)) => assert_eq!(val, 7),
res => unreachable!("Expected Ready(Ok(7)), got {res:?}"),
}
}
#[test]
fn task_added_to_region() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (handle, _stored) = scope.spawn(&mut state, &cx, |_| async { 42_i32 }).unwrap();
// Check region has the task
let region_record = state.region(region).unwrap();
assert!(region_record.task_ids().contains(&handle.task_id()));
}
#[test]
fn multiple_spawns_create_distinct_tasks() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (handle1, _) = scope.spawn(&mut state, &cx, |_| async { 1_i32 }).unwrap();
let (handle2, _) = scope.spawn(&mut state, &cx, |_| async { 2_i32 }).unwrap();
let (handle3, _) = scope.spawn(&mut state, &cx, |_| async { 3_i32 }).unwrap();
// All task IDs should be different
assert_ne!(handle1.task_id(), handle2.task_id());
assert_ne!(handle2.task_id(), handle3.task_id());
assert_ne!(handle1.task_id(), handle3.task_id());
// All tasks should be in the region
let region_record = state.region(region).unwrap();
assert!(region_record.task_ids().contains(&handle1.task_id()));
assert!(region_record.task_ids().contains(&handle2.task_id()));
assert!(region_record.task_ids().contains(&handle3.task_id()));
}
#[test]
fn spawn_into_closing_region_should_fail() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Transition region to Closing
let region_record = state.region_mut(region).expect("region");
region_record.begin_close(None);
// Attempt to spawn should fail
let result = scope.spawn(&mut state, &cx, |_| async { 42 });
assert!(matches!(result, Err(SpawnError::RegionClosed(_))));
}
#[test]
fn test_join_manual_poll() {
use std::task::Context;
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Spawn a task
let (mut handle, mut stored_task) =
scope.spawn(&mut state, &cx, |_| async { 42_i32 }).unwrap();
// The stored task is returned directly, not put in state by scope.spawn
// Create join future
let mut join_fut = std::pin::pin!(handle.join(&cx));
// Create waker context
let waker = std::task::Waker::noop().clone();
let mut ctx = Context::from_waker(&waker);
// Poll join - should be pending
assert!(join_fut.as_mut().poll(&mut ctx).is_pending());
// Poll stored task - should complete and send result
assert!(stored_task.poll(&mut ctx).is_ready());
// Poll join - should be ready now
match join_fut.as_mut().poll(&mut ctx) {
Poll::Ready(Ok(val)) => assert_eq!(val, 42),
other => unreachable!("Expected Ready(Ok(42)), got {other:?}"),
}
}
#[test]
fn spawn_abort_cancels_task() {
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Spawn a task that checks for cancellation
let (mut handle, mut stored_task) = scope
.spawn(&mut state, &cx, |cx| async move {
// We expect to be cancelled immediately because abort() is called before we run
if cx.checkpoint().is_err() {
return "cancelled";
}
"finished"
})
.unwrap();
// Abort the task via handle
handle.abort();
// Drive the task
let waker = std::task::Waker::noop().clone();
let mut ctx = Context::from_waker(&waker);
// Task should run, see cancellation, and return "cancelled"
match stored_task.poll(&mut ctx) {
Poll::Ready(crate::types::Outcome::Ok(())) => {}
res => unreachable!("Task should have completed with Ok(()), got {res:?}"),
}
// Check result via handle
let mut join_fut = std::pin::pin!(handle.join(&cx));
match join_fut.as_mut().poll(&mut ctx) {
Poll::Ready(Ok(val)) => assert_eq!(val, "cancelled"),
Poll::Ready(Err(e)) => unreachable!("Task failed unexpectedly: {e}"),
Poll::Pending => unreachable!("Join should be ready"),
}
}
#[test]
fn hedge_backup_spawn_failure_aborts_primary() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let limits = RegionLimits {
max_tasks: Some(1),
..RegionLimits::unlimited()
};
assert!(state.set_region_limits(region, limits));
let result = block_on(scope.hedge(
&mut state,
&cx,
std::time::Duration::ZERO,
|_| async { 1_u8 },
|_| async { 2_u8 },
));
assert!(matches!(
result,
Err(JoinError::Cancelled(reason))
if reason.kind == CancelKind::ResourceUnavailable
));
let task_id = *state
.region(region)
.expect("region missing")
.task_ids()
.first()
.expect("primary task should remain tracked");
let task = state.task(task_id).expect("primary task record missing");
let (cancel_requested, cancel_reason_kind) = {
let inner = task
.cx_inner
.as_ref()
.expect("primary task must have shared Cx inner")
.read();
(
inner.cancel_requested,
inner.cancel_reason.as_ref().map(|r| r.kind),
)
};
assert!(
cancel_requested,
"primary task must be cancellation-requested when backup spawn fails"
);
assert_eq!(cancel_reason_kind, Some(CancelKind::ResourceUnavailable));
}
#[test]
fn region_closes_empty_child() {
let mut state = RuntimeState::new();
let cx = test_cx();
let parent = state.create_root_region(Budget::INFINITE);
let scope = test_scope(parent, Budget::INFINITE);
let outcome = block_on(scope.region(
&mut state,
&cx,
crate::types::policy::FailFast,
|child, _state| {
let child_id = child.region_id();
async move { Outcome::Ok(child_id) }
},
))
.expect("child region created");
let child_id = match outcome {
Outcome::Ok(id) => id,
other => unreachable!("expected Outcome::Ok(child_id), got {other:?}"),
};
assert!(
state.region(child_id).is_none(),
"closed child region should be reclaimed from arena"
);
let parent_record = state.region(parent).expect("parent record missing");
assert!(
!parent_record.child_ids().contains(&child_id),
"closed child should be removed from parent"
);
}
#[test]
fn region_budget_is_met_with_parent() {
let mut state = RuntimeState::new();
let cx = test_cx();
let parent = state.create_root_region(Budget::with_deadline_secs(10));
let scope = test_scope(parent, Budget::with_deadline_secs(10));
let outcome = block_on(scope.region_with_budget(
&mut state,
&cx,
Budget::with_deadline_secs(30),
crate::types::policy::FailFast,
|child, _state| {
let child_id = child.region_id();
let child_budget = child.budget();
async move { Outcome::Ok((child_id, child_budget)) }
},
))
.expect("child region created");
let (child_id, child_budget) = match outcome {
Outcome::Ok(tuple) => tuple,
other => unreachable!("expected Outcome::Ok(child_id), got {other:?}"),
};
assert_eq!(
child_budget.deadline,
Some(crate::types::Time::from_secs(10))
);
assert!(
state.region(child_id).is_none(),
"closed child region should be reclaimed from arena"
);
}
#[test]
fn region_spawns_tasks_in_child() {
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let parent = state.create_root_region(Budget::INFINITE);
let scope = test_scope(parent, Budget::INFINITE);
let outcome = block_on(scope.region(
&mut state,
&cx,
crate::types::policy::FailFast,
|child, state| {
let child_id = child.region_id();
let (handle, mut stored) = child
.spawn(state, &cx, |_| async { 7_i32 })
.expect("spawn in child");
let parent_has = state
.region(parent)
.expect("parent record missing")
.task_ids()
.contains(&handle.task_id());
let child_has = state
.region(child_id)
.expect("child record missing")
.task_ids()
.contains(&handle.task_id());
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
let poll_result = stored.poll(&mut poll_cx);
if let Poll::Ready(outcome) = poll_result {
let task_outcome = match outcome {
Outcome::Ok(()) => Outcome::Ok(()),
Outcome::Panicked(payload) => Outcome::Panicked(payload),
other => unreachable!("unexpected task outcome: {other:?}"),
};
if let Some(task_record) = state.task_mut(handle.task_id()) {
task_record.complete(task_outcome);
}
let _ = state.task_completed(handle.task_id());
}
std::future::ready(Outcome::Ok((child_id, parent_has, child_has)))
},
))
.expect("child region created");
let (child_id, parent_has, child_has) = match outcome {
Outcome::Ok(tuple) => tuple,
other => unreachable!("expected Outcome::Ok(tuple), got {other:?}"),
};
assert!(!parent_has, "task should not be owned by parent region");
assert!(child_has, "task should be owned by child region");
let parent_record = state.region(parent).expect("parent record missing");
assert!(
!parent_record.child_ids().contains(&child_id),
"closed child should be removed from parent"
);
}
#[test]
fn spawn_panic_propagates_as_panicked_error() {
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (mut handle, mut stored_task) = scope
.spawn(&mut state, &cx, |_| async {
std::panic::panic_any("oops");
})
.unwrap();
// Drive the task
let waker = std::task::Waker::noop().clone();
let mut ctx = Context::from_waker(&waker);
// Polling stored task should return Ready(Panicked) even if it panics (caught inside)
match stored_task.poll(&mut ctx) {
Poll::Ready(crate::types::Outcome::Panicked(_)) => {}
res => unreachable!("Task should have completed with Panicked, got {res:?}"),
}
// Check result via handle
let mut join_fut = std::pin::pin!(handle.join(&cx));
match join_fut.as_mut().poll(&mut ctx) {
Poll::Ready(Err(JoinError::Panicked(p))) => {
assert_eq!(p.message(), "oops");
}
res => unreachable!("Expected Panicked, got {res:?}"),
}
}
#[test]
fn join_all_success() {
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (h1, mut t1) = scope.spawn(&mut state, &cx, |_| async { 1 }).unwrap();
let (h2, mut t2) = scope.spawn(&mut state, &cx, |_| async { 2 }).unwrap();
// Drive tasks to completion
let waker = std::task::Waker::noop().clone();
let mut ctx = Context::from_waker(&waker);
assert!(t1.poll(&mut ctx).is_ready());
assert!(t2.poll(&mut ctx).is_ready());
let handles = vec![h1, h2];
let mut fut = Box::pin(scope.join_all(&cx, handles));
match fut.as_mut().poll(&mut ctx) {
Poll::Ready(results) => {
assert_eq!(results.len(), 2);
assert_eq!(results[0].as_ref().unwrap(), &1);
assert_eq!(results[1].as_ref().unwrap(), &2);
}
Poll::Pending => unreachable!("join_all should be ready"),
}
}
#[test]
fn race_all_aborted_task_is_drained() {
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Task 1: completes immediately
let (h1, mut t1) = scope.spawn(&mut state, &cx, |_| async { 1 }).unwrap();
// Task 2: yields once, checking for cancellation
let (h2, mut t2) = scope
.spawn(&mut state, &cx, |cx| async move {
// Yield once to simulate running
struct YieldOnce(bool);
impl std::future::Future for YieldOnce {
type Output = ();
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<()> {
if self.0 {
std::task::Poll::Ready(())
} else {
self.0 = true;
cx.waker().wake_by_ref();
std::task::Poll::Pending
}
}
}
YieldOnce(false).await;
// Check cancellation
if cx.checkpoint().is_err() {
return 0; // Cancelled
}
2
})
.unwrap();
let waker = std::task::Waker::noop().clone();
let mut ctx = Context::from_waker(&waker);
// Drive t1 to completion (winner)
assert!(t1.poll(&mut ctx).is_ready());
// Initialize race_all
let handles = vec![h1, h2];
let mut race_fut = Box::pin(scope.race_all(&cx, handles));
// Poll race_all.
// It sees h1 ready. Winner=0.
// It aborts h2.
// It awaits h2 drain.
// h2 is still pending (hasn't run), so h2.join() returns Pending.
// race_fut returns Pending.
assert!(race_fut.as_mut().poll(&mut ctx).is_pending());
// Now drive t2. It was aborted, so it should see cancellation if checked?
// Wait, handle.abort() sets inner.cancel_requested.
// But my t2 closure yields first.
// So first poll of t2 -> YieldOnce returns Pending.
assert!(t2.poll(&mut ctx).is_pending());
// Poll race_fut again. Still waiting for h2 drain.
assert!(race_fut.as_mut().poll(&mut ctx).is_pending());
// Poll t2 again. YieldOnce finishes.
// Then it hits checkpoint(). cancel_requested is true.
// It returns 0 (simulated cancellation return).
// Actually, normally tasks return Result or are wrapped.
// Here spawn returns Result<i32>.
// My closure returns i32.
// So h2.join() will return Ok(0).
// This counts as "drained".
assert!(t2.poll(&mut ctx).is_ready());
// Now poll race_fut. h2 drain complete.
// Should return (1, 0).
match race_fut.as_mut().poll(&mut ctx) {
Poll::Ready(Ok((val, idx))) => {
assert_eq!(val, 1);
assert_eq!(idx, 0);
}
res => unreachable!("Expected Ready(Ok((1, 0))), got {res:?}"),
}
}
#[test]
fn race_surfaces_loser_panic_even_if_winner_succeeds() {
use std::task::Context;
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (h1, mut t1) = scope.spawn(&mut state, &cx, |_| async { 1_i32 }).unwrap();
let (h2, mut t2) = scope
.spawn(&mut state, &cx, |_| async {
std::panic::panic_any("loser panic");
})
.unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
assert!(t1.poll(&mut poll_cx).is_ready());
assert!(t2.poll(&mut poll_cx).is_ready());
let result = block_on(scope.race(&cx, h1, h2));
assert!(
matches!(result, Err(JoinError::Panicked(_))),
"loser panic must dominate race result, got {result:?}"
);
}
#[test]
fn race_preserves_winner_panic_over_loser_panic() {
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (h1, mut t1) = scope
.spawn(&mut state, &cx, |_| async {
std::panic::panic_any("winner panic");
})
.unwrap();
let (h2, mut t2) = scope
.spawn(&mut state, &cx, |_| {
let mut first_poll = true;
std::future::poll_fn(move |poll_cx| {
if first_poll {
first_poll = false;
poll_cx.waker().wake_by_ref();
Poll::Pending
} else {
std::panic::panic_any("loser panic");
}
})
})
.unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
assert!(t1.poll(&mut poll_cx).is_ready());
assert!(t2.poll(&mut poll_cx).is_pending());
let result = block_on(scope.race(&cx, h1, h2));
match result {
Err(JoinError::Panicked(payload)) => {
assert_eq!(payload.message(), "winner panic");
}
other => unreachable!("winner panic must dominate race result, got {other:?}"),
}
}
#[test]
fn race_all_surfaces_simultaneous_loser_panic() {
use std::task::Context;
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (h1, mut t1) = scope.spawn(&mut state, &cx, |_| async { 1_i32 }).unwrap();
let (h2, mut t2) = scope
.spawn(&mut state, &cx, |_| async {
std::panic::panic_any("simultaneous loser panic");
})
.unwrap();
let (h3, mut t3) = scope.spawn(&mut state, &cx, |_| async { 3_i32 }).unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
assert!(t1.poll(&mut poll_cx).is_ready());
assert!(t2.poll(&mut poll_cx).is_ready());
assert!(t3.poll(&mut poll_cx).is_ready());
let result = block_on(scope.race_all(&cx, vec![h1, h2, h3]));
assert!(
matches!(result, Err(JoinError::Panicked(_))),
"simultaneous loser panic must dominate race_all result, got {result:?}"
);
}
#[test]
fn race_all_preserves_winner_panic_over_loser_panic() {
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (h1, mut t1) = scope
.spawn(&mut state, &cx, |_| async {
std::panic::panic_any("winner panic");
})
.unwrap();
let (h2, mut t2) = scope
.spawn(&mut state, &cx, |_| {
let mut first_poll = true;
std::future::poll_fn(move |poll_cx| {
if first_poll {
first_poll = false;
poll_cx.waker().wake_by_ref();
Poll::Pending
} else {
std::panic::panic_any("loser panic");
}
})
})
.unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
assert!(t1.poll(&mut poll_cx).is_ready());
assert!(t2.poll(&mut poll_cx).is_pending());
let result = block_on(scope.race_all(&cx, vec![h1, h2]));
match result {
Err(JoinError::Panicked(payload)) => {
assert_eq!(payload.message(), "winner panic");
}
other => unreachable!("winner panic must dominate race_all result, got {other:?}"),
}
}
#[test]
fn race_all_empty_is_pending() {
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let fut = scope.race_all::<i32>(&cx, vec![]);
let waker = std::task::Waker::noop();
let mut poll_cx = std::task::Context::from_waker(waker);
let pinned = std::pin::pin!(fut);
let status = std::future::Future::poll(pinned, &mut poll_cx);
assert!(status.is_pending());
}
// =============================================================================
// CONFORMANCE TESTS: Structured Concurrency Invariants
// =============================================================================
//
// These tests verify the core structured concurrency guarantees that must hold
// for the spawn/join contract to be sound.
#[test]
fn conformance_spawn_creates_trackable_task() {
// INVARIANT: Every spawned task creates a trackable record that belongs to the spawning region
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (handle, _stored) = scope.spawn(&mut state, &cx, |_| async { 42_i32 }).unwrap();
// Task must exist and be trackable
let task_record = state
.task(handle.task_id())
.expect("spawned task must have a record");
// Task must belong to the spawning region
assert_eq!(
task_record.owner, region,
"spawned task must be owned by the spawning region"
);
// Region must track the task
let region_record = state.region(region).expect("spawning region must exist");
assert!(
region_record.task_ids().contains(&handle.task_id()),
"spawning region must track the spawned task"
);
}
#[test]
fn conformance_spawn_enforces_send_bounds() {
// INVARIANT: spawn() enforces Send + 'static bounds for cross-worker task migration
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// This should compile - Send + 'static data
let send_data = String::from("test");
let (handle, _stored) = scope
.spawn(&mut state, &cx, move |_| async move {
send_data.len() // Uses Send + 'static String
})
.unwrap();
// Task record should reflect Send bounds
let task_record = state
.task(handle.task_id())
.expect("Send task must have a record");
assert_eq!(task_record.owner, region);
// NOTE: Non-Send compile-time test examples are in module docstring
// (compile_fail tests with Rc<T> and borrowed references)
}
#[test]
fn conformance_join_awaits_task_completion() {
// INVARIANT: TaskHandle.join() waits for task completion and returns the result
use std::task::Context;
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (mut handle, mut stored) = scope.spawn(&mut state, &cx, |_| async { 123_i32 }).unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
// Before task completion, join should be pending
let mut join_fut = std::pin::pin!(handle.join(&cx));
assert!(
join_fut.as_mut().poll(&mut poll_cx).is_pending(),
"join must be pending before task completion"
);
// Complete the task
assert!(
stored.poll(&mut poll_cx).is_ready(),
"test task must complete in one poll"
);
// After task completion, join should return the result
match join_fut.as_mut().poll(&mut poll_cx) {
std::task::Poll::Ready(Ok(result)) => {
assert_eq!(result, 123, "join must return the task's result");
}
other => panic!("join must be Ready(Ok(123)) after task completion, got {other:?}"),
}
}
#[test]
fn conformance_child_region_task_isolation() {
// INVARIANT: Tasks spawned in child regions belong to the child, not the parent
use std::task::Context;
let mut state = RuntimeState::new();
let cx = test_cx();
let parent_region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(parent_region, Budget::INFINITE);
let outcome = block_on(scope.region(
&mut state,
&cx,
crate::types::policy::FailFast,
|child_scope, state| {
let child_region = child_scope.region_id();
// Spawn task in child region
let (handle, mut stored) = child_scope
.spawn(state, &cx, |_| async { 456_i32 })
.expect("spawn in child region must succeed");
// Verify task ownership invariants
let task_record = state
.task(handle.task_id())
.expect("child task must have a record");
let child_owns = task_record.owner == child_region;
let parent_owns = task_record.owner == parent_region;
// Verify region tracking invariants
let parent_tracks = state
.region(parent_region)
.is_some_and(|r| r.task_ids().contains(&handle.task_id()));
let child_tracks = state
.region(child_region)
.is_some_and(|r| r.task_ids().contains(&handle.task_id()));
// Complete the task for clean shutdown
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
if let std::task::Poll::Ready(outcome) = stored.poll(&mut poll_cx) {
if let Some(task) = state.task_mut(handle.task_id()) {
task.complete(outcome.map_err(|_| {
crate::error::Error::new(crate::error::ErrorKind::Internal)
}));
}
let _ = state.task_completed(handle.task_id());
}
std::future::ready(Outcome::Ok((
child_owns,
parent_owns,
child_tracks,
parent_tracks,
)))
},
))
.expect("child region must complete");
let (child_owns, parent_owns, child_tracks, parent_tracks) = match outcome {
Outcome::Ok(tuple) => tuple,
other => panic!("expected Ok(ownership_data), got {other:?}"),
};
assert!(
child_owns,
"task spawned in child region must be owned by child"
);
assert!(
!parent_owns,
"task spawned in child region must NOT be owned by parent"
);
assert!(child_tracks, "child region must track its spawned tasks");
assert!(!parent_tracks, "parent region must NOT track child's tasks");
}
#[test]
fn conformance_capability_inheritance() {
// INVARIANT: Spawned tasks inherit capabilities from the parent Cx
use crate::cx::macaroon::MacaroonToken;
use crate::cx::registry::RegistryHandle;
use crate::remote::{NodeId, RemoteCap};
use crate::security::key::AuthKey;
use crate::types::SystemPressure;
use std::sync::Arc;
use std::task::Context;
let mut state = RuntimeState::new();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Setup parent Cx with capabilities
let registry = crate::cx::NameRegistry::new();
let registry_handle = RegistryHandle::new(Arc::new(registry));
let parent_registry_arc = registry_handle.as_arc();
let parent_io_cap: Arc<dyn crate::io::IoCap> = Arc::new(crate::io::LabIoCap::new());
let parent_pressure = Arc::new(SystemPressure::new());
parent_pressure.set_headroom(0.25);
let auth_key = AuthKey::from_seed(7);
let token = MacaroonToken::mint(&auth_key, "scope:spawn", "cx/scope");
let parent_cx = Cx::new_with_io(
crate::types::RegionId::new_for_test(0, 0),
crate::types::TaskId::new_for_test(0, 0),
Budget::INFINITE,
None,
None,
Some(Arc::clone(&parent_io_cap)),
None,
)
.with_registry_handle(Some(registry_handle))
.with_remote_cap(RemoteCap::new().with_local_node(NodeId::new("test-node")))
.with_pressure(Arc::clone(&parent_pressure))
.with_macaroon(token);
let parent_macaroon = parent_cx
.macaroon_handle()
.expect("parent must retain macaroon capability");
let mut handle = scope
.spawn_registered(&mut state, &parent_cx, move |child_cx| async move {
// Verify registry inheritance
let child_registry = child_cx
.registry_handle()
.expect("child must inherit registry capability");
let same_registry = Arc::ptr_eq(&child_registry.as_arc(), &parent_registry_arc);
// Verify remote capability inheritance
let child_remote = child_cx
.remote()
.expect("child must inherit remote capability");
let node_name = child_remote.local_node().as_str().to_owned();
// Verify I/O capability inheritance
let child_io_cap = child_cx
.io_cap_handle()
.expect("child must inherit I/O capability");
let same_io_cap = Arc::ptr_eq(&child_io_cap, &parent_io_cap);
// Verify system pressure inheritance
let child_pressure = child_cx
.pressure_handle()
.expect("child must inherit system pressure");
let same_pressure = Arc::ptr_eq(&child_pressure, &parent_pressure);
// Verify macaroon inheritance
let child_macaroon = child_cx
.macaroon_handle()
.expect("child must inherit macaroon capability");
let same_macaroon = Arc::ptr_eq(&child_macaroon, &parent_macaroon);
// Verify timer capability inheritance (if parent has it)
let has_timer = child_cx.has_timer();
(
same_registry,
node_name,
same_io_cap,
same_pressure,
same_macaroon,
has_timer,
)
})
.unwrap();
// Complete the task and verify results
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
let stored = state
.get_stored_future(handle.task_id())
.expect("spawn_registered must store the task");
assert!(stored.poll(&mut poll_cx).is_ready());
let mut join_fut = std::pin::pin!(handle.join(&parent_cx));
match join_fut.as_mut().poll(&mut poll_cx) {
std::task::Poll::Ready(Ok((
same_registry,
node_name,
same_io_cap,
same_pressure,
same_macaroon,
has_timer,
))) => {
assert!(
same_registry,
"child must inherit exact same registry instance"
);
assert_eq!(
node_name, "test-node",
"child must inherit remote capability"
);
assert!(same_io_cap, "child must inherit exact same I/O capability");
assert!(
same_pressure,
"child must inherit exact same system pressure handle"
);
assert!(
same_macaroon,
"child must inherit exact same macaroon capability"
);
assert_eq!(
has_timer,
parent_cx.has_timer(),
"child timer capability should stay consistent with the runtime-backed parent"
);
}
other => panic!("capability inheritance test failed: {other:?}"),
}
}
#[test]
fn conformance_task_cancellation_propagation() {
// INVARIANT: Cancelling a task via abort() propagates cancellation signal
use std::task::Context;
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
let (mut handle, mut stored) = scope
.spawn(&mut state, &cx, |cx| async move {
// Check cancellation status and respond accordingly
if cx.checkpoint().is_err() {
"cancelled"
} else {
"completed"
}
})
.unwrap();
// Abort the task before it runs
handle.abort();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
// Task should complete and see the cancellation
assert!(
stored.poll(&mut poll_cx).is_ready(),
"cancelled task must still complete"
);
// Join should return the cancellation-aware result
let mut join_fut = std::pin::pin!(handle.join(&cx));
match join_fut.as_mut().poll(&mut poll_cx) {
std::task::Poll::Ready(Ok(result)) => {
assert_eq!(
result, "cancelled",
"cancelled task must observe cancellation via checkpoint()"
);
}
other => panic!("cancelled task join failed: {other:?}"),
}
}
#[test]
fn metamorphic_nested_scope_cancellation_closes_descendants_without_spawn_leaks() {
use std::task::{Context, Poll};
struct YieldOnce(bool);
impl std::future::Future for YieldOnce {
type Output = ();
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<()> {
if self.0 {
Poll::Ready(())
} else {
self.0 = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
let mut state = RuntimeState::new();
let cx = test_cx();
let root = state.create_root_region(Budget::INFINITE);
let child = state
.create_child_region(root, Budget::INFINITE)
.expect("child region");
let grandchild = state
.create_child_region(child, Budget::INFINITE)
.expect("grandchild region");
let child_scope = test_scope(child, Budget::INFINITE);
let grandchild_scope = test_scope(grandchild, Budget::INFINITE);
let finalizer_log = Arc::new(std::sync::Mutex::new(Vec::new()));
let child_log = Arc::clone(&finalizer_log);
assert!(
child_scope.defer_sync(&mut state, move || {
child_log
.lock()
.expect("child finalizer log poisoned")
.push("child");
}),
"child finalizer should register before cancellation"
);
let grandchild_log = Arc::clone(&finalizer_log);
assert!(
grandchild_scope.defer_sync(&mut state, move || {
grandchild_log
.lock()
.expect("grandchild finalizer log poisoned")
.push("grandchild");
}),
"grandchild finalizer should register before cancellation"
);
let mut child_handle = child_scope
.spawn_registered(&mut state, &cx, |task_cx| async move {
YieldOnce(false).await;
if task_cx.checkpoint().is_err() {
"child_cancelled"
} else {
"child_completed"
}
})
.expect("spawn child task");
let child_task_id = child_handle.task_id();
let mut grandchild_handle = grandchild_scope
.spawn_registered(&mut state, &cx, |task_cx| async move {
YieldOnce(false).await;
if task_cx.checkpoint().is_err() {
"grandchild_cancelled"
} else {
"grandchild_completed"
}
})
.expect("spawn grandchild task");
let grandchild_task_id = grandchild_handle.task_id();
let cancel_reason = CancelReason::shutdown().with_region(root);
let cancelled = state.cancel_request(root, &cancel_reason, None);
assert!(
cancelled
.iter()
.any(|(task_id, _)| *task_id == child_task_id),
"parent cancellation must reach child task"
);
assert!(
cancelled
.iter()
.any(|(task_id, _)| *task_id == grandchild_task_id),
"parent cancellation must reach grandchild task"
);
let grandchild_tasks_before_failed_spawn = state
.region(grandchild)
.expect("grandchild region missing")
.task_count();
let live_tasks_before_failed_spawn = state.live_task_count();
let failed_spawn = grandchild_scope.spawn(&mut state, &cx, |_| async { 99_u8 });
let grandchild_tasks_after_failed_spawn = state
.region(grandchild)
.expect("grandchild region missing after failed spawn")
.task_count();
let live_tasks_after_failed_spawn = state.live_task_count();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
{
let stored = state
.get_stored_future(grandchild_task_id)
.expect("grandchild stored task");
let poll_result = stored.poll(&mut poll_cx);
assert!(
poll_result.is_pending(),
"grandchild task should yield once before observing cancellation"
);
}
{
let stored = state
.get_stored_future(grandchild_task_id)
.expect("grandchild stored task");
let poll_result = stored.poll(&mut poll_cx);
let task_outcome = match poll_result {
Poll::Ready(Outcome::Ok(())) => Outcome::Ok(()),
Poll::Ready(Outcome::Panicked(payload)) => Outcome::Panicked(payload),
other => panic!(
"grandchild task should complete once cancellation is observed: {other:?}"
),
};
if let Some(task_record) = state.task_mut(grandchild_task_id) {
task_record.complete(task_outcome);
}
}
let _ = state.task_completed(grandchild_task_id);
state.advance_region_state(grandchild);
let mut grandchild_join_fut = std::pin::pin!(grandchild_handle.join(&cx));
let grandchild_result = match grandchild_join_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(Ok(result)) => result,
other => panic!("grandchild cancellation join should succeed: {other:?}"),
};
{
let stored = state
.get_stored_future(child_task_id)
.expect("child stored task");
let poll_result = stored.poll(&mut poll_cx);
assert!(
poll_result.is_pending(),
"child task should yield once before observing cancellation"
);
}
{
let stored = state
.get_stored_future(child_task_id)
.expect("child stored task");
let poll_result = stored.poll(&mut poll_cx);
let task_outcome = match poll_result {
Poll::Ready(Outcome::Ok(())) => Outcome::Ok(()),
Poll::Ready(Outcome::Panicked(payload)) => Outcome::Panicked(payload),
other => {
panic!("child task should complete once cancellation is observed: {other:?}")
}
};
if let Some(task_record) = state.task_mut(child_task_id) {
task_record.complete(task_outcome);
}
}
let _ = state.task_completed(child_task_id);
state.advance_region_state(child);
let mut child_join_fut = std::pin::pin!(child_handle.join(&cx));
let child_result = match child_join_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(Ok(result)) => result,
other => panic!("child cancellation join should succeed: {other:?}"),
};
assert_eq!(child_result, "child_cancelled");
assert_eq!(grandchild_result, "grandchild_cancelled");
assert!(
matches!(failed_spawn, Err(SpawnError::RegionClosed(id)) if id == grandchild),
"nested spawn after parent cancellation must fail against the closing grandchild region"
);
assert_eq!(
grandchild_tasks_before_failed_spawn, grandchild_tasks_after_failed_spawn,
"failed spawn after cancellation must not leak task membership into the grandchild region"
);
assert_eq!(
live_tasks_before_failed_spawn, live_tasks_after_failed_spawn,
"failed spawn after cancellation must not inflate runtime task count"
);
assert_eq!(
*finalizer_log.lock().expect("finalizer log poisoned"),
vec!["grandchild", "child"],
"nested scope finalizers must run in reverse scope creation order"
);
assert!(
state.region(grandchild).is_none(),
"grandchild region should be reclaimed after close"
);
assert!(
state.region(child).is_none(),
"child region should be reclaimed after close"
);
}
#[test]
fn conformance_race_loser_drain_invariant() {
// INVARIANT: In race operations, losers are cancelled and fully drained
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Winner: completes immediately
let (winner_handle, mut winner_stored) = scope
.spawn(&mut state, &cx, |_| async { "winner" })
.unwrap();
// Loser: would run longer, must be cancelled and drained
let (loser_handle, mut loser_stored) = scope
.spawn(&mut state, &cx, |cx| async move {
// Simulate work that can be cancelled
struct YieldOnce(bool);
impl std::future::Future for YieldOnce {
type Output = ();
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<()> {
if self.0 {
std::task::Poll::Ready(())
} else {
self.0 = true;
cx.waker().wake_by_ref();
std::task::Poll::Pending
}
}
}
YieldOnce(false).await;
// Check if we were cancelled
if cx.checkpoint().is_err() {
"loser_cancelled"
} else {
"loser_completed"
}
})
.unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
// Complete winner immediately
assert!(winner_stored.poll(&mut poll_cx).is_ready());
// Start the race
let handles = vec![winner_handle, loser_handle];
let mut race_fut = std::pin::pin!(scope.race_all(&cx, handles));
// Race should be pending initially (waiting for loser drain)
assert!(
race_fut.as_mut().poll(&mut poll_cx).is_pending(),
"race must wait for loser to be drained"
);
// Drive loser to first yield (it's now pending, but abort signal propagates)
assert!(loser_stored.poll(&mut poll_cx).is_pending());
// Still pending on race (loser not fully drained)
assert!(race_fut.as_mut().poll(&mut poll_cx).is_pending());
// Drive loser to completion (should see cancellation)
assert!(loser_stored.poll(&mut poll_cx).is_ready());
// Now race should complete with winner result
match race_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(Ok((result, winner_index))) => {
assert_eq!(result, "winner", "race must return winner result");
assert_eq!(winner_index, 0, "winner index must be correct");
}
other => panic!("race must complete after loser drain: {other:?}"),
}
}
#[test]
fn conformance_region_quiescence_on_empty() {
// INVARIANT: Empty regions reach quiescence and can be closed immediately
let mut state = RuntimeState::new();
let cx = test_cx();
let parent_region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(parent_region, Budget::INFINITE);
// Create and immediately close a child region with no spawned tasks
let outcome = block_on(scope.region(
&mut state,
&cx,
crate::types::policy::FailFast,
|_child_scope, _state| {
// Don't spawn any tasks - region should be empty
std::future::ready(Outcome::Ok("empty_region_completed"))
},
))
.expect("empty child region must complete");
match outcome {
Outcome::Ok(result) => {
assert_eq!(
result, "empty_region_completed",
"empty region must reach quiescence immediately"
);
}
other => panic!("empty region must complete successfully: {other:?}"),
}
// Child region should be cleaned up (no longer in parent's child list)
let parent_record = state
.region(parent_region)
.expect("parent region must exist");
assert!(
parent_record.child_ids().is_empty(),
"completed child region must be removed from parent"
);
}
#[test]
fn conformance_spawn_into_closed_region_fails() {
// INVARIANT: Cannot spawn tasks into regions that have begun closing
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Close the region
let region_record = state.region_mut(region).expect("region must exist");
region_record.begin_close(None);
// Attempt to spawn should fail
let spawn_result = scope.spawn(&mut state, &cx, |_| async { 42 });
assert!(
matches!(spawn_result, Err(SpawnError::RegionClosed(_))),
"spawning into closed region must fail with RegionClosed error"
);
// State should remain consistent (no orphaned tasks)
assert!(
state.tasks_is_empty()
|| state
.region(region)
.is_none_or(|r| r.task_ids().is_empty()),
"failed spawn must not create orphaned tasks"
);
}
#[test]
fn conformance_join_multiple_tasks_preserves_results() {
// INVARIANT: join_all preserves all task results in order
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Spawn multiple tasks with different results
let (h1, mut t1) = scope.spawn(&mut state, &cx, |_| async { 100_i32 }).unwrap();
let (h2, mut t2) = scope.spawn(&mut state, &cx, |_| async { 200_i32 }).unwrap();
let (h3, mut t3) = scope.spawn(&mut state, &cx, |_| async { 300_i32 }).unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
// Complete all tasks
assert!(t1.poll(&mut poll_cx).is_ready());
assert!(t2.poll(&mut poll_cx).is_ready());
assert!(t3.poll(&mut poll_cx).is_ready());
// Join all tasks
let handles = vec![h1, h2, h3];
let mut join_all_fut = std::pin::pin!(scope.join_all(&cx, handles));
match join_all_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(results) => {
assert_eq!(results.len(), 3, "join_all must return all results");
// Results must be in the same order as handles
assert_eq!(
results[0].as_ref().unwrap(),
&100,
"first task result must be preserved"
);
assert_eq!(
results[1].as_ref().unwrap(),
&200,
"second task result must be preserved"
);
assert_eq!(
results[2].as_ref().unwrap(),
&300,
"third task result must be preserved"
);
}
other @ Poll::Pending => panic!("join_all must complete with all results: {other:?}"),
}
}
#[test]
fn conformance_panic_propagation_through_join() {
// INVARIANT: Task panics are captured and propagated through join as JoinError::Panicked
use std::task::{Context, Poll};
let mut state = RuntimeState::new();
let cx = test_cx();
let region = state.create_root_region(Budget::INFINITE);
let scope = test_scope(region, Budget::INFINITE);
// Spawn a task that panics with a specific message
let (mut handle, mut stored) = scope
.spawn(&mut state, &cx, |_| async {
std::panic::panic_any("test_panic_message");
})
.unwrap();
let waker = std::task::Waker::noop().clone();
let mut poll_cx = Context::from_waker(&waker);
// Task execution should complete with Panicked outcome
match stored.poll(&mut poll_cx) {
Poll::Ready(crate::types::Outcome::Panicked(_)) => {
// Expected: panic was caught and wrapped as Outcome::Panicked
}
other => panic!("panicking task must complete with Panicked outcome: {other:?}"),
}
// Join should propagate the panic as JoinError::Panicked
let mut join_fut = std::pin::pin!(handle.join(&cx));
match join_fut.as_mut().poll(&mut poll_cx) {
Poll::Ready(Err(JoinError::Panicked(payload))) => {
assert_eq!(
payload.message(),
"test_panic_message",
"join must preserve panic payload message"
);
}
other => panic!("join of panicked task must return JoinError::Panicked: {other:?}"),
}
}
}