windows-threadpool-sys 0.1.3

Memory-safe access to the Windows thread pool APIs.
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
// Copyright (c) 2026 Mike Grier
//! Integration tests for the `TP_IO` backend's behavioral matrix.
//!
//! The five states a thread-pool I/O submission can reach are each covered
//! directly -- immediate failure, immediate success (skip-on-success),
//! pending completion, cancellation, and object rundown with operations
//! outstanding -- together with the accounting and reclamation invariants that
//! must hold across them at scale.
//!
//! Every test asserts on `outstanding()`, because that count is simultaneously
//! the number of unbalanced `StartThreadpoolIo` calls and the number of
//! operations whose storage the kernel or pool still owns. A drift in either
//! direction is the failure mode this matrix exists to catch.

#![cfg(windows)]

use std::collections::{HashMap, HashSet};
use std::io;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use std::path::{Path, PathBuf};
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;

use windows_overlapped_io_sys::{
    Issued, Operation, OperationState, Submitted, UnassociatedEndpoint,
};
use windows_sys::Win32::Foundation::{ERROR_IO_PENDING, ERROR_OPERATION_ABORTED};
use windows_sys::Win32::Storage::FileSystem::{
    FILE_FLAG_OVERLAPPED, ReadFile, SetFileCompletionNotificationModes, WriteFile,
};
use windows_sys::Win32::System::Pipes::CreateNamedPipeW;

use windows_threadpool_sys::callback_env::CallbackEnviron;
use windows_threadpool_sys::io::{IoCompletion, ThreadpoolIo};

/// The Win32 `FILE_SKIP_COMPLETION_PORT_ON_SUCCESS` flag for
/// `SetFileCompletionNotificationModes`; windows-sys does not export it.
/// Changing this value is a breaking change.
const FILE_SKIP_COMPLETION_PORT_ON_SUCCESS: u8 = 0x1;

/// Named-pipe creation flags; windows-sys exports these only as loose constants
/// in feature-gated modules, so the two this file needs are named here.
/// Changing either value is a breaking change.
mod pipe_mode {
    /// `PIPE_ACCESS_DUPLEX`.
    pub const ACCESS_DUPLEX: u32 = 0x0000_0003;
    /// `PIPE_TYPE_BYTE`.
    pub const TYPE_BYTE: u32 = 0x0000_0000;
}

/// How long a test will wait for the pool to deliver expected callbacks before
/// declaring the backend broken. Generous enough to absorb a loaded CI machine,
/// short enough that a genuine hang fails the run rather than stalling it.
const CALLBACK_TIMEOUT: Duration = Duration::from_secs(30);

// --- harness ---

/// One completion as observed by a test's callback.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Record {
    /// The `OVERLAPPED` address, which is the operation's identity.
    identity: usize,
    io_result: u32,
    bytes: usize,
    /// The payload the operation carried, when the test claims a typed payload.
    payload: usize,
}

/// Collects callback observations and lets the test thread wait for a specific
/// number of them rather than sleeping.
struct Recorder {
    records: Mutex<Vec<Record>>,
    arrived: Condvar,
}

impl Recorder {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            records: Mutex::new(Vec::new()),
            arrived: Condvar::new(),
        })
    }

    fn push(&self, record: Record) {
        let mut records = self.records.lock().expect("record a completion");
        records.push(record);
        self.arrived.notify_all();
    }

    fn records(&self) -> Vec<Record> {
        self.records.lock().expect("read completions").clone()
    }

    fn len(&self) -> usize {
        self.records.lock().expect("read completions").len()
    }

    /// Block until at least `count` completions have been recorded, failing the
    /// test rather than hanging if they never arrive.
    fn wait_for(&self, count: usize) -> Vec<Record> {
        let records = self.records.lock().expect("await completions");
        let (records, timeout) = self
            .arrived
            .wait_timeout_while(records, CALLBACK_TIMEOUT, |records| records.len() < count)
            .expect("await completions");
        assert!(
            !timeout.timed_out(),
            "timed out waiting for {count} completion(s); saw {}",
            records.len()
        );
        records.clone()
    }
}

/// A payload whose `Drop` is observable, proving that reclaiming an operation
/// really frees its payload rather than leaking the storage.
#[derive(Debug)]
struct DropTracked {
    index: usize,
    dropped: Arc<AtomicUsize>,
}

impl Drop for DropTracked {
    fn drop(&mut self) {
        self.dropped.fetch_add(1, Ordering::SeqCst);
    }
}

/// A second, differently-sized tracked payload, so a single object can be shown
/// to reclaim operations of mixed payload types through the type-erased thunk.
#[derive(Debug)]
struct DropTrackedWide {
    _filler: [u64; 16],
    dropped: Arc<AtomicUsize>,
}

impl Drop for DropTrackedWide {
    fn drop(&mut self) {
        self.dropped.fetch_add(1, Ordering::SeqCst);
    }
}

/// A raw buffer base pointer shared across submitting threads.
///
/// SAFETY: the pointee is a `Vec<u8>` that outlives every operation submitted
/// against it, and each operation is given a disjoint one-byte slot.
#[derive(Clone, Copy)]
struct SharedBuffer(*mut u8);

// SAFETY: the pointer is only offset into disjoint per-operation slots of a
// buffer that outlives all of them; no two operations touch the same byte.
unsafe impl Send for SharedBuffer {}
unsafe impl Sync for SharedBuffer {}

