agntcy-slim-session 0.3.0

SLIM session internal implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0

// Standard library imports
use std::{collections::HashMap, time::Duration};

use display_error_chain::ErrorChainExt;
use parking_lot::Mutex;
use tokio::sync::{self, oneshot};
// Third-party crates
use tokio_util::sync::CancellationToken;
use tracing::{Instrument, debug};

use slim_auth::traits::{TokenProvider, Verifier};
use slim_datapath::{
    api::{
        CommandPayload, Content, NameId, ProtoMessage as Message, ProtoName,
        ProtoSessionMessageType, ProtoSessionType, SlimHeader,
    },
    messages::utils::SlimHeaderFlags,
};

// Local crate
use crate::{
    MessageDirection, SessionError,
    common::{OutboundMessage, SessionMessage, SessionOutput},
    completion_handle::CompletionHandle,
    controller_sender::{ControllerSender, PING_INTERVAL},
    session_builder::{ForController, SessionBuilder},
    session_config::SessionConfig,
    session_settings::SessionSettings,
    traits::{MessageHandler, ProcessingState},
};

pub(crate) async fn verify_identity<V>(msg: &Message, verifier: &V) -> Result<(), SessionError>
where
    V: Verifier + Send + Sync,
{
    let identity = msg.get_slim_header().get_identity();
    if verifier.try_verify(&identity).is_err() {
        verifier.verify(&identity).await?;
    }
    Ok(())
}

pub struct SessionController {
    /// session id
    pub(crate) id: u32,

    /// local name
    pub(crate) source: ProtoName,

    /// group or remote endpoint name
    pub(crate) destination: ProtoName,

    /// session config
    pub(crate) config: SessionConfig,

    /// channel to send messages to the processing loop
    tx_controller: sync::mpsc::Sender<SessionMessage>,

    /// use in drop implementation to gracefully close the processing loop
    pub(crate) cancellation_token: CancellationToken,

    /// handle for the processing loop
    handle: Mutex<Option<tokio::task::JoinHandle<()>>>,
}

impl SessionController {
    /// Returns a new SessionBuilder for constructing a SessionController
    pub fn builder<P, V>() -> SessionBuilder<P, V, ForController>
    where
        P: TokenProvider + Send + Sync + Clone + 'static,
        V: Verifier + Send + Sync + Clone + 'static,
    {
        SessionBuilder::for_controller()
    }

    /// Internal constructor for the builder to use
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn from_parts<I, P, V, M>(
        id: u32,
        source: ProtoName,
        destination: ProtoName,
        config: SessionConfig,
        settings: SessionSettings<P, V, M>,
        tx: sync::mpsc::Sender<SessionMessage>,
        rx: sync::mpsc::Receiver<SessionMessage>,
        inner: I,
    ) -> Self
    where
        I: MessageHandler + Send + Sync + 'static,
        P: slim_auth::traits::TokenProvider + Send + Sync + Clone + 'static,
        V: slim_auth::traits::Verifier + Send + Sync + Clone + 'static,
        M: crate::subscription_manager::SubscriptionOps,
    {
        // Spawn the processing loop
        let cancellation_token = CancellationToken::new();

        // setup tracing context
        let span = tracing::debug_span!(
            parent: None,
            "session_controller_processing_loop",
            session_id = id,
            service_id = %settings.service_id,
            source = %source,
            destination = %destination,
            session_type = ?config.session_type
        );

        let handle = crate::runtime::spawn(
            Self::processing_loop(inner, rx, cancellation_token.clone(), settings).instrument(span),
        );

        Self {
            id,
            source,
            destination,
            config,
            tx_controller: tx,
            cancellation_token,
            handle: Mutex::new(Some(handle)),
        }
    }

    /// Internal processing loop that handles messages with mutable access
    fn enter_draining_state<P, V, M>(
        shutdown_deadline: &mut std::pin::Pin<&mut tokio::time::Sleep>,
        settings: &SessionSettings<P, V, M>,
    ) where
        P: slim_auth::traits::TokenProvider + Send + Sync + Clone + 'static,
        V: slim_auth::traits::Verifier + Send + Sync + Clone + 'static,
        M: crate::subscription_manager::SubscriptionOps,
    {
        let shutdown_timeout = settings
            .graceful_shutdown_timeout
            .unwrap_or(Duration::from_secs(60));
        shutdown_deadline
            .as_mut()
            .reset(tokio::time::Instant::now() + shutdown_timeout);
    }

    /// Apply the identity token to all outbound ToSlim messages.
    /// Must run before MLS encryption so header-integrity AAD matches the on-wire header.
    pub(crate) fn apply_identity_to_slim_output<P>(
        output: &mut SessionOutput,
        identity_provider: &P,
    ) -> Result<(), SessionError>
    where
        P: slim_auth::traits::TokenProvider + Send + Sync + Clone + 'static,
    {
        let identity = identity_provider.get_token()?;
        for msg in &mut output.messages {
            if let OutboundMessage::ToSlim(m) = msg {
                m.get_slim_header_mut().set_identity(identity.clone());
            }
        }
        Ok(())
    }

    /// Dispatch outbound messages from SessionOutput to actual channels.
    /// Sends ToApp messages directly to the application channel.
    async fn dispatch_output<P, V, M>(output: SessionOutput, settings: &SessionSettings<P, V, M>)
    where
        P: slim_auth::traits::TokenProvider + Send + Sync + Clone + 'static,
        V: slim_auth::traits::Verifier + Send + Sync + Clone + 'static,
        M: crate::subscription_manager::SubscriptionOps,
    {
        for msg in output.messages {
            match msg {
                OutboundMessage::ToSlim(message) => {
                    if let Err(e) = settings.slim_tx.send(Ok(message)).await {
                        tracing::error!(error = %e, "failed to send message to SLIM");
                    }
                }
                OutboundMessage::ToApp(result) => {
                    if let Err(e) = settings.app_tx.send(result) {
                        tracing::error!(error = %e, "failed to send message to application");
                    }
                }
            }
        }
    }

    async fn processing_loop<P, V, M>(
        mut inner: impl MessageHandler + 'static,
        mut rx: sync::mpsc::Receiver<SessionMessage>,
        cancellation_token: CancellationToken,
        settings: SessionSettings<P, V, M>,
    ) where
        P: slim_auth::traits::TokenProvider + Send + Sync + Clone + 'static,
        V: slim_auth::traits::Verifier + Send + Sync + Clone + 'static,
        M: crate::subscription_manager::SubscriptionOps,
    {
        // Start with an infinite timeout (will be updated on graceful shutdown)
        let mut shutdown_deadline = std::pin::pin!(tokio::time::sleep(Duration::MAX));

        // Init the inner components
        if let Err(e) = inner.init().await {
            tracing::error!(error = %e.chain(), "error during initialization of session");
        }

        loop {
            tokio::select! {
                _ = cancellation_token.cancelled(), if inner.processing_state() == ProcessingState::Active => {
                    // Update the timeout to the configured grace period
                    let shutdown_timeout = settings.graceful_shutdown_timeout
                        .unwrap_or(Duration::from_secs(60)); // Default 60 seconds if not configured

                    // Finish any ongoing processing before starting drain
                    debug!("consuming pending messages before entering draining state");
                    while let Ok(msg) = rx.try_recv() {
                        if let SessionMessage::OnMessage { message, direction: MessageDirection::North, .. } = &msg
                            && let Err(e) = crate::session_controller::verify_identity(message, &settings.identity_verifier).await {
                            debug!(error = %e.chain(), "dropping inbound message during drain: identity verification failed");
                            continue;
                        }
                        match inner.on_message(msg).await {
                            Ok(output) => Self::dispatch_output(output, &settings).await,
                            Err(e) => {
                                tracing::error!(error = %e.chain(), "error processing message during draining - close immediately.");
                                break;
                            }
                        }
                    }

                    // Send drain to message to the inner to notify the beginning of the drain
                    match inner.on_message(SessionMessage::StartDrain {
                        grace_period: shutdown_timeout
                    }).await {
                        Ok(output) => Self::dispatch_output(output, &settings).await,
                        Err(e) => {
                            tracing::error!(error = %e.chain(),  "error during start drain");
                            break;
                        }
                    }

                    Self::enter_draining_state(&mut shutdown_deadline, &settings);

                    debug!("cancellation requested, entering draining state");
                }
                _ = &mut shutdown_deadline => {
                    debug!("graceful shutdown timeout reached, forcing exit");
                    break;
                }
                msg = rx.recv() => {
                    match msg {
                        Some(session_message) => {
                            // Handle GetParticipantsList query immediately without going through the handler
                            if let SessionMessage::GetParticipantsList { tx } = session_message {
                                let participants_list = inner.participants_list();
                                let _ = tx.send(participants_list);
                                continue;
                            }

                            if let SessionMessage::OnMessage { message, direction: MessageDirection::North, .. } = &session_message
                                && let Err(e) = crate::session_controller::verify_identity(message, &settings.identity_verifier).await {
                                debug!(
                                    error = %e.chain(),
                                    msg_type = %message.get_session_message_type().as_str_name(),
                                    msg_id = %message.get_id(),
                                    "dropping inbound message: identity verification failed",
                                );
                                continue;
                            }

                            let draining = inner.processing_state() == ProcessingState::Draining;

                            // if draining and message is sent by the application, reject it
                            if draining && matches!(session_message, SessionMessage::OnMessage { direction: MessageDirection::South, .. }) {
                                tracing::debug!("session is draining, rejecting new messages from application");
                                if let SessionMessage::OnMessage { ack_tx: Some(ack_tx), .. } = session_message {
                                    let _ = ack_tx.send(Err(SessionError::SessionDrainingDrop));
                                }
                                continue;
                            }

                            match inner.on_message(session_message).await {
                                Ok(output) => {
                                    Self::dispatch_output(output, &settings).await;
                                    // If we were active before processing and the handler switched to draining,
                                    // start (or reset) the graceful shutdown deadline just like on cancellation.
                                    if !draining && inner.processing_state() == ProcessingState::Draining {
                                        debug!("internal component requested draining, entering draining state");
                                        Self::enter_draining_state(&mut shutdown_deadline, &settings);
                                    }
                                }
                                Err(e) => {
                                    debug!(
                                        error=%e,
                                        "Error processing message{}",
                                        if draining { " during graceful shutdown" } else { "" }
                                    );
                                    if draining {
                                        debug!("Exiting processing loop due to error while draining");
                                        break;
                                    }
                                }
                            }
                        }
                        None => {
                            debug!("Session channel closed, no more messages can arrive - exiting processing loop");
                            break;
                        }
                    }
                }
            }

            // If we are in draining state and the inner component does not require drain, exit
            if inner.processing_state() == ProcessingState::Draining && !inner.needs_drain() {
                debug!("draining complete, exiting processing loop");
                break;
            }
        }

        // Perform final shutdown
        if let Err(e) = inner.on_shutdown().await {
            tracing::error!(error = %e.chain(), "error during shutdown of session");
        }
    }

