conpty-oxide 0.1.2

Correctness-first Windows ConPTY (pseudoconsole) library with sync and async (tokio) 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
// SPDX-FileCopyrightText: 2025 conpty-oxide contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::*;

use std::sync::{mpsc, Mutex as TestMutex, OnceLock};
use std::time::Duration;

use crate::backend::ConPtyBackend;
use crate::core::pipes::{create_sync_pipes, SyncPipes};

/// Runs `f` on a helper thread and fails the test if it does not finish
/// within five seconds.
///
/// A watchdog is essential here: the failure mode under test is a hang
/// (e.g. `ClosePseudoConsole` blocking), which would otherwise stall the
/// whole test run forever instead of failing one test. The helper thread
/// signals completion over a channel; on timeout the *test* thread
/// panics, and the wedged helper is abandoned (the test harness can still
/// finish and report).
fn complete_within_5s(name: &str, f: impl FnOnce() + Send + 'static) {
    let (done_tx, done_rx) = mpsc::channel();
    thread::Builder::new()
        .name(format!("watchdog-subject-{name}"))
        .spawn(move || {
            f();
            let _ = done_tx.send(());
        })
        .expect("spawning the test subject thread must succeed");
    done_rx
        .recv_timeout(Duration::from_secs(5))
        .unwrap_or_else(|_| panic!("`{name}` hung for more than 5 seconds"));
}

fn backend() -> ConPtyBackend {
    ConPtyBackend::system().expect("ConPTY must be available on a test machine")
}

static CLOSE_OBSERVER: OnceLock<TestMutex<Option<mpsc::Sender<HPCON>>>> = OnceLock::new();

unsafe extern "system" fn observe_close(hpcon: HPCON) {
    let observer = CLOSE_OBSERVER.get_or_init(|| TestMutex::new(None));
    if let Some(sender) = observer
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .as_ref()
    {
        let _ = sender.send(hpcon);
    }
}

/// Creates a console plus the two user-side pipe ends that remain ours.
fn console_and_user_ends(backend: ConPtyBackend) -> (PseudoConsole, OwnedHandle, OwnedHandle) {
    let SyncPipes {
        conout_read,
        conout_write,
        conin_read,
        conin_write,
    } = create_sync_pipes().expect("creating pipes must succeed");
    let console = PseudoConsole::new(backend, Size::default(), conin_read, conout_write, false)
        .expect("CreatePseudoConsole must succeed");
    (console, conout_read, conin_write)
}

#[test]
fn shared_core_is_send_and_sync() {
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<ConsoleShared>();
    assert_send_sync::<PseudoConsole>();
}

#[test]
fn create_yields_a_live_console() {
    let (console, _conout_read, _conin_write) = console_and_user_ends(backend());
    assert_ne!(console.hpcon(), 0);
    assert!(!console.is_released());
    assert!(!console.shared().is_closed());
}

#[test]
fn spawn_capability_exists_only_while_the_hpcon_is_live() {
    let (console, conout_read, conin_write) = console_and_user_ends(backend());
    {
        let capability = console
            .spawn_capability()
            .expect("an open pseudoconsole must be spawnable");
        assert_eq!(
            windows_spawn::AsPseudoConsole::raw_pseudoconsole(&capability),
            console.hpcon()
        );
    }

    drop(conout_read);
    console.shared().notify_reader_closed();
    console.shared().request_close();
    let error = console
        .spawn_capability()
        .expect_err("a closed pseudoconsole must not yield a spawn capability");
    assert_eq!(error.kind(), io::ErrorKind::NotConnected);
    drop(conin_write);
}

#[test]
fn resize_succeeds_while_open_and_fails_after_close() {
    let (console, conout_read, conin_write) = console_and_user_ends(backend());
    console
        .resize(crate::size::test_size(50, 132))
        .expect("resize on a live console must succeed");

    // Retire the reader, then close.
    drop(conout_read);
    console.shared().notify_reader_closed();
    console.shared().request_close();
    assert!(console.shared().is_closed());

    let err = console
        .resize(crate::size::test_size(30, 100))
        .expect_err("resize after close must fail");
    assert_eq!(err.kind(), io::ErrorKind::NotConnected);
    drop(conin_write);
}