impl SharedBuffer {
    /// The one-byte slot reserved for `index`.
    ///
    /// This is a method rather than a field access on purpose: a closure that
    /// touched `self.0` directly would capture the bare `*mut u8` under
    /// disjoint closure capture and stop being `Send`.
    ///
    /// # Safety
    ///
    /// `index` must be within the buffer this was built from.
    unsafe fn slot(self, index: usize) -> *mut u8 {
        // SAFETY: forwarded from this function's own contract.
        unsafe { self.0.add(index) }
    }
}

fn temp_file_with(content: &[u8], tag: &str) -> PathBuf {
    let path = std::env::temp_dir().join(format!(
        "windows-threadpool-sys-tp-io-{tag}-{}.tmp",
        std::process::id()
    ));
    std::fs::write(&path, content).expect("write temp file");
    path
}

fn read_endpoint(path: &Path) -> UnassociatedEndpoint {
    UnassociatedEndpoint::open(path, true, false, 0).expect("open overlapped endpoint")
}

/// A connected server pipe end carrying no data, so a read on it stays pending
/// until it is cancelled. The client end is returned and must be kept alive.
fn pending_pipe(tag: &str) -> (UnassociatedEndpoint, std::fs::File) {
    let name = format!(
        "{}{}-{}",
        r"\\.\pipe\windows-threadpool-sys-tp-io-",
        tag,
        std::process::id()
    );
    let wide: Vec<u16> = name.encode_utf16().chain(std::iter::once(0)).collect();

    // SAFETY: creates a fresh overlapped named pipe from a valid wide name.
    let handle = unsafe {
        CreateNamedPipeW(
            wide.as_ptr(),
            pipe_mode::ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
            pipe_mode::TYPE_BYTE,
            1,
            4096,
            4096,
            0,
            ptr::null(),
        )
    };
    assert!(
        !handle.is_null() && handle as isize != -1,
        "CreateNamedPipeW failed: {}",
        io::Error::last_os_error()
    );

    // SAFETY: fresh, exclusively owned, and opened with FILE_FLAG_OVERLAPPED.
    let endpoint =
        unsafe { UnassociatedEndpoint::assume_overlapped(OwnedHandle::from_raw_handle(handle)) };

    // Connecting a client makes a server read pend rather than fail with
    // ERROR_PIPE_LISTENING; no data is ever written, so it never completes.
    let client = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open(&name)
        .expect("connect the client pipe end");

    (endpoint, client)
}

/// Classify an overlapped `ReadFile` for a handle that is not in
/// skip-on-success mode: both synchronous success and `ERROR_IO_PENDING`
/// deliver a completion callback.
///
/// SAFETY: `buffer` must point to at least `len` writable bytes that stay valid
/// until the operation completes, and `overlapped` must be the operation's own
/// identity pointer.
unsafe fn issue_read(
    handle: std::os::windows::io::BorrowedHandle<'_>,
    overlapped: *mut windows_sys::Win32::System::IO::OVERLAPPED,
    buffer: *mut u8,
    len: u32,
) -> io::Result<Issued> {
    // SAFETY: forwarded from this function's own contract.
    let ok = unsafe {
        ReadFile(
            handle.as_raw_handle(),
            buffer,
            len,
            ptr::null_mut(),
            overlapped,
        )
    };
    if ok != 0 {
        return Ok(Issued::Pending);
    }
    let error = io::Error::last_os_error();
    if error.raw_os_error() == Some(ERROR_IO_PENDING as i32) {
        return Ok(Issued::Pending);
    }
    Err(error)
}

// --- matrix state 1: immediate failure ---

/// A native call that fails immediately delivers no callback, so `submit` must
/// balance its own start with `CancelThreadpoolIo` and return the operation.
#[test]
fn immediate_failure_returns_the_operation_and_balances_accounting() {
    let path = temp_file_with(b"immediate failure", "immediate-failure");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: 0,
            });
        },
        None,
    )
    .expect("create TP_IO");

    let source = *b"denied";
    let src_ptr = source.as_ptr();
    let src_len = source.len() as u32;

    let operation = Operation::new(());
    // SAFETY: issues exactly one overlapped WriteFile on a read-only handle,
    // which fails immediately with ERROR_ACCESS_DENIED and queues no callback.
    let submitted = unsafe {
        tp.submit(operation, |handle, overlapped| {
            let ok = WriteFile(
                handle.as_raw_handle(),
                src_ptr,
                src_len,
                ptr::null_mut(),
                overlapped,
            );
            if ok != 0 {
                return Ok(Issued::Pending);
            }
            let error = io::Error::last_os_error();
            if error.raw_os_error() == Some(ERROR_IO_PENDING as i32) {
                return Ok(Issued::Pending);
            }
            Err(error)
        })
    };

    match submitted {
        Submitted::Failed { operation, error } => {
            assert_eq!(operation.state(), OperationState::Idle);
            assert!(error.raw_os_error().is_some(), "expected an OS error");
        }
        other => panic!("expected an immediate failure, got {other:?}"),
    }

    assert_eq!(tp.outstanding(), 0, "the start must have been balanced");
    assert_eq!(recorder.len(), 0, "a failed submission must not call back");

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// Repeating the failure path at scale must not drift the accounting.
#[test]
fn repeated_immediate_failures_do_not_drift_accounting() {
    const ATTEMPTS: usize = 500;

    let path = temp_file_with(b"repeat", "repeat-failure");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: 0,
            });
        },
        None,
    )
    .expect("create TP_IO");

    let dropped = Arc::new(AtomicUsize::new(0));
    for index in 0..ATTEMPTS {
        let operation = Operation::new(DropTracked {
            index,
            dropped: Arc::clone(&dropped),
        });
        // SAFETY: `issue` starts no operation and reports the failure, so no
        // callback will arrive -- exactly what the `Err` contract requires.
        let submitted = unsafe {
            tp.submit(operation, |_handle, _overlapped| {
                Err(io::Error::from_raw_os_error(5))
            })
        };
        match submitted {
            Submitted::Failed { operation, .. } => {
                assert_eq!(operation.payload().index, index, "payload must survive");
            }
            other => panic!("expected an immediate failure, got {other:?}"),
        }
        assert_eq!(tp.outstanding(), 0, "accounting drifted at attempt {index}");
    }

    assert_eq!(
        dropped.load(Ordering::SeqCst),
        ATTEMPTS,
        "every returned operation's payload must be dropped"
    );
    assert_eq!(recorder.len(), 0, "failed submissions must not call back");

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

