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
#[cfg(test)]
#[cfg(all(unix, feature = "sandbox-microvm"))]
mod tests {
use super::super::common::*;
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::time::Duration;
fn reset_reader_and_drain(primary: &mut std::fs::File) {
crate::ui::terminal::EVENT_READER_SHUTDOWN
.store(true, std::sync::atomic::Ordering::Relaxed);
crate::ui::terminal::join_reader(Duration::from_millis(100));
crate::ui::terminal::EVENT_READER_SHUTDOWN
.store(false, std::sync::atomic::Ordering::Relaxed);
crate::ui::terminal::EVENT_READER_EXITED.store(false, std::sync::atomic::Ordering::Relaxed);
drain_fd_nonblock(primary);
}
#[test]
fn crossterm_input_reader_suite() {
let _guard = serial_fd_test();
use std::collections::VecDeque;
use std::io::Write;
use std::os::unix::io::OwnedFd;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;
// ── save original stdin ──
let saved_stdin = unsafe { OwnedFd::from_raw_fd(libc::dup(0)) };
assert!(saved_stdin.as_raw_fd() >= 0, "dup(0) failed");
let saved_termios: Option<libc::termios> = unsafe {
let mut t: libc::termios = std::mem::zeroed();
if libc::tcgetattr(0, &mut t) >= 0 {
Some(t)
} else {
None
}
};
// ── reset reader flags ──
crate::ui::terminal::EVENT_READER_SHUTDOWN.store(false, Ordering::Relaxed);
crate::ui::terminal::EVENT_READER_EXITED.store(false, Ordering::Relaxed);
// ── PTY as fake stdin (one pair for all phases) ──
let (mut primary, secondary) = open_pty_pair().expect("open PTY pair");
assert!(unsafe { libc::dup2(secondary.as_raw_fd(), 0) } >= 0);
make_raw_fd(0).expect("make_raw on fd 0");
set_nonblocking(&primary).expect("nonblocking on primary");
// ═══════════════════════════════════════════════════════════
// Phase 3.1: Sentinel-before-reader
// ═══════════════════════════════════════════════════════════
{
let mut primary_clone = primary.try_clone().expect("clone primary");
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(5));
let _ = primary_clone.write_all(b"\x1b[0n");
let _ = primary_clone.flush();
});
let mut null = std::fs::OpenOptions::new()
.write(true)
.open("/dev/null")
.unwrap();
crate::ui::terminal::sync_and_drain_via_sentinel(&mut null, Duration::from_millis(100));
drain_fd_nonblock(&mut primary);
let (tokio_tx, mut tokio_rx) =
tokio::sync::mpsc::unbounded_channel::<crate::event::UserEvent>();
crate::ui::input_reader::spawn_input_reader(tokio_tx);
let keystrokes: &[u8] = b"the quick brown fox";
for &b in keystrokes {
primary.write_all(&[b]).unwrap();
primary.flush().unwrap();
std::thread::sleep(Duration::from_millis(1));
}
let mut key_events = 0usize;
let deadline = Instant::now() + Duration::from_millis(500);
loop {
match tokio_rx.try_recv() {
Ok(crate::event::UserEvent::Key(_)) => key_events += 1,
Ok(_) => {}
Err(tokio::sync::mpsc::error::TryRecvError::Empty) => {
if key_events >= keystrokes.len() || Instant::now() >= deadline {
break;
}
std::thread::sleep(Duration::from_millis(1));
}
Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => break,
}
}
assert_eq!(
key_events,
keystrokes.len(),
"3.1 sentinel-before-reader: expected {} key events, got {}",
keystrokes.len(),
key_events
);
reset_reader_and_drain(&mut primary);
}
// ═══════════════════════════════════════════════════════════
// Phase 3.2: Shutdown race
// ═══════════════════════════════════════════════════════════
{
let (tokio_tx, mut tokio_rx) =
tokio::sync::mpsc::unbounded_channel::<crate::event::UserEvent>();
crate::ui::input_reader::spawn_input_reader(tokio_tx);
let event_count = Arc::new(AtomicUsize::new(0));
let event_count2 = Arc::clone(&event_count);
let bridge_done = Arc::new(AtomicBool::new(false));
let bridge_done2 = Arc::clone(&bridge_done);
let bridge = std::thread::spawn(move || {
loop {
match tokio_rx.try_recv() {
Ok(_) => {
event_count2.fetch_add(1, Ordering::Relaxed);
}
Err(tokio::sync::mpsc::error::TryRecvError::Empty) => {
if bridge_done2.load(Ordering::Relaxed) {
break;
}
std::thread::yield_now();
}
Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => break,
}
}
});
let injector_running = Arc::new(AtomicBool::new(true));
let injector_running2 = Arc::clone(&injector_running);
let injector_bytes = Arc::new(AtomicUsize::new(0));
let injector_bytes2 = Arc::clone(&injector_bytes);
let shutdown_active = Arc::new(AtomicBool::new(false));
let shutdown_active2 = Arc::clone(&shutdown_active);
let injector_paused = Arc::new(AtomicBool::new(false));
let injector_paused2 = Arc::clone(&injector_paused);
let charset: Vec<u8> = (b'a'..=b'z')
.chain(b'A'..=b'Z')
.chain(b'0'..=b'9')
.collect();
let mut pri = primary.try_clone().expect("clone primary");
let injector = std::thread::spawn(move || {
for i in 0u64.. {
if !injector_running2.load(Ordering::Relaxed) {
break;
}
if injector_paused2.load(Ordering::Relaxed) {
std::thread::sleep(Duration::from_millis(1));
continue;
}
let b = charset[i as usize % charset.len()];
let _ = pri.write_all(&[b]);
let _ = pri.flush();
if shutdown_active2.load(Ordering::Relaxed) {
injector_bytes2.fetch_add(1, Ordering::Relaxed);
}
std::thread::sleep(Duration::from_micros(1000));
}
});
std::thread::sleep(Duration::from_millis(100));
// Pause the flood and let the bridge catch up before sampling the
// baseline. `event_count` is incremented by the BRIDGE thread,
// which trails the reader under load — sampling while it lags
// credits pre-shutdown events to the post-shutdown window and
// inflates `new_events`. That is a measurement artifact, not a
// reader that ignored the flag: on a loaded machine this reported
// 8 "new" events from a 2-byte shutdown window. With the flood
// paused the bridge drains to a fixed point, so the baseline is
// exact.
injector_paused.store(true, Ordering::Relaxed);
let settle_deadline = Instant::now() + Duration::from_secs(2);
let mut last_seen = event_count.load(Ordering::Relaxed);
loop {
std::thread::sleep(Duration::from_millis(10));
let current = event_count.load(Ordering::Relaxed);
if current == last_seen || Instant::now() >= settle_deadline {
break;
}
last_seen = current;
}
let events_before = event_count.load(Ordering::Relaxed);
// Resume so bytes are genuinely in flight as shutdown lands —
// that in-flight byte is the race this section exists to check.
injector_paused.store(false, Ordering::Relaxed);
shutdown_active.store(true, Ordering::Relaxed);
crate::ui::terminal::EVENT_READER_SHUTDOWN.store(true, Ordering::Relaxed);
crate::ui::terminal::join_reader(Duration::from_millis(50));
// Quiesce the injector BEFORE the sentinel. The shutdown-race
// window this section measures is already closed — the flood ran
// continuously through `EVENT_READER_SHUTDOWN` and the join above,
// which is what puts bytes in flight at the moment of shutdown —
// so stopping here costs the test nothing.
//
// Leaving it running made the drain below chase a live producer:
// the sentinel lands behind whatever the injector has already
// queued, and at ~1 byte/ms the drain consumes at roughly the
// production rate without ever clearing that backlog. On loaded
// CI the deadline expired with the sentinel still buffered
// (observed: drained=187, shutdown_window_bytes=191, no sentinel).
// With the producer stopped the backlog is finite, so the drain
// reaches the sentinel in bounded time.
injector_running.store(false, Ordering::Relaxed);
let _ = injector.join();
// The reader is joined and the flood is stopped, so nothing will
// consume this. Write a DETERMINISTIC sentinel (a byte outside
// the injector's alnum charset) and drain until it appears — the
// earlier version drained whatever happened to be buffered at
// this instant, which came back empty intermittently.
const SENTINEL: u8 = b'~';
{
let mut sentinel_pri = primary.try_clone().expect("clone primary for sentinel");
let _ = sentinel_pri.write_all(&[SENTINEL; 4]);
let _ = sentinel_pri.flush();
}
let mut drained: Vec<u8> = Vec::new();
// Generous, but the loop exits the moment the sentinel appears —
// this only bounds the pathological case. With the producer
// stopped the backlog is finite, so a slow runner needs headroom
// to chew through it, not a tighter deadline.
let drain_deadline = Instant::now() + Duration::from_secs(2);
while Instant::now() < drain_deadline {
drained.extend_from_slice(&crate::ui::terminal::drain_stdin_nonblocking());
if drained.contains(&SENTINEL) {
break;
}
std::thread::sleep(Duration::from_millis(2));
}
let mut prev_count = event_count.load(Ordering::Relaxed);
let stabilization_deadline = Instant::now() + Duration::from_millis(200);
loop {
std::thread::sleep(Duration::from_millis(2));
let current = event_count.load(Ordering::Relaxed);
if current == prev_count {
break;
}
prev_count = current;
if Instant::now() >= stabilization_deadline {
break;
}
}
let events_after = event_count.load(Ordering::Relaxed);
bridge_done.store(true, Ordering::Relaxed);
let _ = bridge.join();
let new_events = events_after.saturating_sub(events_before);
let shutdown_bytes = injector_bytes.load(Ordering::Relaxed);
eprintln!(
"shutdown_race: events_before={} new={} drained={} shutdown_window_bytes={}",
events_before,
new_events,
drained.len(),
shutdown_bytes,
);
// The injector floods bytes at ~1/ms right up to shutdown, so
// at the instant EVENT_READER_SHUTDOWN is set there is almost
// always a byte already mid-read; the reader emits that one
// final event before its loop observes the flag. "Exactly 0"
// was therefore flaky (new=1 intermittently). The real
// invariant is that shutdown stops the flood PROMPTLY — a
// bounded handful, not the dozens that would arrive if the
// reader ignored the flag entirely.
const MAX_SHUTDOWN_RACE_EVENTS: usize = 3;
assert!(
new_events <= MAX_SHUTDOWN_RACE_EVENTS,
"3.2 shutdown race: {} events arrived during shutdown (max {}; drained={}, shutdown_bytes={})",
new_events,
MAX_SHUTDOWN_RACE_EVENTS,
drained.len(),
shutdown_bytes,
);
assert!(
drained.contains(&SENTINEL),
"3.2: drain buffer never captured the post-shutdown sentinel (len={})",
drained.len(),
);
reset_reader_and_drain(&mut primary);
}
// ═══════════════════════════════════════════════════════════
// Phase 3.3: Poll latency (3 rates)
// ═══════════════════════════════════════════════════════════
{
let rates: [(&str, usize, u64); 3] = [
("poll_latency_baseline", 50, 100_000),
("poll_latency_moderate", 200, 10_000),
("poll_latency_stress", 500, 2_000),
];
for (name, count, gap_us) in &rates {
let count = *count;
let gap_us = *gap_us;
let (tokio_tx, mut tokio_rx) =
tokio::sync::mpsc::unbounded_channel::<crate::event::UserEvent>();
crate::ui::input_reader::spawn_input_reader(tokio_tx);
let latencies: Arc<Mutex<Vec<u128>>> =
Arc::new(Mutex::new(Vec::with_capacity(count)));
let timestamps: Arc<Mutex<VecDeque<Instant>>> =
Arc::new(Mutex::new(VecDeque::with_capacity(count)));
let timestamps2 = Arc::clone(×tamps);
let latencies2 = Arc::clone(&latencies);
let injector_done = Arc::new(AtomicBool::new(false));
let injector_done2 = Arc::clone(&injector_done);
let charset: Vec<u8> = (b'a'..=b'z')
.chain(b'A'..=b'Z')
.chain(b'0'..=b'9')
.collect();
let mut pri = primary.try_clone().expect("clone primary");
let injector = std::thread::spawn(move || {
for i in 0..count {
let b = charset[i % charset.len()];
{
let mut ts = timestamps2.lock().unwrap();
ts.push_back(Instant::now());
}
let _ = pri.write_all(&[b]);
let _ = pri.flush();
if gap_us > 0 {
std::thread::sleep(Duration::from_micros(gap_us));
}
}
injector_done2.store(true, Ordering::Relaxed);
});
let collector_done = Arc::new(AtomicBool::new(false));
let collector_done2 = Arc::clone(&collector_done);
let collector = std::thread::spawn(move || {
let mut received = 0usize;
loop {
match tokio_rx.try_recv() {
Ok(_) => {
let now = Instant::now();
let ts_opt = timestamps.lock().unwrap().pop_front();
if let Some(ts) = ts_opt {
latencies2
.lock()
.unwrap()
.push(now.duration_since(ts).as_micros());
}
received += 1;
}
Err(tokio::sync::mpsc::error::TryRecvError::Empty) => {
if injector_done.load(Ordering::Relaxed) && received >= count {
break;
}
std::thread::yield_now();
}
Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => break,
}
}
collector_done2.store(true, Ordering::Relaxed);
});
let deadline = Instant::now() + Duration::from_secs(10);
while !collector_done.load(Ordering::Relaxed) {
if Instant::now() >= deadline {
eprintln!("{name}: collector timed out");
break;
}
std::thread::sleep(Duration::from_millis(5));
}
let _ = injector.join();
let _ = collector.join();
reset_reader_and_drain(&mut primary);
let lats = latencies.lock().unwrap();
if lats.is_empty() {
eprintln!("{name}: NO LATENCY DATA");
continue;
}
let min = lats.iter().min().unwrap();
let max = lats.iter().max().unwrap();
let avg: f64 = lats.iter().sum::<u128>() as f64 / lats.len() as f64;
let mut sorted = lats.clone();
sorted.sort_unstable();
let median = sorted[sorted.len() / 2];
let p99 = sorted[(sorted.len() * 99 / 100).min(sorted.len() - 1)];
eprintln!(
"{name}: n={} min={min}us max={max}us avg={avg:.0}us median={median}us p99={p99}us",
lats.len(),
);
}
}
// ── cleanup: restore stdin ──
let saved_fd = saved_stdin.as_raw_fd();
unsafe {
libc::dup2(saved_fd, 0);
if let Some(ref t) = saved_termios {
libc::tcsetattr(0, libc::TCSANOW, t);
}
}
crate::ui::terminal::EVENT_READER_SHUTDOWN.store(false, Ordering::Relaxed);
crate::ui::terminal::EVENT_READER_EXITED.store(false, Ordering::Relaxed);
}
}