    /// getters
    pub fn id(&self) -> u32 {
        self.id
    }

    pub fn source(&self) -> &ProtoName {
        &self.source
    }

    pub fn dst(&self) -> &ProtoName {
        &self.destination
    }

    pub fn session_type(&self) -> ProtoSessionType {
        self.config.session_type
    }

    pub fn metadata(&self) -> HashMap<String, String> {
        self.config.metadata.clone()
    }

    pub fn session_config(&self) -> SessionConfig {
        self.config.clone()
    }

    pub fn is_initiator(&self) -> bool {
        self.config.initiator
    }

    pub async fn participants_list(&self) -> Result<Vec<ProtoName>, SessionError> {
        let (tx, rx) = oneshot::channel();

        // Send query to the processing loop
        self.tx_controller
            .send(SessionMessage::GetParticipantsList { tx })
            .await
            .map_err(|_| SessionError::ParticipantsListQueryFailed)?;

        // Wait for response
        rx.await
            .map_err(|_| SessionError::ParticipantsListQueryFailed)
    }

    async fn on_message(
        &self,
        message: Message,
        direction: MessageDirection,
        ack_tx: Option<oneshot::Sender<Result<(), SessionError>>>,
    ) -> Result<(), SessionError> {
        self.tx_controller
            .send(SessionMessage::OnMessage {
                message,
                direction,
                ack_tx,
            })
            .await
            .map_err(|_e| SessionError::SessionControllerSendFailed)
    }

    /// Send a message to the controller for processing
    pub async fn on_message_from_app(
        &self,
        message: Message,
    ) -> Result<CompletionHandle, SessionError> {
        let (ack_tx, ack_rx) = oneshot::channel();
        self.on_message(message, MessageDirection::South, Some(ack_tx))
            .await?;

        let ret = CompletionHandle::from_oneshot_receiver(ack_rx);

        Ok(ret)
    }

    /// Send a message to the controller for processing
    pub async fn on_message_from_slim(&self, message: Message) -> Result<(), SessionError> {
        self.on_message(message, MessageDirection::North, None)
            .await
    }

    /// Send an error message to the controller for processing
    pub async fn on_error_message_from_slim(
        &self,
        error: SessionError,
    ) -> Result<(), SessionError> {
        self.tx_controller
            .send(SessionMessage::MessageError { error })
            .await
            .map_err(|_e| SessionError::SessionControllerSendFailed)
    }

    pub fn close(&self) -> Result<tokio::task::JoinHandle<()>, SessionError> {
        self.cancellation_token.cancel();

        self.handle
            .lock()
            .take()
            .ok_or(SessionError::SessionAlreadyClosed)
    }

    pub async fn publish_message(
        &self,
        message: Message,
    ) -> Result<CompletionHandle, SessionError> {
        self.on_message_from_app(message).await
    }