// --- matrix state 2: immediate success (skip-on-success) ---

/// With `FILE_SKIP_COMPLETION_PORT_ON_SUCCESS` a synchronous success queues no
/// callback, so `submit` must balance the start and hand back the operation.
///
/// The mode is a request, not a guarantee: an uncached read can still pend, so
/// this test accepts either outcome and asserts the invariants of whichever one
/// occurred.
#[test]
fn immediate_success_balances_accounting_without_a_callback() {
    let content = b"skip on success payload";
    let path = temp_file_with(content, "skip-success");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<()> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<()>() };
            assert_eq!(operation.state(), OperationState::Completed);
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: 0,
            });
        },
        None,
    )
    .expect("create TP_IO");

    // SAFETY: the object owns a valid overlapped handle bound to the pool.
    let modes_ok = unsafe {
        SetFileCompletionNotificationModes(
            tp.handle().as_raw_handle(),
            FILE_SKIP_COMPLETION_PORT_ON_SUCCESS,
        )
    };
    assert_ne!(
        modes_ok,
        0,
        "SetFileCompletionNotificationModes failed: {}",
        io::Error::last_os_error()
    );

    let mut buffer = [0_u8; 64];
    let buf_ptr = buffer.as_mut_ptr();
    let buf_len = buffer.len() as u32;
    let mut bytes: u32 = 0;
    let bytes_ptr: *mut u32 = &mut bytes;

    let mut operation = Operation::new(());
    operation.set_offset(0);

    // SAFETY: issues exactly one overlapped ReadFile into `buffer`, which
    // outlives the operation because this test blocks below. Under
    // skip-on-success a synchronous success writes `bytes` and queues no
    // callback, so it is reported as Completed.
    let submitted = unsafe {
        tp.submit(operation, |handle, overlapped| {
            let ok = ReadFile(
                handle.as_raw_handle(),
                buf_ptr,
                buf_len,
                bytes_ptr,
                overlapped,
            );
            if ok != 0 {
                return Ok(Issued::Completed {
                    bytes_transferred: *bytes_ptr,
                });
            }
            let error = io::Error::last_os_error();
            if error.raw_os_error() == Some(ERROR_IO_PENDING as i32) {
                return Ok(Issued::Pending);
            }
            Err(error)
        })
    };

    match submitted {
        Submitted::Completed {
            operation,
            bytes_transferred,
        } => {
            assert_eq!(operation.state(), OperationState::Completed);
            assert_eq!(bytes_transferred as usize, content.len());
            assert_eq!(&buffer[..content.len()], content);
            assert_eq!(tp.outstanding(), 0, "the start must have been balanced");
            assert_eq!(recorder.len(), 0, "no callback may run on this path");
        }
        Submitted::Pending(id) => {
            let records = recorder.wait_for(1);
            assert_eq!(records[0].identity, id.as_ptr() as usize);
            assert_eq!(records[0].bytes, content.len());
            tp.run_down();
            assert_eq!(tp.outstanding(), 0);
        }
        Submitted::Failed { error, .. } => panic!("submit failed: {error}"),
    }

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

// --- matrix state 3: pending completion ---

