From f25ac958c1e10dc59c10775889fdfd8428c165d6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 29 Nov 2026 02:44:58 +0100 Subject: [PATCH] thing-tui: Drop restores first, then frees the one-guard slot An enter racing a drop is refused until the terminal is given back. The race is forced with a barrier inside the old guard's restore write, not a timing assumption. Per thing2:ruling:tui-panic-restore-rule and thing2:finding:cut-tui-host.s3.drop-unregisters-before-restoring. Co-Authored-By: Claude Opus 5.5 --- crates/thing-tui/src/terminal.rs | 4 +- crates/thing-tui/src/terminal/guard_tests.rs | 46 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/crates/thing-tui/src/terminal.rs b/crates/thing-tui/src/terminal.rs index 01f882c..9be10bc 100644 --- a/crates/thing-tui/src/terminal.rs +++ b/crates/thing-tui/src/terminal.rs @@ -175,8 +175,10 @@ impl Terminal { impl Drop for Terminal { fn drop(&mut self) { - *active() = None; + // Restore first, then free the slot: an enter that races this drop is refused until the + // terminal is given back, so the restore never runs over a new guard. self.shared.restore(); + *active() = None; } } diff --git a/crates/thing-tui/src/terminal/guard_tests.rs b/crates/thing-tui/src/terminal/guard_tests.rs index e4093c7..8363e41 100644 --- a/crates/thing-tui/src/terminal/guard_tests.rs +++ b/crates/thing-tui/src/terminal/guard_tests.rs @@ -390,3 +390,49 @@ fn a_second_guard_is_refused_while_one_is_live() { drop(Terminal::start(second_out.clone(), false).expect("a guard after the last one dropped")); assert_eq!(count(&second_out.bytes(), LEAVE), 1); } + +/// A writer that, once armed, stops inside its next write until the test lets it go: the drop is held +/// mid-restore at a point the test chooses, with no sleep and no timing assumption. +struct Gated { + out: Shared, + armed: Arc, + inside: Arc, + release: Arc, +} + +impl Write for Gated { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.armed.swap(false, std::sync::atomic::Ordering::SeqCst) { + self.inside.wait(); + self.release.wait(); + } + self.out.write(buf) + } + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +#[test] +fn an_enter_racing_a_drop_is_refused_until_the_restore_is_done() { + let _one = one_at_a_time(); + spy(); + let out = Shared::default(); + let armed = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let inside = Arc::new(std::sync::Barrier::new(2)); + let release = Arc::new(std::sync::Barrier::new(2)); + let gated = Gated { out: out.clone(), armed: armed.clone(), inside: inside.clone(), release: release.clone() }; + let terminal = Terminal::start(gated, false).unwrap(); + armed.store(true, std::sync::atomic::Ordering::SeqCst); + let dropping = std::thread::spawn(move || drop(terminal)); + // The drop is now inside its restore, holding the terminal. An enter on this thread must not get it. + inside.wait(); + let racing = Terminal::start(Shared::default(), false); + let refused = racing.as_ref().err().map(io::Error::kind); + release.wait(); + dropping.join().unwrap(); + drop(racing); + assert_eq!(refused, Some(io::ErrorKind::AlreadyExists), "a guard entered while the old one was still giving the terminal back"); + assert_eq!(count(&out.bytes(), LEAVE), 1); + drop(Terminal::start(Shared::default(), false).expect("the slot is free once the restore is done")); +}