    /// Publish a message to a specific connection (forward_to)
    pub async fn publish_to(
        &self,
        name: &ProtoName,
        forward_to: u64,
        blob: Vec<u8>,
        payload_type: Option<String>,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<CompletionHandle, SessionError> {
        self.publish_with_flags(
            name,
            SlimHeaderFlags::default().with_forward_to(forward_to),
            blob,
            payload_type,
            metadata,
        )
        .await
    }

    /// Publish a message to a specific app name
    pub async fn publish(
        &self,
        name: &ProtoName,
        blob: Vec<u8>,
        payload_type: Option<String>,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<CompletionHandle, SessionError> {
        self.publish_with_flags(
            name,
            SlimHeaderFlags::default(),
            blob,
            payload_type,
            metadata,
        )
        .await
    }

    /// Publish a message with specific flags
    pub async fn publish_with_flags(
        &self,
        name: &ProtoName,
        flags: SlimHeaderFlags,
        blob: Vec<u8>,
        payload_type: Option<String>,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<CompletionHandle, SessionError> {
        let ct = payload_type.unwrap_or_else(|| "msg".to_string());

        let mut msg = Message::builder()
            .source(self.source().clone())
            .destination(name.clone())
            .identity("")
            .flags(flags)
            .session_type(self.session_type())
            .session_message_type(ProtoSessionMessageType::Msg)
            .session_id(self.id())
            .message_id(rand::random::<u32>()) // this will be changed by the session itself
            .application_payload(&ct, blob)
            .build_publish()?;
        if let Some(map) = metadata
            && !map.is_empty()
        {
            msg.set_metadata_map(map);
        }

        // southbound=true means towards slim
        self.publish_message(msg).await
    }

    /// Creates a discovery request message with minimum required information
    fn create_discovery_request(&self, destination: &ProtoName) -> Result<Message, SessionError> {
        let payload = CommandPayload::builder().discovery_request().as_content();

        let msg = Message::builder()
            .source(self.source().clone())
            .destination(destination.clone())
            .identity("")
            .session_type(self.session_type())
            .session_message_type(ProtoSessionMessageType::DiscoveryRequest)
            .session_id(self.id())
            .message_id(rand::random::<u32>())
            .payload(payload)
            .build_publish()?;

        Ok(msg)
    }

    pub(crate) async fn invite_participant_internal(
        &self,
        destination: &ProtoName,
    ) -> Result<CompletionHandle, SessionError> {
        let msg = self.create_discovery_request(destination)?;
        self.publish_message(msg).await
    }

    pub async fn invite_participant(
        &self,
        destination: &ProtoName,
    ) -> Result<CompletionHandle, SessionError> {
        match self.session_type() {
            ProtoSessionType::PointToPoint => Err(SessionError::CannotInviteToP2P),
            ProtoSessionType::Multicast => {
                if !self.is_initiator() {
                    return Err(SessionError::NotInitiator);
                }
                self.invite_participant_internal(destination).await
            }
            _ => Err(SessionError::SessionTypeUnknown(self.session_type())),
        }
    }

    pub async fn remove_participant(
        &self,
        destination: &ProtoName,
    ) -> Result<CompletionHandle, SessionError> {
        match self.session_type() {
            ProtoSessionType::PointToPoint => Err(SessionError::CannotRemoveFromP2P),
            ProtoSessionType::Multicast => {
                if !self.is_initiator() {
                    return Err(SessionError::NotInitiator);
                }
                let msg = Message::builder()
                    .source(self.source().clone())
                    .destination(destination.clone().with_id(NameId::NULL_COMPONENT))
                    .identity("")
                    .session_type(ProtoSessionType::Multicast)
                    .session_message_type(ProtoSessionMessageType::LeaveRequest)
                    .session_id(self.id())
                    .message_id(rand::random::<u32>())
                    .payload(CommandPayload::builder().leave_request().as_content())
                    .build_publish()?;
                self.publish_message(msg).await
            }
            _ => Err(SessionError::SessionTypeUnknown(self.session_type())),
        }
    }
}

impl Drop for SessionController {
    fn drop(&mut self) {
        self.cancellation_token.cancel();
    }
}

pub fn handle_channel_discovery_message(
    message: &Message,
    app_name: &ProtoName,
    session_id: u32,
    session_type: ProtoSessionType,
) -> Result<Message, SessionError> {
    let destination = message.get_slim_header().source.clone().unwrap();

    // the destination of the discovery message may be different from the name of
    // application itself. This can happen if the application subscribes to multiple
    // service names. So we can reply using as a source the destination name of
    // the discovery message but setting the application id
    let mut source = message.get_slim_header().destination.clone().unwrap();
    source.set_id(app_name.id());
    let msg_id = message.get_id();

    let slim_header = SlimHeader::new(
        source,
        destination,
        "",
        Some(SlimHeaderFlags::default().with_forward_to(message.get_incoming_conn())),
    );

    let msg = Message::builder()
        .with_slim_header(slim_header)
        .session_type(session_type)
        .session_message_type(ProtoSessionMessageType::DiscoveryReply)
        .session_id(session_id)
        .message_id(msg_id)
        .payload(CommandPayload::builder().discovery_reply().as_content())
        .build_publish()?;

    Ok(msg)
}

pub(crate) struct SessionControllerCommon<
    P,
    V,
    M = crate::subscription_manager::SubscriptionManager,
> where
    P: TokenProvider + Send + Sync + Clone + 'static,
    V: Verifier + Send + Sync + Clone + 'static,
    M: crate::subscription_manager::SubscriptionOps,
{
    /// common session fields
    pub(crate) settings: SessionSettings<P, V, M>,

    /// sender for command messages
    pub(crate) sender: ControllerSender,

    /// processing state
    pub(crate) processing_state: ProcessingState,

    /// Maps (kind, name, conn) → subscription_id for route/subscription tracking.
    subscription_ids: HashMap<(SubscriptionKind, ProtoName, u64), u64>,
}

/// Distinguishes route entries from subscription entries in the subscription_ids map.
/// Both can share the same `(Name, conn)` pair, so this enum prevents key collisions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum SubscriptionKind {
    /// A recv_from route (`set_route` / `remove_route`).
    Route,
    /// A forward_to subscription (`subscribe` / `unsubscribe`).
    Subscription,
}

impl<P, V, M> SessionControllerCommon<P, V, M>
where
    P: TokenProvider + Send + Sync + Clone + 'static,
    V: Verifier + Send + Sync + Clone + 'static,
    M: crate::subscription_manager::SubscriptionOps,
{
    pub(crate) fn new(settings: SessionSettings<P, V, M>) -> Self {
        // Create the controller sender.
        let controller_sender = ControllerSender::new(
            settings.config.get_timer_settings(),
            settings.source.clone(),
            settings.config.session_type,
            settings.id,
            Some(PING_INTERVAL),
            settings.config.initiator,
            settings.tx_session.clone(),
        );

        SessionControllerCommon {
            settings,
            sender: controller_sender,
            processing_state: ProcessingState::Active,
            subscription_ids: HashMap::new(),
        }
    }

    /// Send control message through ControllerSender, returning the output.
    pub(crate) fn send_with_timer(
        &mut self,
        message: Message,
    ) -> Result<SessionOutput, SessionError> {
        self.sender.on_message(&message)
    }

    async fn await_subscription_ack(
        rx: tokio::sync::oneshot::Receiver<
            Result<(), crate::subscription_manager::SubscriptionAckError>,
        >,
    ) -> Result<(), SessionError> {
        crate::subscription_manager::SubscriptionManager::await_ack(rx)
            .await
            .map_err(SessionError::SubscriptionAckFailed)
    }

    pub(crate) async fn add_route(
        &mut self,
        name: ProtoName,
        conn: u64,
    ) -> Result<(), SessionError> {
        if name == self.settings.source.clone() {
            // We never add a route for ourselves
            return Ok(());
        }

        let source_proto = self.settings.source.clone();
        let (subscription_id, rx) = self
            .settings
            .subscription_manager
            .set_route(&source_proto, &name, conn)
            .await
            .map_err(SessionError::SubscriptionAckFailed)?;
        Self::await_subscription_ack(rx).await?;

        debug!(%name, %conn, %subscription_id, source = %self.settings.source, "route added");

        self.subscription_ids
            .insert((SubscriptionKind::Route, name, conn), subscription_id);

        Ok(())
    }

    pub(crate) async fn delete_route(
        &mut self,
        name: ProtoName,
        conn: u64,
    ) -> Result<(), SessionError> {
        if name == self.settings.source.clone() {
            // We never remove a route for ourselves
            return Ok(());
        }

        let key = (SubscriptionKind::Route, name, conn);
        let subscription_id = self.subscription_ids.remove(&key);
        let (_, name, conn) = key;
        match subscription_id {
            Some(subscription_id) => {
                let source_proto = self.settings.source.clone();
                let rx = self
                    .settings
                    .subscription_manager
                    .remove_route(&source_proto, &name, subscription_id, conn)
                    .await
                    .map_err(SessionError::SubscriptionAckFailed)?;

                Self::await_subscription_ack(rx).await?;
                tracing::debug!(%name, %conn, %subscription_id, "route deleted");
            }
            None => {
                tracing::warn!(
                    %name, %conn, io = %self.settings.source,
                    "no subscription_id found for route, skipping delete"
                );
            }
        }

        Ok(())
    }

    pub(crate) async fn add_subscription(
        &mut self,
        name: ProtoName,
        conn: u64,
    ) -> Result<(), SessionError> {
        let source_proto = self.settings.source.clone();
        let (subscription_id, rx) = self
            .settings
            .subscription_manager
            .subscribe(&source_proto, &name, Some(conn))
            .await
            .map_err(SessionError::SubscriptionAckFailed)?;

        Self::await_subscription_ack(rx).await?;

        debug!(%name, %conn, %subscription_id, "subscription added");

        self.subscription_ids.insert(
            (SubscriptionKind::Subscription, name, conn),
            subscription_id,
        );

        Ok(())
    }

    pub(crate) async fn delete_subscription(
        &mut self,
        name: ProtoName,
        conn: u64,
    ) -> Result<(), SessionError> {
        let key = (SubscriptionKind::Subscription, name, conn);
        let subscription_id = self.subscription_ids.remove(&key);
        let (_, name, conn) = key;
        match subscription_id {
            Some(subscription_id) => {
                let source_proto = self.settings.source.clone();
                let rx = self
                    .settings
                    .subscription_manager
                    .unsubscribe(&source_proto, &name, subscription_id, Some(conn))
                    .await
                    .map_err(SessionError::SubscriptionAckFailed)?;

                Self::await_subscription_ack(rx).await?;
                debug!(%name, %conn, %subscription_id, "subscription deleted");
            }
            None => {
                tracing::debug!(
                    %name, %conn,
                    "no subscription_id found for subscription, skipping delete"
                );
            }
        }

        Ok(())
    }

    pub(crate) fn create_control_message(
        &mut self,
        dst: &ProtoName,
        message_type: ProtoSessionMessageType,
        message_id: u32,
        payload: Content,
        broadcast: bool,
    ) -> Result<Message, SessionError> {
        let mut builder = Message::builder()
            .source(self.settings.source.clone())
            .destination(dst.clone())
            .identity("")
            .session_type(self.settings.config.session_type)
            .session_message_type(message_type)
            .session_id(self.settings.id)
            .message_id(message_id)
            .payload(payload);

        if broadcast {
            builder = builder.fanout(256);
        }

        let ret = builder.build_publish()?;

        Ok(ret)
    }

    /// Send control message without creating ack channel (for internal use by moderator)
    pub(crate) fn send_control_message(
        &mut self,
        dst: &ProtoName,
        message_type: ProtoSessionMessageType,
        message_id: u32,
        payload: Content,
        metadata: Option<HashMap<String, String>>,
        broadcast: bool,
    ) -> Result<SessionOutput, SessionError> {
        let mut msg =
            self.create_control_message(dst, message_type, message_id, payload, broadcast)?;
        if let Some(m) = metadata {
            msg.set_metadata_map(m);
        }
        self.send_with_timer(msg)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Test: internal draining transition triggered by a leave request.
    // This test sends a LeaveRequest into a multicast participant session and then
    // verifies (indirectly) that subsequent messages are still accepted while the
    // session is transitioning, indicating that graceful draining has begun.
    // Removed broken test_internal_draining_via_leave_request (incompatible mock trait implementation)

    use crate::Direction;
    use crate::session_config::MlsSettings;
    use crate::subscription_manager::{SpySubscriptionManager, SubscriptionCall};
    use slim_auth::shared_secret::SharedSecret;

    use std::sync::Arc;
    use std::sync::atomic::AtomicBool;
    use std::time::Duration;
    use tokio::time::timeout;
    use tracing_test::traced_test;

    const SHARED_SECRET: &str = "kjandjansdiasb8udaijdniasdaindasndasndasndasndasndasndasndas";

    fn test_identity() -> String {
        SharedSecret::new("test", SHARED_SECRET)
            .unwrap()
            .get_token()
            .unwrap()
    }

    /// Test helper to create a SessionController with common setup
    struct SessionControllerTestBuilder {
        session_id: u32,
        source: ProtoName,
        destination: ProtoName,
        session_type: ProtoSessionType,
        mls_settings: Option<MlsSettings>,
        initiator: bool,
        max_retries: Option<u32>,
        interval: Option<Duration>,
        metadata: HashMap<String, String>,
        graceful_shutdown_timeout: Option<Duration>,
    }

    impl SessionControllerTestBuilder {
        #[allow(dead_code)]
        fn new() -> Self {
            Self {
                session_id: 10,
                source: ProtoName::from_strings(["org", "ns", "source"]).with_id(1),
                destination: ProtoName::from_strings(["org", "ns", "dest"]).with_id(2),
                session_type: ProtoSessionType::PointToPoint,
                mls_settings: None,
                initiator: true,
                max_retries: Some(5),
                interval: Some(Duration::from_millis(200)),
                metadata: HashMap::new(),
                graceful_shutdown_timeout: Some(Duration::from_secs(10)),
            }
        }

        fn with_session_id(mut self, id: u32) -> Self {
            self.session_id = id;
            self
        }

        #[allow(dead_code)]
        fn with_source(mut self, source: ProtoName) -> Self {
            self.source = source;
            self
        }

        #[allow(dead_code)]
        fn with_destination(mut self, destination: ProtoName) -> Self {
            self.destination = destination;
            self
        }

        fn with_session_type(mut self, session_type: ProtoSessionType) -> Self {
            self.session_type = session_type;
            self
        }

        fn with_mls_enabled(mut self, enabled: bool) -> Self {
            self.mls_settings = if enabled {
                Some(MlsSettings::default())
            } else {
                None
            };
            self
        }

        fn with_initiator(mut self, initiator: bool) -> Self {
            self.initiator = initiator;
            self
        }

        fn with_metadata(mut self, metadata: HashMap<String, String>) -> Self {
            self.metadata = metadata;
            self
        }

        fn with_graceful_shutdown_timeout(mut self, timeout: Duration) -> Self {
            self.graceful_shutdown_timeout = Some(timeout);
            self
        }

        fn build(
            self,
        ) -> (
            SessionController,
            tokio::sync::mpsc::Receiver<Result<Message, slim_datapath::Status>>,
            tokio::sync::mpsc::UnboundedReceiver<Result<Message, SessionError>>,
        ) {
            let config = SessionConfig {
                session_type: self.session_type,
                max_retries: self.max_retries,
                interval: self.interval,
                mls_settings: self.mls_settings,
                initiator: self.initiator,
                metadata: self.metadata,
            };

            let (tx_slim, rx_slim) = tokio::sync::mpsc::channel(10);
            let (tx_app, rx_app) = tokio::sync::mpsc::unbounded_channel();
            let (tx_session_layer, _rx_session_layer) = tokio::sync::mpsc::channel(10);

            let controller = SessionController::builder()
                .with_id(self.session_id)
                .with_source(self.source.clone())
                .with_destination(self.destination.clone())
                .with_config(config)
                .with_identity_provider(SharedSecret::new("test", SHARED_SECRET).unwrap())
                .with_identity_verifier(SharedSecret::new("test", SHARED_SECRET).unwrap())
                .with_slim_tx(tx_slim)
                .with_app_tx(tx_app)
                .with_tx_to_session_layer(tx_session_layer)
                .ready()
                .expect("failed to validate builder")
                .build()
                .expect("failed to build controller");

            (controller, rx_slim, rx_app)
        }
    }

    #[tokio::test]
    async fn test_session_controller_getters() {
        let mut metadata = HashMap::new();
        metadata.insert("key1".to_string(), "value1".to_string());

        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_id(42)
            .with_session_type(ProtoSessionType::Multicast)
            .with_mls_enabled(true)
            .with_metadata(metadata)
            .build();

        assert_eq!(controller.id(), 42);
        assert_eq!(
            controller.source(),
            &ProtoName::from_strings(["org", "ns", "source"]).with_id(1)
        );
        // For multicast sessions, destination uses DATA_CHANNEL_ID
        assert_eq!(
            controller.dst(),
            &ProtoName::from_strings(["org", "ns", "dest"]).with_id(NameId::DATA_CHANNEL_ID)
        );
        assert_eq!(controller.session_type(), ProtoSessionType::Multicast);
        assert!(controller.is_initiator());
        assert_eq!(
            controller.metadata().get("key1"),
            Some(&"value1".to_string())
        );

        let retrieved_config = controller.session_config();
        assert_eq!(retrieved_config.session_type, ProtoSessionType::Multicast);
        assert_eq!(retrieved_config.max_retries, Some(5));
    }

    #[tokio::test]
    async fn test_publish_basic() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new().build();

        let target_name = ProtoName::from_strings(["org", "ns", "target"]);
        let payload = b"Hello World".to_vec();

        controller
            .publish(
                &target_name,
                payload.clone(),
                Some("test-type".to_string()),
                None,
            )
            .await
            .expect("publish should succeed");

        tokio::time::sleep(Duration::from_millis(50)).await;
    }

    #[tokio::test]
    async fn test_publish_to_specific_connection() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::Multicast)
            .build();

        let target_name = ProtoName::from_strings(["org", "ns", "target"]);
        let payload = b"Hello to specific connection".to_vec();
        let connection_id = 123u64;

        controller
            .publish_to(
                &target_name,
                connection_id,
                payload.clone(),
                Some("test-type".to_string()),
                None,
            )
            .await
            .expect("publish_to should succeed");

        tokio::time::sleep(Duration::from_millis(50)).await;
    }