/// A pending read is completed by the pool's callback, which claims the typed
/// operation and thereby reclaims its storage.
#[test]
fn pending_completion_delivers_the_callback_and_claims_the_operation() {
    let content = b"thread pool overlapped read";
    let path = temp_file_with(content, "pending-read");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            assert_eq!(operation.state(), OperationState::Completed);
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        None,
    )
    .expect("create TP_IO");

    let mut buffer = [0_u8; 64];
    let buf_ptr = buffer.as_mut_ptr();
    let buf_len = buffer.len() as u32;

    let mut operation = Operation::new(0xABCD_usize);
    operation.set_offset(0);

    // SAFETY: issues exactly one overlapped ReadFile into `buffer`, which stays
    // alive until this test blocks in `wait_for` / `run_down` below.
    let submitted = unsafe {
        tp.submit(operation, |handle, ov| {
            issue_read(handle, ov, buf_ptr, buf_len)
        })
    };

    let id = match submitted {
        Submitted::Pending(id) => id,
        other => panic!("expected a pending submission, got {other:?}"),
    };

    let records = recorder.wait_for(1);
    assert_eq!(records.len(), 1, "expected exactly one completion");
    assert_eq!(
        records[0].identity,
        id.as_ptr() as usize,
        "identity mismatch"
    );
    assert_eq!(records[0].io_result, 0, "expected a successful read");
    assert_eq!(records[0].bytes, content.len());
    assert_eq!(
        records[0].payload, 0xABCD,
        "payload must survive the round trip"
    );
    assert_eq!(&buffer[..content.len()], content);

    tp.run_down();
    assert_eq!(tp.outstanding(), 0);

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// Identities must stay distinct across operations that are outstanding *at the
/// same time*, and each must call back exactly once carrying its own payload.
///
/// The reads are issued on a pipe carrying no data, so none of them can complete
/// while the rest are being submitted. That matters: an identity is the address
/// of the operation's storage, so once an operation completes and is reclaimed
/// the allocator may hand the same address to a later operation. Uniqueness is a
/// property of the live set, not of all operations for all time -- which is
/// exactly why this test keeps every operation live until it asserts.
#[test]
fn simultaneously_outstanding_operations_keep_distinct_identities() {
    const OPERATIONS: usize = 256;

    let (endpoint, _client) = pending_pipe("identity");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        endpoint,
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        None,
    )
    .expect("create TP_IO");

    // One byte of stable storage per operation; never reallocated after `base`
    // is taken, and it outlives every operation.
    let mut landed = vec![0_u8; OPERATIONS];
    let base = landed.as_mut_ptr();

    let mut identities: HashMap<usize, usize> = HashMap::new();
    for slot in 0..OPERATIONS {
        let operation = Operation::new(slot);
        // SAFETY: issues exactly one 1-byte overlapped ReadFile into this slot's
        // own byte of `landed`; slot < OPERATIONS and `landed` outlives it
        // because the rundown below blocks until every callback has run.
        let submitted = unsafe {
            tp.submit(operation, |handle, ov| {
                issue_read(handle, ov, base.add(slot), 1)
            })
        };
        match submitted {
            Submitted::Pending(id) => {
                assert!(
                    identities.insert(id.as_ptr() as usize, slot).is_none(),
                    "two simultaneously outstanding operations shared identity {:p}",
                    id.as_ptr()
                );
            }
            other => panic!("expected pending at slot {slot}, got {other:?}"),
        }
    }

    // Every operation is still live, so every identity must be distinct.
    assert_eq!(tp.outstanding(), OPERATIONS);
    assert_eq!(identities.len(), OPERATIONS);

    tp.cancel_all().expect("cancel every outstanding read");
    let records = recorder.wait_for(OPERATIONS);
    tp.run_down();
    assert_eq!(tp.outstanding(), 0);
    assert_eq!(records.len(), OPERATIONS, "every operation must call back");

    let mut payloads_seen: HashSet<usize> = HashSet::new();
    for record in &records {
        let slot = *identities
            .get(&record.identity)
            .unwrap_or_else(|| panic!("unknown identity {:#x}", record.identity));
        assert_eq!(
            record.payload, slot,
            "identity and payload disagree about the slot"
        );
        assert!(
            payloads_seen.insert(slot),
            "slot {slot} completed more than once"
        );
        assert_eq!(
            record.io_result, ERROR_OPERATION_ABORTED,
            "slot {slot} should have been aborted"
        );
    }
    assert_eq!(payloads_seen.len(), OPERATIONS);
}

