thing-tui: Drop restores first, then frees the one-guard slot
All checks were successful
Portable Eve contracts and browser lowering / ubuntu-latest eve-browser-lowering (pull_request) Successful in 2m24s
Portable Eve contracts and browser lowering / ubuntu-latest eve-contracts (pull_request) Successful in 43s

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 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-29 02:44:58 +01:00
commit f25ac958c1
2 changed files with 49 additions and 1 deletions

View file

@ -175,8 +175,10 @@ impl<W: Write + Send + 'static> Terminal<W> {
impl<W: Write + Send + 'static> Drop for Terminal<W> {
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;
}
}

View file

@ -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<std::sync::atomic::AtomicBool>,
inside: Arc<std::sync::Barrier>,
release: Arc<std::sync::Barrier>,
}
impl Write for Gated {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
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"));
}