/// Whether `clear` works at all is a property of the backend, but the
/// *state machine* around it must behave the same everywhere: refuse once
/// the pseudoconsole has been closed, exactly as `resize` does.
#[test]
fn clear_matches_the_backend_capability_and_fails_after_close() {
    let backend = backend();
    let supported = backend.supports_clear();
    let (console, conout_read, conin_write) = console_and_user_ends(backend);

    match console.clear() {
        Ok(()) => assert!(supported, "clear succeeded without a clear export"),
        Err(err) if err.kind() == io::ErrorKind::Unsupported => {
            assert!(!supported, "clear refused although the export is present");
        },
        Err(err) => panic!("unexpected error: {err:?}"),
    }

    // Retire the reader, then close.
    drop(conout_read);
    console.shared().notify_reader_closed();
    console.shared().request_close();
    assert!(console.shared().is_closed());

    let err = console.clear().expect_err("clear after close must fail");
    assert_eq!(err.kind(), io::ErrorKind::NotConnected);
    drop(conin_write);
}

#[test]
fn drop_console_before_user_pipe_ends() {
    complete_within_5s("drop_console_before_user_pipe_ends", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend());
        let mode = Arc::new(AtomicU8::new(0));
        console.shared().observe_drop_mode(Arc::clone(&mode));
        drop(console);
        assert_eq!(
            mode.load(Ordering::SeqCst),
            1,
            "an open legacy reader requires the detached close path"
        );
        drop(conout_read);
        drop(conin_write);
    });
}

#[test]
fn final_defense_detaches_legacy_close_until_reader_eof() {
    for reader in [ReaderState::Open, ReaderState::Drained, ReaderState::Closed] {
        let legacy = backend().without_release();
        let (console, conout_read, conin_write) = console_and_user_ends(legacy);
        let mode = Arc::new(AtomicU8::new(0));
        console.shared().observe_drop_mode(Arc::clone(&mode));
        console.shared().lock().reader = reader;

        drop(console);
        let expected = if reader == ReaderState::Drained { 2 } else { 1 };
        assert_eq!(mode.load(Ordering::SeqCst), expected, "reader={reader:?}");
        drop(conout_read);
        drop(conin_write);
    }
}

#[test]
fn detached_close_worker_invokes_the_backend() {
    let callback_registry = CLOSE_OBSERVER.get_or_init(|| TestMutex::new(None));
    let (closed_tx, closed_rx) = mpsc::channel();
    {
        let mut slot = callback_registry
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        assert!(slot.is_none(), "the close observer must not overlap itself");
        *slot = Some(closed_tx);
    }

    let expected: HPCON = 0x5a17;
    spawn_detached_close(
        backend().with_test_close(observe_close),
        expected,
        "conpty-oxide-test-close",
    );
    let received = closed_rx.recv_timeout(Duration::from_secs(5));
    *callback_registry
        .lock()
        .unwrap_or_else(PoisonError::into_inner) = None;

    assert_eq!(
        received.expect("the detached worker must invoke the backend close export"),
        expected
    );
}

#[test]
fn drop_user_pipe_ends_before_console() {
    complete_within_5s("drop_user_pipe_ends_before_console", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend());
        drop(conout_read);
        drop(conin_write);
        drop(console);
    });
}

#[test]
fn drop_after_reader_closed_notification() {
    complete_within_5s("drop_after_reader_closed_notification", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend());
        drop(conout_read);
        console.shared().notify_reader_closed();
        drop(console);
        drop(conin_write);
    });
}

#[test]
fn request_close_after_reader_closed_closes_inline() {
    complete_within_5s("request_close_after_reader_closed_closes_inline", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend());
        let shared = Arc::clone(console.shared());
        drop(conout_read);
        shared.notify_reader_closed();
        shared.request_close();
        assert!(shared.is_closed());
        // A second request is a no-op, not a double close.
        shared.request_close();
        drop(console);
        drop(conin_write);
    });
}

#[test]
fn legacy_request_close_with_live_reader_completes() {
    complete_within_5s("legacy_request_close_with_live_reader_completes", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend());
        let shared = Arc::clone(console.shared());
        // Reader still "Open" and the session was never released: this is
        // the legacy watcher's blocking-capable path. With no client and
        // an un-drained conout it must still complete, because closing is
        // exactly what ends a clientless session.
        shared.request_close();
        assert!(shared.is_closed());
        drop(console);
        drop(conout_read);
        drop(conin_write);
    });
}