/// At scale, every read must complete exactly once and land its own data.
///
/// Unlike the identity test above these reads complete as fast as they are
/// issued, so operations are *not* all simultaneously live and their storage
/// addresses may be recycled. Correlation is therefore by payload, which is
/// carried inside the operation and is unique for all time.
#[test]
fn many_file_reads_each_complete_once_with_their_own_data() {
    const OPERATIONS: usize = 512;

    // A distinct byte per offset, so each read's landed byte identifies its slot.
    let content: Vec<u8> = (0..OPERATIONS).map(|i| i as u8).collect();
    let path = temp_file_with(&content, "scale-reads");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        None,
    )
    .expect("create TP_IO");

    // One byte of stable storage per operation; never reallocated after `base`
    // is taken, and it outlives every operation.
    let mut landed = vec![0_u8; OPERATIONS];
    let base = landed.as_mut_ptr();

    for slot in 0..OPERATIONS {
        let mut operation = Operation::new(slot);
        operation.set_offset(slot as u64);
        // SAFETY: issues exactly one 1-byte overlapped ReadFile into this slot's
        // own byte of `landed`; slot < OPERATIONS and `landed` outlives it.
        let submitted = unsafe {
            tp.submit(operation, |handle, ov| {
                issue_read(handle, ov, base.add(slot), 1)
            })
        };
        assert!(
            matches!(submitted, Submitted::Pending(_)),
            "slot {slot}: expected pending, got {submitted:?}"
        );
    }

    let records = recorder.wait_for(OPERATIONS);
    tp.run_down();
    assert_eq!(tp.outstanding(), 0);
    assert_eq!(records.len(), OPERATIONS, "every operation must call back");

    let mut payloads_seen: HashSet<usize> = HashSet::new();
    for record in &records {
        assert!(
            record.payload < OPERATIONS,
            "payload {} is out of range",
            record.payload
        );
        assert!(
            payloads_seen.insert(record.payload),
            "slot {} completed more than once",
            record.payload
        );
        assert_eq!(record.io_result, 0, "slot {} failed", record.payload);
        assert_eq!(
            record.bytes, 1,
            "slot {} read the wrong length",
            record.payload
        );
    }
    assert_eq!(payloads_seen.len(), OPERATIONS);

    // Each slot must have received the byte stored at its own offset.
    for (slot, byte) in landed.iter().enumerate() {
        assert_eq!(*byte, slot as u8, "slot {slot} landed the wrong byte");
    }

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// Operations submitted concurrently from several threads must be accounted for
/// exactly once each: `ThreadpoolIo` is `Send + Sync` and advertises this.
#[test]
fn concurrent_submissions_from_many_threads_are_accounted_exactly_once() {
    const THREADS: usize = 8;
    const PER_THREAD: usize = 64;
    const OPERATIONS: usize = THREADS * PER_THREAD;

    let content: Vec<u8> = (0..OPERATIONS).map(|i| i as u8).collect();
    let path = temp_file_with(&content, "threads");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let mut landed = vec![0_u8; OPERATIONS];
    let base = SharedBuffer(landed.as_mut_ptr());

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        None,
    )
    .expect("create TP_IO");

    std::thread::scope(|scope| {
        for thread in 0..THREADS {
            let tp = &tp;
            scope.spawn(move || {
                for step in 0..PER_THREAD {
                    let slot = thread * PER_THREAD + step;
                    let mut operation = Operation::new(slot);
                    operation.set_offset(slot as u64);
                    // SAFETY: one 1-byte overlapped ReadFile into this slot's own
                    // byte; slots are disjoint across threads and `landed`
                    // outlives every operation.
                    let submitted = unsafe {
                        tp.submit(operation, |handle, ov| {
                            issue_read(handle, ov, base.slot(slot), 1)
                        })
                    };
                    assert!(
                        matches!(submitted, Submitted::Pending(_)),
                        "slot {slot}: expected pending, got {submitted:?}"
                    );
                }
            });
        }
    });

    let records = recorder.wait_for(OPERATIONS);
    tp.run_down();
    assert_eq!(tp.outstanding(), 0);
    assert_eq!(records.len(), OPERATIONS);

    let payloads: HashSet<usize> = records.iter().map(|record| record.payload).collect();
    assert_eq!(payloads.len(), OPERATIONS, "every slot must complete once");
    for record in &records {
        assert_eq!(record.io_result, 0, "slot {} failed", record.payload);
    }
    for (slot, byte) in landed.iter().enumerate() {
        assert_eq!(*byte, slot as u8, "slot {slot} landed the wrong byte");
    }

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// Sequential submissions reuse one object without accumulating state.
#[test]
fn sequential_submissions_reuse_the_object() {
    const ROUNDS: usize = 25;

    let content: Vec<u8> = (0..ROUNDS).map(|i| (i * 3) as u8).collect();
    let path = temp_file_with(&content, "sequential");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        None,
    )
    .expect("create TP_IO");

    let mut landed = vec![0_u8; ROUNDS];
    let base = landed.as_mut_ptr();

    for round in 0..ROUNDS {
        let mut operation = Operation::new(round);
        operation.set_offset(round as u64);
        // SAFETY: one 1-byte overlapped ReadFile into this round's own byte;
        // `landed` outlives every operation and this loop drains each round.
        let submitted = unsafe {
            tp.submit(operation, |handle, ov| {
                issue_read(handle, ov, base.add(round), 1)
            })
        };
        assert!(
            matches!(submitted, Submitted::Pending(_)),
            "round {round}: expected pending, got {submitted:?}"
        );

        // Drain this round before starting the next one.
        recorder.wait_for(round + 1);
        tp.run_down();
        assert_eq!(tp.outstanding(), 0, "round {round} left work outstanding");
    }

    let records = recorder.records();
    assert_eq!(records.len(), ROUNDS);
    for (round, record) in records.iter().enumerate() {
        assert_eq!(record.payload, round, "rounds completed out of order");
        assert_eq!(record.io_result, 0);
    }
    for (round, byte) in landed.iter().enumerate() {
        assert_eq!(*byte, (round * 3) as u8);
    }

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

// --- matrix state 4: cancellation ---

/// Cancelling one operation still completes it -- as `ERROR_OPERATION_ABORTED`
/// -- and that callback remains the point at which its storage is reclaimed.
#[test]
fn cancelling_one_operation_completes_it_as_aborted() {
    let (endpoint, _client) = pending_pipe("cancel-one");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        endpoint,
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        None,
    )
    .expect("create TP_IO");

    let mut buffer = [0_u8; 32];
    let buf_ptr = buffer.as_mut_ptr();
    let buf_len = buffer.len() as u32;

    let operation = Operation::new(7_usize);
    // SAFETY: one overlapped ReadFile on a connected pipe carrying no data, so
    // it stays pending until cancelled; `buffer` outlives the wait below.
    let submitted = unsafe {
        tp.submit(operation, |handle, ov| {
            issue_read(handle, ov, buf_ptr, buf_len)
        })
    };

    let id = match submitted {
        Submitted::Pending(id) => id,
        other => panic!("expected a pending submission, got {other:?}"),
    };

    assert_eq!(tp.outstanding(), 1, "the read must still be outstanding");
    assert_eq!(
        recorder.len(),
        0,
        "a pending read must not have called back"
    );

    tp.cancel(id).expect("cancel the pending read");

    let records = recorder.wait_for(1);
    assert_eq!(records.len(), 1);
    assert_eq!(records[0].identity, id.as_ptr() as usize);
    assert_eq!(
        records[0].io_result, ERROR_OPERATION_ABORTED,
        "a cancelled operation must complete as ERROR_OPERATION_ABORTED"
    );
    assert_eq!(records[0].payload, 7);

    tp.run_down();
    assert_eq!(tp.outstanding(), 0);
}

