ui-contract: the open-record count is the thread's own

Records are open only around runs of a pass on the pass's own thread, so the
count that lets a registration skip the pass state is a thread-local. As a
process-wide atomic, a record open in another thread's pass made this thread
read its pass state (outside its component tree, an unbound read), which made
the settled-pass service-lookup trial flaky under parallel test threads. A
close with no open record no longer wraps the count.
This commit is contained in:
aiongg committed 2026-11-27 01:09:30 +00:00
1 parent a9adb36c6a
commit fdfcecf201
1 file changed
+19 -12
+19 -12
View File
@@ -24,10 +24,10 @@ use crate::runtime::reads::BindKey;
use crate::runtime::shift::Shift;
use egui::{Context, IdSet, RecordShift, Rect};
use std::any::Any;
use std::cell::Cell;
use std::collections::HashSet;
use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
/// A registration of the recorded run: the call that makes it again and what it is called with. Two entries are equal
/// when they come from the same call (the same `again` of [`journal`]) with equal arguments.
@@ -108,8 +108,17 @@ impl fmt::Debug for Entry {
}
}
/// Records open across all contexts, so a registration made while none is open takes no lock.
static OPEN: AtomicUsize = AtomicUsize::new(0);
thread_local! {
/// The records open on this thread, so a registration made while none is open reads no pass state. Records are
/// open only around runs of a pass, on the pass's own thread: a job's thread (a finished job's wake, a progress
/// report) has none, and never takes the pass state the pass's thread holds.
static OPEN: Cell<usize> = const { Cell::new(0) };
}
/// Whether a record is open on this thread.
fn any_open() -> bool {
OPEN.with(Cell::get) > 0
}
/// The journal of the running pass while a record is open, and the records open around the running code.
#[derive(Default)]
@@ -246,7 +255,7 @@ impl Record {
/// Whether a record is open around the running code.
pub(crate) fn recording(ctx: &Context) -> bool {
OPEN.load(Ordering::Acquire) > 0 && pass_state::with::<Recorder, _>(ctx, |recorder| !recorder.open.is_empty())
any_open() && pass_state::with::<Recorder, _>(ctx, |recorder| !recorder.open.is_empty())
}
/// Opens a record around the real run of a region, which `close` ends. A run inside an open label scope cannot be
@@ -263,7 +272,7 @@ pub(crate) fn open_in_pass(ctx: &Context) {
fn push(ctx: &Context, open: Open) {
pass_state::with::<Recorder, _>(ctx, |recorder| recorder.open.push(Open { from: recorder.journal.len(), ..open }));
OPEN.fetch_add(1, Ordering::AcqRel);
OPEN.with(|open| open.set(open.get() + 1));
}
/// Opens a record around a run of the replay audit, which `close` ends as any other.
@@ -275,13 +284,13 @@ pub(crate) fn open_audit(ctx: &Context) {
/// Whether the running code is inside a run of the replay audit.
#[cfg(feature = "testing")]
pub(crate) fn in_audit_run(ctx: &Context) -> bool {
OPEN.load(Ordering::Acquire) > 0 && pass_state::with::<Recorder, _>(ctx, |recorder| recorder.open.iter().any(|open| open.audit))
any_open() && pass_state::with::<Recorder, _>(ctx, |recorder| recorder.open.iter().any(|open| open.audit))
}
/// Ends the innermost record, the one around a run on `layer`: paint or widgets on a layer that is neither `layer` nor
/// an overlay the run showed block it.
pub(crate) fn close(ctx: &Context, layer: Layer) -> Record {
OPEN.fetch_sub(1, Ordering::AcqRel);
OPEN.with(|open| open.set(open.get().saturating_sub(1)));
let (open, journal) = pass_state::with::<Recorder, _>(ctx, |recorder| {
// A close with no open record ends one that holds nothing and is never replayed.
let open = recorder.open.pop().unwrap_or_else(|| {
@@ -458,10 +467,8 @@ pub(crate) fn replay(ctx: &Context, record: &Record, shift: &RecordShift) {
}
fn with_open(ctx: &Context, f: impl FnOnce(&mut Facts)) {
// Records are open only around runs of a pass on its own thread. A job's thread (a finished job's wake, a progress
// report) runs no pass of `ctx`: it has no record to note into and must not take the pass state that the pass's
// thread holds.
if OPEN.load(Ordering::Acquire) > 0 && frame::running(ctx).is_some() {
// A record open on this thread is one of a running pass: of `ctx`, or of another context this thread runs.
if any_open() && frame::running(ctx).is_some() {
pass_state::with::<Recorder, _>(ctx, |recorder| {
if let Some(open) = recorder.open.last_mut() {
f(&mut open.facts);
@@ -518,7 +525,7 @@ where
/// Notes that the running code read the value `key`.
pub(crate) fn note_read(ctx: &Context, key: BindKey) {
if OPEN.load(Ordering::Acquire) > 0 && frame::running(ctx).is_some() {
if any_open() && frame::running(ctx).is_some() {
pass_state::with::<Recorder, _>(ctx, |recorder| {
if let Some(open) = recorder.open.last_mut()
&& (!open.kept || !matches!(key, BindKey::Place(..)))