#[cfg(any(feature = "blocking", feature = "tokio"))]
#[test]
fn input_close_worker_spawn_failure_keeps_close_retryable() {
    complete_within_5s(
        "input_close_worker_spawn_failure_keeps_close_retryable",
        || {
            let legacy = backend().without_release();
            let (console, conout_read, conin_write) = console_and_user_ends(legacy);
            let shared = Arc::clone(console.shared());

            #[cfg(feature = "tracing")]
            {
                let events = crate::tracing_test_support::count_events(|| {
                    shared.request_close_detached_with_worker_spawn_failure();
                });
                assert_eq!(events, 1, "the injected handoff failure must be observed");
            }
            #[cfg(not(feature = "tracing"))]
            shared.request_close_detached_with_worker_spawn_failure();
            assert!(
                !shared.is_closed(),
                "a failed handoff must not claim the live HPCON"
            );
            console
                .resize(crate::size::test_size(40, 100))
                .expect("the live HPCON must remain usable after the failed handoff");

            // A later request can claim the same HPCON. Retire the raw reader
            // first so the real worker has no output pipe to wait on.
            drop(conout_read);
            shared.notify_reader_closed();
            shared.request_close_detached();
            assert!(shared.is_closed());

            drop(conin_write);
            drop(console);
        },
    );
}

#[cfg(feature = "tracing")]
#[test]
fn close_worker_spawn_failure_is_logged() {
    let events = crate::tracing_test_support::count_events(|| {
        log_close_worker_spawn_failure(
            &io::Error::other("injected close worker failure"),
            "test-close-worker",
            true,
        );
    });
    assert_eq!(events, 1);
}

#[test]
fn notify_eof_executes_a_deferred_close() {
    let backend = backend();
    if !backend.supports_release() {
        // Deferral only happens in released mode; nothing to test here.
        return;
    }
    complete_within_5s("notify_eof_executes_a_deferred_close", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend);
        let shared = Arc::clone(console.shared());
        assert!(shared
            .release_after_spawn()
            .expect("ReleasePseudoConsole must succeed"));
        assert!(console.is_released());

        // With the reader open, a close request in released mode defers.
        shared.request_close();
        assert!(!shared.is_closed());

        // The reader hitting EOF executes the pending close.
        shared.notify_reader_eof();
        assert!(shared.is_closed());
        drop(console);
        drop(conout_read);
        drop(conin_write);
    });
}

#[test]
fn reader_close_executes_a_deferred_close() {
    let backend = std::env::var_os("CONPTY_OXIDE_TEST_DLL_DIR")
        .map(ConPtyBackend::from_dir)
        .transpose()
        .expect("the configured standalone backend must load")
        .unwrap_or_else(backend);
    if !backend.supports_release() {
        return;
    }
    complete_within_5s("reader_close_executes_a_deferred_close", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend);
        let shared = Arc::clone(console.shared());
        assert!(shared
            .release_after_spawn()
            .expect("ReleasePseudoConsole must succeed"));

        shared.request_close();
        assert!(!shared.is_closed());

        drop(conout_read);
        shared.notify_reader_closed();
        assert!(shared.is_closed());
        drop(console);
        drop(conin_write);
    });
}

#[test]
fn release_after_spawn_is_idempotent() {
    let backend = backend();
    if !backend.supports_release() {
        return;
    }
    complete_within_5s("release_after_spawn_is_idempotent", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend);
        assert!(console.release_after_spawn().expect("release must succeed"));
        assert!(console.release_after_spawn().expect("release must succeed"));
        assert!(!console.shared().release_failed());
        drop(conout_read);
        drop(conin_write);
        drop(console);
    });
}

#[test]
fn released_console_drops_cleanly_in_any_order() {
    let backend = backend();
    if !backend.supports_release() {
        return;
    }
    complete_within_5s("released_console_drops_cleanly_in_any_order", || {
        let (console, conout_read, conin_write) = console_and_user_ends(backend);
        assert!(console.release_after_spawn().expect("release must succeed"));
        drop(console);
        drop(conin_write);
        drop(conout_read);
    });
}

#[test]
fn reader_finished_tracks_notifications() {
    let (console, conout_read, conin_write) = console_and_user_ends(backend());
    let shared = Arc::clone(console.shared());
    assert!(!shared.reader_finished());
    // This test injects EOF directly instead of obtaining it from a read.
    // Retire the raw read end first so the resulting close is prompt even on
    // a legacy backend.
    drop(conout_read);
    shared.notify_reader_eof();
    assert!(shared.reader_finished());
    assert!(
        shared.is_closed(),
        "EOF proves the host exited and must retire the remaining HPCON"
    );
    // Drained -> Closed is a valid forward transition.
    shared.notify_reader_closed();
    assert!(shared.reader_finished());
    drop(conin_write);
    drop(console);
}