/// `cancel_all` aborts every outstanding operation on the endpoint, and each one
/// still reports its own completion.
#[test]
fn cancel_all_aborts_every_outstanding_operation() {
    const OPERATIONS: usize = 32;

    let (endpoint, _client) = pending_pipe("cancel-all");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        endpoint,
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        None,
    )
    .expect("create TP_IO");

    let mut landed = vec![0_u8; OPERATIONS];
    let base = landed.as_mut_ptr();

    let mut identities = HashSet::new();
    for slot in 0..OPERATIONS {
        let operation = Operation::new(slot);
        // SAFETY: one 1-byte overlapped ReadFile per slot on a pipe carrying no
        // data; each slot is disjoint and `landed` outlives every operation.
        let submitted = unsafe {
            tp.submit(operation, |handle, ov| {
                issue_read(handle, ov, base.add(slot), 1)
            })
        };
        match submitted {
            Submitted::Pending(id) => {
                identities.insert(id.as_ptr() as usize);
            }
            other => panic!("expected pending at slot {slot}, got {other:?}"),
        }
    }

    assert_eq!(tp.outstanding(), OPERATIONS);

    tp.cancel_all().expect("cancel every outstanding read");

    let records = recorder.wait_for(OPERATIONS);
    tp.run_down();
    assert_eq!(tp.outstanding(), 0);
    assert_eq!(records.len(), OPERATIONS);

    let mut payloads = HashSet::new();
    for record in &records {
        assert!(
            identities.contains(&record.identity),
            "unknown identity {:#x}",
            record.identity
        );
        assert_eq!(
            record.io_result, ERROR_OPERATION_ABORTED,
            "slot {} was not aborted",
            record.payload
        );
        assert!(payloads.insert(record.payload), "a slot completed twice");
    }
    assert_eq!(payloads.len(), OPERATIONS);
}

/// Cancelling when nothing is outstanding is a benign no-op that reports
/// `ERROR_NOT_FOUND` rather than corrupting the accounting.
#[test]
fn cancel_all_with_nothing_outstanding_is_benign() {
    let (endpoint, _client) = pending_pipe("cancel-empty");
    let tp = ThreadpoolIo::new(endpoint, |_| {}, None).expect("create TP_IO");

    // Either outcome is acceptable; what matters is that nothing is disturbed.
    let _ = tp.cancel_all();

    assert_eq!(tp.outstanding(), 0);
    tp.run_down();
    tp.wait();
    assert_eq!(tp.outstanding(), 0);
}

// --- matrix state 5: rundown with operations outstanding ---

/// Dropping the object with operations outstanding must cancel them, wait for
/// the resulting callbacks, and terminate -- never free storage the kernel still
/// owns, and never block forever.
#[test]
fn drop_with_operations_outstanding_cancels_drains_and_terminates() {
    const OPERATIONS: usize = 16;

    let (endpoint, _client) = pending_pipe("drop-outstanding");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);
    let dropped = Arc::new(AtomicUsize::new(0));
    let payload_dropped = Arc::clone(&dropped);

    let mut landed = vec![0_u8; OPERATIONS];
    let base = landed.as_mut_ptr();

    {
        let tp = ThreadpoolIo::new(
            endpoint,
            move |completion: &IoCompletion| {
                // SAFETY: only Operation<DropTracked> is submitted below, claimed once.
                let operation = unsafe { completion.claim::<DropTracked>() };
                seen.push(Record {
                    identity: completion.overlapped_ptr() as usize,
                    io_result: completion.io_result(),
                    bytes: completion.bytes_transferred(),
                    payload: operation.payload().index,
                });
            },
            None,
        )
        .expect("create TP_IO");

        for slot in 0..OPERATIONS {
            let operation = Operation::new(DropTracked {
                index: slot,
                dropped: Arc::clone(&payload_dropped),
            });
            // SAFETY: one 1-byte overlapped ReadFile per slot on a pipe carrying
            // no data, so each stays pending; `landed` outlives the whole block
            // because Drop below blocks until every callback has run.
            let submitted = unsafe {
                tp.submit(operation, |handle, ov| {
                    issue_read(handle, ov, base.add(slot), 1)
                })
            };
            assert!(
                matches!(submitted, Submitted::Pending(_)),
                "slot {slot}: expected pending, got {submitted:?}"
            );
        }

        assert_eq!(tp.outstanding(), OPERATIONS, "reads must be outstanding");

        // Drop here, with every operation still outstanding. This must cancel,
        // drain, and return rather than deadlock or leak.
    }

    let records = recorder.records();
    assert_eq!(
        records.len(),
        OPERATIONS,
        "rundown must let every outstanding operation call back"
    );
    for record in &records {
        assert_eq!(
            record.io_result, ERROR_OPERATION_ABORTED,
            "slot {} should have been aborted by rundown",
            record.payload
        );
    }
    assert_eq!(
        dropped.load(Ordering::SeqCst),
        OPERATIONS,
        "every claimed payload must have been dropped"
    );
}

/// The voluntary path -- `cancel_all` then `run_down` -- reaches the same state
/// as `Drop`, but under the caller's control.
#[test]
fn explicit_cancel_all_then_run_down_drains_every_operation() {
    const OPERATIONS: usize = 24;

    let (endpoint, _client) = pending_pipe("explicit-rundown");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        endpoint,
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        None,
    )
    .expect("create TP_IO");

    let mut landed = vec![0_u8; OPERATIONS];
    let base = landed.as_mut_ptr();

    for slot in 0..OPERATIONS {
        let operation = Operation::new(slot);
        // SAFETY: one 1-byte overlapped ReadFile per slot on a pipe carrying no
        // data; `landed` outlives every operation because run_down blocks below.
        let submitted = unsafe {
            tp.submit(operation, |handle, ov| {
                issue_read(handle, ov, base.add(slot), 1)
            })
        };
        assert!(matches!(submitted, Submitted::Pending(_)));
    }

    assert_eq!(tp.outstanding(), OPERATIONS);

    tp.cancel_all().expect("cancel every outstanding read");
    tp.run_down();

    assert_eq!(tp.outstanding(), 0, "run_down must drain completely");
    assert_eq!(recorder.len(), OPERATIONS, "every operation must call back");

    // Dropping after a completed rundown must not block or double-free.
    drop(tp);
}