    #[tokio::test]
    async fn test_publish_with_metadata() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::Multicast)
            .build();

        let target_name = ProtoName::from_strings(["org", "ns", "target"]);
        let payload = b"Hello with metadata".to_vec();

        let mut metadata = HashMap::new();
        metadata.insert("custom_key".to_string(), "custom_value".to_string());

        controller
            .publish(
                &target_name,
                payload.clone(),
                Some("test-type".to_string()),
                Some(metadata),
            )
            .await
            .expect("publish with metadata should succeed");

        tokio::time::sleep(Duration::from_millis(50)).await;
    }

    #[tokio::test]
    async fn test_invite_participant_in_multicast() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::Multicast)
            .build();

        let participant = ProtoName::from_strings(["org", "ns", "participant"]);

        controller
            .invite_participant(&participant)
            .await
            .expect("invite should succeed");

        tokio::time::sleep(Duration::from_millis(50)).await;
    }

    #[tokio::test]
    async fn test_invite_participant_not_initiator_error() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::Multicast)
            .with_initiator(false)
            .build();

        let participant = ProtoName::from_strings(["org", "ns", "new_participant"]);

        let result = controller.invite_participant(&participant).await;
        assert!(result.is_err_and(|e| matches!(e, SessionError::NotInitiator)));
    }

    #[tokio::test]
    async fn test_invite_participant_p2p_error() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::PointToPoint)
            .build();

        let participant = ProtoName::from_strings(["org", "ns", "participant"]);

        let result = controller.invite_participant(&participant).await;
        assert!(result.is_err_and(|e| matches!(e, SessionError::CannotInviteToP2P)));
    }

    #[tokio::test]
    async fn test_remove_participant_in_multicast() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::Multicast)
            .build();

        let participant = ProtoName::from_strings(["org", "ns", "participant"]);

        controller
            .remove_participant(&participant)
            .await
            .expect("remove should succeed");

        tokio::time::sleep(Duration::from_millis(50)).await;
    }

    #[tokio::test]
    async fn test_remove_participant_not_initiator_error() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::Multicast)
            .with_initiator(false)
            .build();

        let participant = ProtoName::from_strings(["org", "ns", "participant"]);

        let result = controller.remove_participant(&participant).await;
        assert!(result.is_err_and(|e| matches!(e, SessionError::NotInitiator)));
    }

    #[tokio::test]
    async fn test_remove_participant_p2p_error() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::PointToPoint)
            .build();

        let participant = ProtoName::from_strings(["org", "ns", "participant"]);

        let result = controller.remove_participant(&participant).await;
        assert!(result.is_err_and(|e| matches!(e, SessionError::CannotRemoveFromP2P)));
    }

    #[test]
    fn test_handle_channel_discovery_message() {
        let app_name = ProtoName::from_strings(["org", "ns", "app"]).with_id(100);
        let session_id = 42;

        let discovery_request = Message::builder()
            .source(ProtoName::from_strings(["org", "ns", "requester"]).with_id(1))
            .destination(ProtoName::from_strings(["org", "ns", "service"]))
            .identity(test_identity())
            .incoming_conn(999)
            .session_type(ProtoSessionType::Multicast)
            .session_message_type(ProtoSessionMessageType::DiscoveryRequest)
            .session_id(session_id)
            .message_id(123)
            .payload(CommandPayload::builder().discovery_request().as_content())
            .build_publish()
            .unwrap();

        let response = handle_channel_discovery_message(
            &discovery_request,
            &app_name,
            session_id,
            ProtoSessionType::Multicast,
        )
        .expect("should create discovery response");

        assert_eq!(
            response.get_session_message_type(),
            ProtoSessionMessageType::DiscoveryReply
        );
        assert_eq!(response.get_session_header().get_session_id(), session_id);
        assert_eq!(response.get_id(), 123);
        assert_eq!(
            response.get_dst(),
            ProtoName::from_strings(["org", "ns", "requester"]).with_id(1)
        );
        assert_eq!(response.get_slim_header().get_forward_to(), Some(999));
    }

    #[tokio::test]
    async fn test_controller_drop_cancels_processing() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new().build();

        let token = controller.cancellation_token.clone();
        assert!(!token.is_cancelled());

        drop(controller);

        tokio::time::sleep(Duration::from_millis(100)).await;
        assert!(token.is_cancelled());
    }

    #[tokio::test]
    async fn test_close_success() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_graceful_shutdown_timeout(std::time::Duration::from_secs(2))
            .build();

        let token = controller.cancellation_token.clone();
        assert!(!token.is_cancelled());

        let handle = controller.close();
        assert!(handle.is_ok(), "got error {}", handle.unwrap_err());
        assert!(token.is_cancelled());

        // Wait for the handle to complete
        handle
            .unwrap()
            .await
            .expect("processing task should complete");
    }

    #[tokio::test]
    async fn test_close_already_closed() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new().build();

        // Close once - should succeed
        let handle = controller.close();
        assert!(handle.is_ok());
        handle
            .unwrap()
            .await
            .expect("processing task should complete");

        // Close again - should fail with appropriate error
        let result = controller.close();
        assert!(result.is_err());
        match result {
            Err(SessionError::SessionAlreadyClosed) => {
                // expected
            }
            _ => panic!("Expected SessionError::SessionAlreadyClosed"),
        }
    }

    #[tokio::test]
    async fn test_close_cancels_token_immediately() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new().build();

        let token = controller.cancellation_token.clone();

        // Verify token is not cancelled before close
        assert!(!token.is_cancelled());

        // Close returns immediately after cancelling token
        let handle = controller.close();
        assert!(handle.is_ok());

        // Token should be cancelled immediately
        assert!(token.is_cancelled());

        // Wait for processing to complete
        handle.unwrap().await.expect("processing should complete");
    }

    #[tokio::test]
    async fn test_on_message_direction_north() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new().build();

        let test_message = Message::builder()
            .source(controller.dst().clone())
            .destination(controller.source().clone())
            .identity(test_identity())
            .session_type(ProtoSessionType::PointToPoint)
            .session_message_type(ProtoSessionMessageType::Msg)
            .session_id(controller.id())
            .message_id(1)
            .application_payload("test", b"test data".to_vec())
            .build_publish()
            .unwrap();

        let result = controller.on_message_from_slim(test_message).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_create_discovery_request() {
        let (controller, _rx_slim, _rx_app) = SessionControllerTestBuilder::new()
            .with_session_type(ProtoSessionType::Multicast)
            .build();

        let target = ProtoName::from_strings(["org", "ns", "target"]);
        let discovery_msg = controller
            .create_discovery_request(&target)
            .expect("should create discovery request");

        assert_eq!(discovery_msg.get_source(), *controller.source());
        assert_eq!(discovery_msg.get_dst(), target);
        assert_eq!(
            discovery_msg.get_session_message_type(),
            ProtoSessionMessageType::DiscoveryRequest
        );
        assert_eq!(
            discovery_msg.get_session_header().get_session_id(),
            controller.id()
        );
        assert_eq!(
            discovery_msg.get_session_type(),
            ProtoSessionType::Multicast
        );
    }

    #[tokio::test]
    #[traced_test]
    async fn test_end_to_end_p2p() {
        let session_id = 10;
        let moderator_name = ProtoName::from_strings(["org", "ns", "moderator"]).with_id(1);
        let participant_name = ProtoName::from_strings(["org", "ns", "participant"]);
        let participant_name_id = ProtoName::from_strings(["org", "ns", "participant"]).with_id(1);
        // create a SessionModerator
        let (tx_slim_moderator, mut rx_slim_moderator) = tokio::sync::mpsc::channel(10);
        let (tx_app_moderator, _rx_app_moderator) = tokio::sync::mpsc::unbounded_channel();
        let (tx_session_layer_moderator, _rx_session_layer_moderator) =
            tokio::sync::mpsc::channel(10);

        let moderator_config = SessionConfig {
            session_type: slim_datapath::api::ProtoSessionType::PointToPoint,
            max_retries: Some(5),
            interval: Some(Duration::from_millis(1000)),
            mls_settings: Some(MlsSettings::default()),
            initiator: true,
            metadata: std::collections::HashMap::new(),
        };

        let (spy_moderator_mgr, mut rx_spy_moderator) = SpySubscriptionManager::new();
        let moderator = SessionController::builder()
            .with_id(session_id)
            .with_source(moderator_name.clone())
            .with_destination(participant_name.clone())
            .with_config(moderator_config)
            .with_identity_provider(SharedSecret::new("moderator", SHARED_SECRET).unwrap())
            .with_identity_verifier(SharedSecret::new("moderator", SHARED_SECRET).unwrap())
            .with_slim_tx(tx_slim_moderator.clone())
            .with_app_tx(tx_app_moderator.clone())
            .with_tx_to_session_layer(tx_session_layer_moderator)
            .with_subscription_manager(spy_moderator_mgr)
            .ready()
            .expect("failed to validate builder")
            .build()
            .unwrap();

        // create a SessionParticipant
        let (tx_slim_participant, mut rx_slim_participant) = tokio::sync::mpsc::channel(10);
        let (tx_app_participant, mut rx_app_participant) = tokio::sync::mpsc::unbounded_channel();
        let (tx_session_layer_participant, _rx_session_layer_participant) =
            tokio::sync::mpsc::channel(10);

        let participant_config = SessionConfig {
            session_type: slim_datapath::api::ProtoSessionType::PointToPoint,
            max_retries: Some(5),
            interval: Some(Duration::from_millis(200)),
            mls_settings: Some(MlsSettings::default()),
            initiator: false,
            metadata: std::collections::HashMap::new(),
        };

        let (spy_participant_mgr, mut rx_spy_participant) = SpySubscriptionManager::new();
        let participant = SessionController::builder()
            .with_id(session_id)
            .with_source(participant_name_id.clone())
            .with_destination(moderator_name.clone())
            .with_config(participant_config)
            .with_identity_provider(SharedSecret::new("participant", SHARED_SECRET).unwrap())
            .with_identity_verifier(SharedSecret::new("participant", SHARED_SECRET).unwrap())
            .with_slim_tx(tx_slim_participant.clone())
            .with_app_tx(tx_app_participant.clone())
            .with_tx_to_session_layer(tx_session_layer_participant)
            .with_subscription_manager(spy_participant_mgr)
            .ready()
            .expect("failed to validate builder")
            .build()
            .unwrap();

        let completion_handle = moderator
            .invite_participant_internal(&participant_name)
            .await
            .expect("error inviting participant");

        let received_discovery_request =
            timeout(Duration::from_millis(100), rx_slim_moderator.recv())
                .await
                .expect("timeout waiting for discovery request on moderator slim channel")
                .expect("channel closed")
                .expect("error in discovery request");

        assert_eq!(
            received_discovery_request.get_session_message_type(),
            slim_datapath::api::ProtoSessionMessageType::DiscoveryRequest
        );

        let discovery_msg_id = received_discovery_request.get_id();

        // create a discovery reply and call the on message on the moderator with the reply (direction north)
        let mut discovery_reply = Message::builder()
            .source(participant_name_id.clone())
            .destination(moderator_name.clone())
            .identity(test_identity())
            .forward_to(1)
            .session_type(slim_datapath::api::ProtoSessionType::PointToPoint)
            .session_message_type(slim_datapath::api::ProtoSessionMessageType::DiscoveryReply)
            .session_id(session_id)
            .message_id(discovery_msg_id)
            .payload(CommandPayload::builder().discovery_reply().as_content())
            .build_publish()
            .unwrap();
        discovery_reply
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        moderator
            .on_message_from_slim(discovery_reply)
            .await
            .expect("error processing discovery reply on moderator");

        // moderator sets route for participant after discovery reply
        assert_eq!(
            rx_spy_moderator.recv().await,
            Some(SubscriptionCall::SetRoute),
            "moderator should set route after discovery reply"
        );

        // check that a join request is received by slim
        let join_request = timeout(Duration::from_millis(100), rx_slim_moderator.recv())
            .await
            .expect("timeout waiting for join request on moderator slim channel")
            .expect("channel closed")
            .expect("error in join request");

        assert_eq!(
            join_request.get_session_message_type(),
            slim_datapath::api::ProtoSessionMessageType::JoinRequest
        );
        assert_eq!(join_request.get_dst(), participant_name_id);

        // call the on message on the participant side with the join request (direction north)
        let mut join_request_to_participant = join_request.clone();
        join_request_to_participant
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        participant
            .on_message_from_slim(join_request_to_participant)
            .await
            .expect("error processing join request on participant");

        // participant sets route for moderator after join request
        assert_eq!(
            rx_spy_participant.recv().await,
            Some(SubscriptionCall::SetRoute),
            "participant should set route after join request"
        );

        // check that a join reply is received by slim on the participant
        let join_reply = timeout(Duration::from_millis(100), rx_slim_participant.recv())
            .await
            .expect("timeout waiting for join reply on participant slim channel")
            .expect("channel closed")
            .expect("error in join reply");

        assert_eq!(
            join_reply.get_session_message_type(),
            slim_datapath::api::ProtoSessionMessageType::JoinReply
        );
        assert_eq!(join_reply.get_dst(), moderator_name);

        // call the on message on the moderator with the reply (direction north)
        let mut join_reply_to_moderator = join_reply.clone();
        join_reply_to_moderator
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        moderator
            .on_message_from_slim(join_reply_to_moderator)
            .await
            .expect("error processing join reply on moderator");

        // check that a welcome message is received by slim on the moderator
        let welcome_message = timeout(Duration::from_millis(100), rx_slim_moderator.recv())
            .await
            .expect("timeout waiting for welcome message on moderator slim channel")
            .expect("channel closed")
            .expect("error in welcome message");

        assert_eq!(
            welcome_message.get_session_message_type(),
            slim_datapath::api::ProtoSessionMessageType::GroupWelcome
        );
        assert_eq!(welcome_message.get_dst(), participant_name_id);

        // call the on message on the participant side with the welcome message (direction north)
        let mut welcome_to_participant = welcome_message.clone();
        welcome_to_participant
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        participant
            .on_message_from_slim(welcome_to_participant)
            .await
            .expect("error processing welcome message on participant");

        // check that an ack group is received by slim on the participant
        let ack_group = timeout(Duration::from_millis(100), rx_slim_participant.recv())
            .await
            .expect("timeout waiting for ack group on participant slim channel")
            .expect("channel closed")
            .expect("error in ack group");

        assert_eq!(
            ack_group.get_session_message_type(),
            slim_datapath::api::ProtoSessionMessageType::GroupAck
        );
        assert_eq!(ack_group.get_dst(), moderator_name);

        // call the on message on the moderator with the ack (direction north)
        let mut ack_to_moderator = ack_group.clone();
        ack_to_moderator
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        moderator
            .on_message_from_slim(ack_to_moderator)
            .await
            .expect("error processing ack group on moderator");

        // no other message should be sent
        let no_more_moderator = timeout(Duration::from_millis(100), rx_slim_moderator.recv()).await;
        assert!(
            no_more_moderator.is_err(),
            "Expected no more messages on moderator slim channel, received {:?}",
            no_more_moderator
                .ok()
                .and_then(|opt| opt)
                .and_then(|res| res.ok())
        );

        let no_more_participant =
            timeout(Duration::from_millis(100), rx_slim_participant.recv()).await;
        assert!(
            no_more_participant.is_err(),
            "Expected no more messages on participant slim channel"
        );

        // the completion handler should now be complete
        completion_handle.await.expect("error in completion handle");

        // create an application message using the participant name
        let app_data = b"Hello from moderator to participant".to_vec();
        let app_message = Message::builder()
            .source(moderator_name.clone())
            .destination(participant_name.clone())
            .identity(test_identity())
            .session_type(slim_datapath::api::ProtoSessionType::PointToPoint)
            .session_message_type(slim_datapath::api::ProtoSessionMessageType::Msg)
            .session_id(session_id)
            .message_id(1)
            .application_payload("test-app-data", app_data.clone())
            .build_publish()
            .unwrap();

        // call on message on the moderator (direction south)
        moderator
            .on_message_from_app(app_message)
            .await
            .expect("error sending application message from moderator");

        // check that message is received from slim with destination equal to participant name id
        let app_msg_to_slim = timeout(Duration::from_millis(100), rx_slim_moderator.recv())
            .await
            .expect("timeout waiting for application message on moderator slim channel")
            .expect("channel closed")
            .expect("error in application message");

        assert_eq!(app_msg_to_slim.get_dst(), participant_name_id);
        assert!(
            app_msg_to_slim.is_publish(),
            "message should be a publish message"
        );

        let app_msg_id = app_msg_to_slim.get_id();

        // call the on message on the participant (direction north)
        let mut app_msg_to_participant = app_msg_to_slim.clone();
        app_msg_to_participant
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        participant
            .on_message_from_slim(app_msg_to_participant)
            .await
            .expect("error processing application message on participant");

        // check that the message is received by the application
        let app_msg_received = timeout(Duration::from_millis(100), rx_app_participant.recv())
            .await
            .expect("timeout waiting for application message on participant app channel")
            .expect("channel closed")
            .expect("error in application message to app");

        assert_eq!(app_msg_received.get_source(), moderator_name);
        assert!(
            app_msg_received.is_publish(),
            "message should be a publish message"
        );

        let content = app_msg_received
            .get_payload()
            .unwrap()
            .as_application_payload()
            .unwrap()
            .blob
            .clone();
        assert_eq!(content, app_data);

        // check that an ack is sent to slim
        let ack_msg = timeout(Duration::from_millis(100), rx_slim_participant.recv())
            .await
            .expect("timeout waiting for ack on participant slim channel")
            .expect("channel closed")
            .expect("error in ack");

        assert_eq!(
            ack_msg.get_session_message_type(),
            slim_datapath::api::ProtoSessionMessageType::MsgAck,
            "message should be an ack"
        );
        assert_eq!(ack_msg.get_dst(), moderator_name);
        assert_eq!(ack_msg.get_id(), app_msg_id);

        // call the on message with the ack on the moderator
        let mut ack_to_moderator = ack_msg.clone();
        ack_to_moderator
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        moderator
            .on_message_from_slim(ack_to_moderator)
            .await
            .expect("error processing ack on moderator");

        // check that no other message is generated
        let no_more_moderator_after_ack =
            timeout(Duration::from_millis(100), rx_slim_moderator.recv()).await;
        assert!(
            no_more_moderator_after_ack.is_err(),
            "Expected no more messages on moderator slim channel after ack"
        );

        let no_more_participant_after_ack =
            timeout(Duration::from_millis(100), rx_slim_participant.recv()).await;
        assert!(
            no_more_participant_after_ack.is_err(),
            "Expected no more messages on participant slim channel after ack"
        );

        // create a leave request and send to moderator on message (direction south)
        let leave_request = Message::builder()
            .source(moderator_name.clone())
            .destination(participant_name.clone())
            .identity(test_identity())
            .session_type(slim_datapath::api::ProtoSessionType::PointToPoint)
            .session_message_type(slim_datapath::api::ProtoSessionMessageType::LeaveRequest)
            .session_id(session_id)
            .message_id(rand::random::<u32>())
            .payload(CommandPayload::builder().leave_request().as_content())
            .build_publish()
            .unwrap();

        moderator
            .on_message_from_app(leave_request)
            .await
            .expect("error sending leave request");

        // check that the request is received by slim on the moderator
        let received_leave_request = timeout(Duration::from_millis(100), rx_slim_moderator.recv())
            .await
            .expect("timeout waiting for leave request on moderator slim channel")
            .expect("channel closed")
            .expect("error in leave request");

        assert_eq!(
            received_leave_request.get_session_message_type(),
            slim_datapath::api::ProtoSessionMessageType::LeaveRequest
        );
        assert_eq!(received_leave_request.get_dst(), participant_name_id);

        // send the request to the participant (direction north)
        let mut leave_request_to_participant = received_leave_request.clone();
        leave_request_to_participant
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        participant
            .on_message_from_slim(leave_request_to_participant)
            .await
            .expect("error processing leave request on participant");

        // get the leave reply on the participant slim
        let leave_reply = timeout(Duration::from_millis(100), rx_slim_participant.recv())
            .await
            .expect("timeout waiting for leave reply on participant slim channel")
            .expect("channel closed")
            .expect("error in leave reply");

        assert_eq!(
            leave_reply.get_session_message_type(),
            slim_datapath::api::ProtoSessionMessageType::LeaveReply
        );
        assert_eq!(leave_reply.get_dst(), moderator_name);

        // participant removes route after processing leave request
        assert_eq!(
            rx_spy_participant.recv().await,
            Some(SubscriptionCall::RemoveRoute),
            "participant should remove route after leave request"
        );

        // send the leave reply to the moderator on message (direction north)
        let mut leave_reply_to_moderator = leave_reply.clone();
        leave_reply_to_moderator
            .get_slim_header_mut()
            .set_incoming_conn(Some(1));

        moderator
            .on_message_from_slim(leave_reply_to_moderator)
            .await
            .expect("error processing leave reply on moderator");

        // moderator removes route after processing leave reply
        assert_eq!(
            rx_spy_moderator.recv().await,
            Some(SubscriptionCall::RemoveRoute),
            "moderator should remove route after leave reply"
        );

        // check that no other messages are generated by the moderator
        let no_more_moderator_final =
            timeout(Duration::from_millis(100), rx_slim_moderator.recv()).await;

        assert!(
            no_more_moderator_final.is_err(),
            "Expected no more messages on moderator slim channel after leave"
        );

        let no_more_participant_final =
            timeout(Duration::from_millis(100), rx_slim_participant.recv()).await;
        assert!(
            no_more_participant_final.is_err(),
            "Expected no more messages on participant slim channel after leave"
        );
    }

    // ============================================================================
    // Draining Tests
    #[traced_test]
    #[tokio::test]
    async fn test_internal_draining_via_processing_state_switch() {
        use super::*;
        use tokio::sync::mpsc;
        use tracing::debug;

        // Custom handler that flips processing_state to Draining after first normal message
        struct InternalDrainHandler {
            state: ProcessingState,
            messages: Vec<SessionMessage>,
            needs_drain: Arc<AtomicBool>,
        }

        impl InternalDrainHandler {
            fn new(needs_drain: Arc<AtomicBool>) -> Self {
                Self {
                    state: ProcessingState::Active,
                    messages: vec![],
                    needs_drain,
                }
            }
        }

        impl MessageHandler for InternalDrainHandler {
            async fn init(&mut self) -> Result<(), SessionError> {
                Ok(())
            }

            async fn on_message(
                &mut self,
                message: SessionMessage,
            ) -> Result<SessionOutput, SessionError> {
                debug!(?self.state, "internal-drain-handler received message");
                self.messages.push(message);

                // when we receive 2 messages, transition to draining state
                if self.messages.len() == 2 {
                    debug!("internal-drain-handler transitioning to draining");
                    self.state = ProcessingState::Draining;
                }

                Ok(SessionOutput::new())
            }

            fn needs_drain(&self) -> bool {
                self.needs_drain.load(std::sync::atomic::Ordering::SeqCst)
            }

            fn processing_state(&self) -> ProcessingState {
                self.state
            }

            async fn on_shutdown(&mut self) -> Result<(), SessionError> {
                debug!("shutdown called on handler");
                Ok(())
            }
        }

        // Build minimal SessionSettings
        let (tx_slim, _rx_slim) = mpsc::channel(8);
        let (tx_app, _rx_app) = mpsc::unbounded_channel();
        let (tx_session, rx_session) = mpsc::channel(32);
        let (tx_session_layer, _rx_session_layer) = mpsc::channel(8);

        let subscription_manager =
            crate::subscription_manager::SubscriptionManager::new(tx_slim.clone());
        let settings = SessionSettings {
            id: 999,
            source: ProtoName::from_strings(["org", "ns", "source"]).with_id(1),
            destination: ProtoName::from_strings(["org", "ns", "dest"]).with_id(2),
            control: ProtoName::from_strings(["org", "ns", "dest"]).with_id(2),
            config: SessionConfig {
                session_type: ProtoSessionType::PointToPoint,
                max_retries: Some(3),
                interval: Some(Duration::from_millis(150)),
                mls_settings: None,
                initiator: true,
                metadata: HashMap::new(),
            },
            direction: Direction::Bidirectional,
            slim_tx: tx_slim,
            app_tx: tx_app,
            tx_session: tx_session.clone(),
            tx_to_session_layer: tx_session_layer,
            identity_provider: SharedSecret::new("src", SHARED_SECRET).unwrap(),
            identity_verifier: SharedSecret::new("src", SHARED_SECRET).unwrap(),
            graceful_shutdown_timeout: Some(Duration::from_secs(10)),
            subscription_manager,
            service_id: String::new(),
        };

        let needs_drain = Arc::new(AtomicBool::new(true));
        let handler = InternalDrainHandler::new(needs_drain.clone());
        let cancellation_token = CancellationToken::new();
        let cancellation_token_clone = cancellation_token.clone();

        // Spawn processing loop without unnecessary cloning
        let processing_handle = tokio::spawn(async move {
            SessionController::processing_loop(
                handler,
                rx_session,
                cancellation_token_clone,
                settings,
            )
            .await
        });

        // Send first regular message
        tx_session
            .send(create_test_message(1, b"first".to_vec()))
            .await
            .expect("failed to send first message");

        tokio::time::sleep(Duration::from_millis(100)).await;

        assert!(logs_contain("internal-drain-handler received message"));

        // Send second message; this causes internal handler move to draining (active -> draining)
        tx_session
            .send(create_test_message(2, b"second".to_vec()))
            .await
            .expect("failed to send second message");

        tokio::time::sleep(Duration::from_millis(100)).await;

        assert!(logs_contain("internal-drain-handler received message"));

        assert!(logs_contain(
            "internal-drain-handler transitioning to draining"
        ));

        // Send a third message that should not be processed, as draining is active
        tx_session
            .send(create_test_message(3, b"third".to_vec()))
            .await
            .expect("failed to send third message");

        tokio::time::sleep(Duration::from_millis(100)).await;

        assert!(logs_contain(
            "session is draining, rejecting new messages from application"
        ));

        // set needs drain to false to allow shutdown to complete
        needs_drain.store(false, std::sync::atomic::Ordering::SeqCst);

        // trigger cancellation to exit processing loop
        cancellation_token.cancel();

        tokio::time::sleep(Duration::from_millis(100)).await;

        // Send a session message to trigger the shutdown process
        tx_session
            .send(SessionMessage::StartDrain {
                grace_period: std::time::Duration::from_millis(100),
            })
            .await
            .expect("failed to send timeout message");

        // Wait for processing loop to complete
        processing_handle.await.expect("processing loop panicked");
    }
    // ============================================================================

    /// Mock handler that tracks draining behavior
    struct DrainableHandler {
        messages_received: Arc<tokio::sync::Mutex<Vec<SessionMessage>>>,
        needs_drain: Arc<AtomicBool>,
        shutdown_called: Arc<tokio::sync::Mutex<bool>>,
        drain_delay: Option<Duration>,
    }

    impl DrainableHandler {
        fn new() -> Self {
            Self {
                messages_received: Arc::new(tokio::sync::Mutex::new(Vec::new())),
                needs_drain: Arc::new(AtomicBool::new(false)),
                shutdown_called: Arc::new(tokio::sync::Mutex::new(false)),
                drain_delay: None,
            }
        }

        fn with_needs_drain(self, needs_drain: bool) -> Self {
            self.needs_drain
                .store(needs_drain, std::sync::atomic::Ordering::SeqCst);
            self
        }

        #[allow(dead_code)]
        fn with_drain_delay(mut self, delay: Duration) -> Self {
            self.drain_delay = Some(delay);
            self
        }

        #[allow(dead_code)]
        async fn get_messages_count(&self) -> usize {
            self.messages_received.lock().await.len()
        }

        #[allow(dead_code)]
        async fn was_shutdown_called(&self) -> bool {
            *self.shutdown_called.lock().await
        }
    }

    impl MessageHandler for DrainableHandler {
        async fn init(&mut self) -> Result<(), SessionError> {
            Ok(())
        }

        async fn on_message(
            &mut self,
            message: SessionMessage,
        ) -> Result<SessionOutput, SessionError> {
            self.messages_received.lock().await.push(message);
            Ok(SessionOutput::new())
        }

        fn needs_drain(&self) -> bool {
            self.needs_drain.load(std::sync::atomic::Ordering::SeqCst)
        }

        async fn on_shutdown(&mut self) -> Result<(), SessionError> {
            if let Some(delay) = self.drain_delay {
                tokio::time::sleep(delay).await;
            }
            *self.shutdown_called.lock().await = true;
            Ok(())
        }
    }

    /// Helper to create test SessionSettings
    fn create_test_settings(
        graceful_shutdown_timeout: Option<Duration>,
    ) -> SessionSettings<SharedSecret, SharedSecret> {
        let (tx_slim, _rx_slim) = tokio::sync::mpsc::channel(10);
        let (tx_app, _rx_app) = tokio::sync::mpsc::unbounded_channel();
        let (tx_session, _rx_session) = tokio::sync::mpsc::channel(10);
        let (tx_session_layer, _rx_session_layer) = tokio::sync::mpsc::channel(10);

        let subscription_manager =
            crate::subscription_manager::SubscriptionManager::new(tx_slim.clone());
        SessionSettings {
            id: 1,
            source: ProtoName::from_strings(["org", "ns", "test"]).with_id(1),
            destination: ProtoName::from_strings(["org", "ns", "test"]).with_id(2),
            control: ProtoName::from_strings(["org", "ns", "test"]).with_id(2),
            config: SessionConfig {
                session_type: ProtoSessionType::PointToPoint,
                max_retries: Some(5),
                interval: Some(Duration::from_millis(200)),
                mls_settings: None,
                initiator: true,
                metadata: HashMap::new(),
            },
            direction: Direction::Bidirectional,
            slim_tx: tx_slim,
            app_tx: tx_app,
            tx_session,
            tx_to_session_layer: tx_session_layer,
            identity_provider: SharedSecret::new("test", SHARED_SECRET).unwrap(),
            identity_verifier: SharedSecret::new("test", SHARED_SECRET).unwrap(),
            graceful_shutdown_timeout,
            subscription_manager,
            service_id: String::new(),
        }
    }

    /// Helper to create a test message
    fn create_test_message(message_id: u32, payload: Vec<u8>) -> SessionMessage {
        SessionMessage::OnMessage {
            message: Message::builder()
                .source(ProtoName::from_strings(["org", "ns", "test"]).with_id(1))
                .destination(ProtoName::from_strings(["org", "ns", "test"]).with_id(2))
                .identity(test_identity())
                .forward_to(1)
                .session_type(ProtoSessionType::PointToPoint)
                .session_message_type(ProtoSessionMessageType::Msg)
                .session_id(1)
                .message_id(message_id)
                .application_payload("test", payload)
                .build_publish()
                .unwrap(),
            direction: MessageDirection::South,
            ack_tx: None,
        }
    }

    async fn count_on_messages(messages: &Arc<tokio::sync::Mutex<Vec<SessionMessage>>>) -> usize {
        let messages = messages.lock().await;
        messages
            .iter()
            .filter(|msg| matches!(msg, SessionMessage::OnMessage { .. }))
            .count()
    }

    /// Helper to spawn a processing loop and return the task handle
    fn spawn_processing_loop(
        handler: DrainableHandler,
        rx: tokio::sync::mpsc::Receiver<SessionMessage>,
        cancellation_token: CancellationToken,
        settings: SessionSettings<SharedSecret, SharedSecret>,
    ) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            SessionController::processing_loop(handler, rx, cancellation_token, settings).await;
        })
    }

    #[tokio::test]
    async fn test_draining_processes_queued_messages() {
        let handler = DrainableHandler::new();
        let messages_received = handler.messages_received.clone();
        let shutdown_called = handler.shutdown_called.clone();

        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();

        let settings = create_test_settings(Some(Duration::from_secs(2)));
        let processing_task = spawn_processing_loop(handler, rx, cancellation_token, settings);

        // Send multiple messages before cancellation
        tx.send(create_test_message(1, vec![1, 2, 3]))
            .await
            .unwrap();
        tx.send(create_test_message(2, vec![4, 5, 6]))
            .await
            .unwrap();
        tx.send(create_test_message(3, vec![7, 8, 9]))
            .await
            .unwrap();

        // Give some time for messages to be queued
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Trigger cancellation
        token_clone.cancel();

        // Close the channel to signal no more messages
        drop(tx);

        // Wait for processing to complete
        timeout(Duration::from_secs(3), processing_task)
            .await
            .expect("timeout waiting for processing loop")
            .expect("processing loop panicked");

        // Verify all messages were processed
        let processed_messages = count_on_messages(&messages_received).await;
        assert_eq!(
            processed_messages, 3,
            "All queued messages should be processed during draining"
        );
        assert!(
            *shutdown_called.lock().await,
            "Shutdown should have been called"
        );
    }

    #[tokio::test]
    async fn test_draining_with_needs_drain_true() {
        let handler = DrainableHandler::new().with_needs_drain(true);
        let messages_received = handler.messages_received.clone();
        let shutdown_called = handler.shutdown_called.clone();

        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();

        let settings = create_test_settings(Some(Duration::from_secs(2)));
        let processing_task = spawn_processing_loop(handler, rx, cancellation_token, settings);

        // Send a message
        tx.send(create_test_message(1, vec![1, 2, 3]))
            .await
            .unwrap();
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Trigger cancellation and close channel
        token_clone.cancel();
        drop(tx);

        // Wait for processing to complete (should wait for drain timeout)
        timeout(Duration::from_secs(3), processing_task)
            .await
            .expect("timeout waiting for processing loop")
            .expect("processing loop panicked");

        // Verify message was processed and shutdown was called
        let processed_messages = count_on_messages(&messages_received).await;
        assert_eq!(processed_messages, 1, "Message should be processed");
        assert!(
            *shutdown_called.lock().await,
            "Shutdown should have been called after draining"
        );
    }

    #[tokio::test]
    async fn test_draining_with_needs_drain_false() {
        let handler = DrainableHandler::new().with_needs_drain(false);
        let messages_received = handler.messages_received.clone();
        let shutdown_called = handler.shutdown_called.clone();

        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();

        let settings = create_test_settings(Some(Duration::from_secs(2)));

        let start_time = tokio::time::Instant::now();
        let processing_task = spawn_processing_loop(handler, rx, cancellation_token, settings);

        // Send a message
        tx.send(create_test_message(1, vec![1, 2, 3]))
            .await
            .unwrap();
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Trigger cancellation and close channel
        token_clone.cancel();
        drop(tx);

        // Wait for processing to complete (should exit quickly)
        timeout(Duration::from_secs(1), processing_task)
            .await
            .expect("timeout waiting for processing loop")
            .expect("processing loop panicked");

        let elapsed = start_time.elapsed();

        // Verify message was processed and shutdown was called quickly
        let processed_messages = count_on_messages(&messages_received).await;
        assert_eq!(processed_messages, 1, "Message should be processed");
        assert!(
            *shutdown_called.lock().await,
            "Shutdown should have been called"
        );
        assert!(
            elapsed < Duration::from_millis(500),
            "Should exit quickly when no draining needed"
        );
    }

    #[tokio::test]
    async fn test_draining_timeout_enforced() {
        // Test that the timeout fires when draining takes too long with needs_drain=true
        let handler = DrainableHandler::new().with_needs_drain(true);

        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();

        let settings = create_test_settings(Some(Duration::from_millis(500)));
        let processing_task = spawn_processing_loop(handler, rx, cancellation_token, settings);

        // Give the processing loop a moment to start
        tokio::time::sleep(Duration::from_millis(50)).await;

        let start_time = tokio::time::Instant::now();

        // Trigger cancellation - this starts draining
        token_clone.cancel();

        // Keep sending messages to prevent channel from closing
        // This simulates a scenario where messages keep arriving during drain period
        let send_task = tokio::spawn(async move {
            for i in 0..10 {
                tokio::time::sleep(Duration::from_millis(100)).await;
                if tx
                    .send(create_test_message(i, vec![i as u8]))
                    .await
                    .is_err()
                {
                    break;
                }
            }
        });

        // Wait for processing to complete - should timeout after 500ms
        timeout(Duration::from_secs(2), processing_task)
            .await
            .expect("timeout waiting for processing loop")
            .expect("processing loop panicked");

        let elapsed = start_time.elapsed();

        // Verify timeout was enforced (should be around 500ms)
        assert!(
            elapsed >= Duration::from_millis(400),
            "Should wait at least close to the timeout period"
        );
        assert!(
            elapsed < Duration::from_secs(2),
            "Should respect the timeout and exit, not wait forever"
        );

        // Clean up the send task
        send_task.abort();
    }

    #[tokio::test]
    async fn test_draining_no_messages_in_queue() {
        let handler = DrainableHandler::new().with_needs_drain(true);
        let messages_received = handler.messages_received.clone();
        let shutdown_called = handler.shutdown_called.clone();

        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();

        let settings = create_test_settings(Some(Duration::from_secs(1)));
        let processing_task = spawn_processing_loop(handler, rx, cancellation_token, settings);

        // Trigger cancellation immediately without sending messages
        token_clone.cancel();
        drop(tx);

        // Wait for processing to complete
        timeout(Duration::from_secs(2), processing_task)
            .await
            .expect("timeout waiting for processing loop")
            .expect("processing loop panicked");

        // Verify no messages were processed but shutdown was called
        let processed_messages = count_on_messages(&messages_received).await;
        assert_eq!(processed_messages, 0, "No messages should be processed");
        assert!(
            *shutdown_called.lock().await,
            "Shutdown should still be called"
        );
    }

    #[tokio::test]
    async fn test_draining_messages_after_cancellation_processed() {
        let handler = DrainableHandler::new();
        let messages_received = handler.messages_received.clone();

        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();

        let settings = create_test_settings(Some(Duration::from_secs(2)));

        // Send messages before cancellation
        tx.send(create_test_message(1, vec![1, 2, 3]))
            .await
            .unwrap();
        tx.send(create_test_message(2, vec![4, 5, 6]))
            .await
            .unwrap();

        // Spawn the processing loop after messages are queued
        let processing_task = spawn_processing_loop(handler, rx, cancellation_token, settings);

        // Give a moment for processing to start
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Trigger cancellation while messages are in queue
        token_clone.cancel();

        // Close channel
        drop(tx);

        // Wait for processing to complete
        timeout(Duration::from_secs(3), processing_task)
            .await
            .expect("timeout waiting for processing loop")
            .expect("processing loop panicked");

        // Verify messages in queue when cancellation happened were still processed
        let processed_messages = count_on_messages(&messages_received).await;
        assert_eq!(
            processed_messages, 2,
            "Messages in queue during cancellation should be processed"
        );
    }
}