// --- reclamation invariants ---

/// A callback that claims nothing still reclaims the operation, freeing its
/// payload through the type-erased thunk armed at submission.
#[test]
fn unclaimed_completions_reclaim_their_payload() {
    const OPERATIONS: usize = 256;

    let content: Vec<u8> = (0..OPERATIONS).map(|i| i as u8).collect();
    let path = temp_file_with(&content, "unclaimed");

    let dropped = Arc::new(AtomicUsize::new(0));
    let payload_dropped = Arc::clone(&dropped);
    let observed = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&observed);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |_completion: &IoCompletion| {
            // Deliberately claim nothing: the completion's own drop must reclaim
            // the operation generically.
            counter.fetch_add(1, Ordering::SeqCst);
        },
        None,
    )
    .expect("create TP_IO");

    let mut landed = vec![0_u8; OPERATIONS];
    let base = landed.as_mut_ptr();

    for slot in 0..OPERATIONS {
        let mut operation = Operation::new(DropTracked {
            index: slot,
            dropped: Arc::clone(&payload_dropped),
        });
        operation.set_offset(slot as u64);
        // SAFETY: one 1-byte overlapped ReadFile into this slot's own byte;
        // `landed` outlives every operation because run_down blocks below.
        let submitted = unsafe {
            tp.submit(operation, |handle, ov| {
                issue_read(handle, ov, base.add(slot), 1)
            })
        };
        assert!(
            matches!(submitted, Submitted::Pending(_)),
            "slot {slot}: expected pending, got {submitted:?}"
        );
    }

    tp.run_down();
    assert_eq!(tp.outstanding(), 0);
    assert_eq!(observed.load(Ordering::SeqCst), OPERATIONS);
    assert_eq!(
        dropped.load(Ordering::SeqCst),
        OPERATIONS,
        "an unclaimed completion must still free its payload"
    );

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// One object may carry operations of different payload types simultaneously,
/// because unclaimed reclamation reads the thunk armed for each operation's own
/// payload type rather than assuming a single `P`.
#[test]
fn mixed_payload_types_reclaim_generically() {
    const PAIRS: usize = 100;
    const OPERATIONS: usize = PAIRS * 2;

    let content: Vec<u8> = (0..OPERATIONS).map(|i| i as u8).collect();
    let path = temp_file_with(&content, "mixed-payloads");

    let narrow_dropped = Arc::new(AtomicUsize::new(0));
    let wide_dropped = Arc::new(AtomicUsize::new(0));
    let observed = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&observed);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |_completion: &IoCompletion| {
            // The callback cannot know which payload type this completion
            // carries, so it claims nothing and lets the armed thunk decide.
            counter.fetch_add(1, Ordering::SeqCst);
        },
        None,
    )
    .expect("create TP_IO");

    let mut landed = vec![0_u8; OPERATIONS];
    let base = landed.as_mut_ptr();

    for pair in 0..PAIRS {
        let narrow_slot = pair * 2;
        let mut narrow = Operation::new(DropTracked {
            index: narrow_slot,
            dropped: Arc::clone(&narrow_dropped),
        });
        narrow.set_offset(narrow_slot as u64);
        // SAFETY: one 1-byte overlapped ReadFile into this slot's own byte.
        let submitted = unsafe {
            tp.submit(narrow, |handle, ov| {
                issue_read(handle, ov, base.add(narrow_slot), 1)
            })
        };
        assert!(matches!(submitted, Submitted::Pending(_)));

        let wide_slot = pair * 2 + 1;
        let mut wide = Operation::new(DropTrackedWide {
            _filler: [0; 16],
            dropped: Arc::clone(&wide_dropped),
        });
        wide.set_offset(wide_slot as u64);
        // SAFETY: one 1-byte overlapped ReadFile into this slot's own byte.
        let submitted = unsafe {
            tp.submit(wide, |handle, ov| {
                issue_read(handle, ov, base.add(wide_slot), 1)
            })
        };
        assert!(matches!(submitted, Submitted::Pending(_)));
    }

    tp.run_down();
    assert_eq!(tp.outstanding(), 0);
    assert_eq!(observed.load(Ordering::SeqCst), OPERATIONS);
    assert_eq!(
        narrow_dropped.load(Ordering::SeqCst),
        PAIRS,
        "narrow payloads must be reclaimed with their own thunk"
    );
    assert_eq!(
        wide_dropped.load(Ordering::SeqCst),
        PAIRS,
        "wide payloads must be reclaimed with their own thunk"
    );

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

// --- edge cases ---

/// A read starting past end-of-file transfers nothing; whichever way Windows
/// reports it, the accounting must balance.
#[test]
fn read_past_end_of_file_still_balances_accounting() {
    let content = b"short";
    let path = temp_file_with(content, "past-eof");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<()> is submitted below, claimed once.
            let _operation = unsafe { completion.claim::<()>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: 0,
            });
        },
        None,
    )
    .expect("create TP_IO");

    let mut buffer = [0_u8; 16];
    let buf_ptr = buffer.as_mut_ptr();
    let buf_len = buffer.len() as u32;

    let mut operation = Operation::new(());
    operation.set_offset(4096);

    // SAFETY: one overlapped ReadFile well past EOF; `buffer` outlives the wait.
    let submitted = unsafe {
        tp.submit(operation, |handle, ov| {
            issue_read(handle, ov, buf_ptr, buf_len)
        })
    };

    match submitted {
        Submitted::Pending(_) => {
            let records = recorder.wait_for(1);
            assert_eq!(records[0].bytes, 0, "a read past EOF transfers nothing");
        }
        Submitted::Failed { .. } => {
            assert_eq!(recorder.len(), 0, "a failed submission must not call back");
        }
        Submitted::Completed {
            bytes_transferred, ..
        } => {
            assert_eq!(bytes_transferred, 0, "a read past EOF transfers nothing");
        }
    }

    tp.run_down();
    assert_eq!(tp.outstanding(), 0);

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// A zero-length read completes without transferring anything and is accounted
/// for like any other operation.
#[test]
fn zero_length_read_completes_and_balances_accounting() {
    let path = temp_file_with(b"zero length read", "zero-length");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<()> is submitted below, claimed once.
            let _operation = unsafe { completion.claim::<()>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: 0,
            });
        },
        None,
    )
    .expect("create TP_IO");

    let mut buffer = [0_u8; 8];
    let buf_ptr = buffer.as_mut_ptr();

    let mut operation = Operation::new(());
    operation.set_offset(0);

    // SAFETY: one zero-length overlapped ReadFile; `buffer` outlives the wait.
    let submitted =
        unsafe { tp.submit(operation, |handle, ov| issue_read(handle, ov, buf_ptr, 0)) };

    match submitted {
        Submitted::Pending(_) => {
            let records = recorder.wait_for(1);
            assert_eq!(records[0].bytes, 0);
        }
        Submitted::Completed {
            bytes_transferred, ..
        } => assert_eq!(bytes_transferred, 0),
        Submitted::Failed { error, .. } => panic!("zero-length read failed: {error}"),
    }

    tp.run_down();
    assert_eq!(tp.outstanding(), 0);

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// An object created with an explicit callback environment behaves identically.
#[test]
fn operations_run_under_an_explicit_callback_environment() {
    const OPERATIONS: usize = 16;

    let content: Vec<u8> = (0..OPERATIONS).map(|i| i as u8).collect();
    let path = temp_file_with(&content, "callback-env");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let mut env = CallbackEnviron::new();
    env.set_runs_long();

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<usize> is submitted below, claimed once.
            let operation = unsafe { completion.claim::<usize>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: *operation.payload(),
            });
        },
        Some(&mut env),
    )
    .expect("create TP_IO with a callback environment");

    let mut landed = vec![0_u8; OPERATIONS];
    let base = landed.as_mut_ptr();

    for slot in 0..OPERATIONS {
        let mut operation = Operation::new(slot);
        operation.set_offset(slot as u64);
        // SAFETY: one 1-byte overlapped ReadFile into this slot's own byte.
        let submitted = unsafe {
            tp.submit(operation, |handle, ov| {
                issue_read(handle, ov, base.add(slot), 1)
            })
        };
        assert!(matches!(submitted, Submitted::Pending(_)));
    }

    let records = recorder.wait_for(OPERATIONS);
    tp.run_down();
    assert_eq!(tp.outstanding(), 0);

    let payloads: HashSet<usize> = records.iter().map(|record| record.payload).collect();
    assert_eq!(payloads.len(), OPERATIONS);
    for (slot, byte) in landed.iter().enumerate() {
        assert_eq!(*byte, slot as u8);
    }

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// An object that never submits anything runs down and drops cleanly.
#[test]
fn object_with_no_submissions_runs_down_and_drops_cleanly() {
    let path = temp_file_with(b"unused", "no-submissions");

    let tp = ThreadpoolIo::new(read_endpoint(&path), |_| {}, None).expect("create TP_IO");
    assert_eq!(tp.outstanding(), 0);
    tp.run_down();
    tp.wait();
    assert_eq!(tp.outstanding(), 0);

    drop(tp);
    let _ = std::fs::remove_file(&path);
}

/// `run_down` and `wait` are idempotent once the object is quiescent.
#[test]
fn run_down_and_wait_are_idempotent_when_quiescent() {
    let content = b"idempotent rundown";
    let path = temp_file_with(content, "idempotent");
    let recorder = Recorder::new();
    let seen = Arc::clone(&recorder);

    let tp = ThreadpoolIo::new(
        read_endpoint(&path),
        move |completion: &IoCompletion| {
            // SAFETY: only Operation<()> is submitted below, claimed once.
            let _operation = unsafe { completion.claim::<()>() };
            seen.push(Record {
                identity: completion.overlapped_ptr() as usize,
                io_result: completion.io_result(),
                bytes: completion.bytes_transferred(),
                payload: 0,
            });
        },
        None,
    )
    .expect("create TP_IO");

    let mut buffer = [0_u8; 64];
    let buf_ptr = buffer.as_mut_ptr();
    let buf_len = buffer.len() as u32;

    let mut operation = Operation::new(());
    operation.set_offset(0);
    // SAFETY: one overlapped ReadFile into `buffer`, which outlives the wait.
    let submitted = unsafe {
        tp.submit(operation, |handle, ov| {
            issue_read(handle, ov, buf_ptr, buf_len)
        })
    };
    assert!(matches!(submitted, Submitted::Pending(_)));

    recorder.wait_for(1);

    for _ in 0..5 {
        tp.run_down();
        tp.wait();
        assert_eq!(tp.outstanding(), 0);
    }
    assert_eq!(recorder.len(), 1, "rundown must not replay completions");

    drop(tp);
    let _ = std::fs::remove_file(&path);
}