Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ milestones:
- {id: M8, name: "real application and parity sweep, phase 5", status: planned}
autonomy: "Deepak: agents may push branches, open PRs, merge, and delete their own merged PR branches, except ask_first"
next:
- "M0e.2 #533: fix the QuartzCore display-change race the A/B stacks show, then a 5,000-run hosted A/B"
- "M0e.3 require known-flaky-native after 10 clean hosted runs plus a 300-iteration native_wake run, then stop"
- "M0e.3 make known-flaky-native required again after 10 clean hosted runs plus a 300-iteration native_wake run, then stop for the M1 kickoff"
ask_first: [dependency changes, destructive actions, milestone design kickoff, new subsystem or public API or relaxed invariant, unsafe boundary or allowlist change, license or copied-source change, weakening a CI gate or threshold, anything that costs money]
---

Expand Down
5 changes: 4 additions & 1 deletion crates/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ scope: framework crates under crates/
parent: ../AGENTS.md
updated: 2026-10-02
known_defects:
- "native_wake can SIGSEGV on hosted runners (was #533): during window close, after our invalidate(), QuartzCore +[CADisplayLink notifyDisplayChange:] faults at 0x20 on a dispatch thread; 3 of 2,400 runs, same rate on main and M0e.1 (same-VM A/B, 2026-10-02)"
- "native_wake SIGSEGV on hosted runners (was #533): QuartzCore's display-change callback races invalidate() during a close; M0e.2 moves invalidate() to owner teardown (same-VM A/B: main 6 of 10,000, fix 0 of 10,000); remove after M0e.3's clean runs"
- "an accessibility omission control in native_process can miss its 10 s frame deadline under hosted Metal validation (was #622); M0e.1 fixes it, remove after 10 clean hosted runs"
known_flaky_ci: "#533 and #622 run in the known-flaky-native job, which ci-pass does not require; fix next, then require it"
---
Expand Down Expand Up @@ -71,6 +71,9 @@ and destroy elements before revoking the handler.
- The release profile is `panic = "abort"`: `catch_unwind` protects only debug
and test builds.
- Never mutate the process environment after threads exist.
- Never invalidate a display link during a window close: QuartzCore's
display-change callback can race `invalidate()` (#533). The close pauses
and detaches the link; owner teardown invalidates it once.
- Metal validation on for correctness runs, off for timing runs.

## Native tests
Expand Down
19 changes: 19 additions & 0 deletions crates/alpine-platform-macos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ pub mod native_validation {
active: [u64; 10],
run_loop_registrations: u64,
link_invalidations: u64,
early_link_invalidations: u64,
delegate_revocations: u64,
window_closes: u64,
pasteboard_releases: u64,
Expand Down Expand Up @@ -1452,6 +1453,7 @@ pub mod native_validation {
active: [u64; 10],
run_loop_registrations: u64,
link_invalidations: u64,
early_link_invalidations: u64,
delegate_revocations: u64,
window_closes: u64,
pasteboard_releases: u64,
Expand All @@ -1463,6 +1465,7 @@ pub mod native_validation {
active,
run_loop_registrations,
link_invalidations,
early_link_invalidations,
delegate_revocations,
window_closes,
pasteboard_releases,
Expand Down Expand Up @@ -1500,6 +1503,13 @@ pub mod native_validation {
self.link_invalidations
}

/// Returns display-link invalidations made outside owner teardown,
/// which runs after the window close.
#[must_use]
pub const fn early_link_invalidations(self) -> u64 {
self.early_link_invalidations
}

/// Returns native delegate revocations performed before release.
#[must_use]
pub const fn delegate_revocations(self) -> u64 {
Expand Down Expand Up @@ -1792,6 +1802,13 @@ pub mod native_validation {
self.observed
}

/// Returns display-link callbacks that reached the delegate's driver
/// path, live or closing.
#[must_use]
pub const fn callback_observations(self) -> u64 {
self.callback_observations
}

/// Returns the last callback's portable-to-native directive.
#[must_use]
pub const fn last_directive(self) -> PauseDirectiveEvidence {
Expand Down Expand Up @@ -2341,6 +2358,7 @@ pub mod native_validation {
[73, 79, 83, 89, 97, 101, 103, 107, 109, 113],
127,
131,
157,
137,
139,
149,
Expand All @@ -2358,6 +2376,7 @@ pub mod native_validation {
);
assert_eq!(evidence.run_loop_registrations(), 127);
assert_eq!(evidence.link_invalidations(), 131);
assert_eq!(evidence.early_link_invalidations(), 157);
assert_eq!(evidence.delegate_revocations(), 137);
assert_eq!(evidence.window_closes(), 139);
assert_eq!(evidence.pasteboard_releases(), 149);
Expand Down
84 changes: 66 additions & 18 deletions crates/alpine-platform-macos/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,10 @@ struct InitializationProbeState {
active: Cell<[u64; NATIVE_OWNER_KINDS]>,
run_loop_registrations: Cell<u64>,
link_invalidations: Cell<u64>,
early_link_invalidations: Cell<u64>,
delegate_revocations: Cell<u64>,
window_closes: Cell<u64>,
window_close_finished: Cell<bool>,
pasteboard_releases: Cell<u64>,
release_order_violations: Cell<u64>,
window: RefCell<Option<Weak<NSWindow>>>,
Expand Down Expand Up @@ -366,6 +368,9 @@ impl InitializationProbe {
self.0
.link_invalidations
.set(self.0.link_invalidations.get() + 1);
if !self.0.window_close_finished.get() {
increment_cell(&self.0.early_link_invalidations);
}
}

fn record_delegate_revocation(&self) {
Expand All @@ -378,6 +383,11 @@ impl InitializationProbe {
self.0.window_closes.set(self.0.window_closes.get() + 1);
}

// Owner teardown calls this once no AppKit close can still be running.
fn record_window_close_finished(&self) {
self.0.window_close_finished.set(true);
}

fn record_window(&self, window: &Retained<NSWindow>) {
self.0.window.replace(Some(Weak::from_retained(window)));
}
Expand All @@ -396,6 +406,7 @@ impl InitializationProbe {
active,
self.0.run_loop_registrations.get(),
self.0.link_invalidations.get(),
self.0.early_link_invalidations.get(),
self.0.delegate_revocations.get(),
self.0.window_closes.get(),
self.0.pasteboard_releases.get(),
Expand Down Expand Up @@ -2917,6 +2928,7 @@ struct DisplayLinkDelegateIvars {
last_display_link_update: Cell<Option<Instant>>,
frame_progress_seen_updates: Cell<u64>,
frame_progress_timer: Cell<Option<FrameProgressTimer>>,
display_link_retired: Cell<bool>,
display_link_invalidated: Cell<bool>,
native_close_finished: Cell<bool>,
#[cfg(alpine_native_validation)]
Expand Down Expand Up @@ -3818,25 +3830,40 @@ impl DisplayLinkDelegate {
if self.ivars().native_close_finished.replace(true) {
return;
}
self.invalidate_display_link();
if let Some(display_link) = &self.ivars().display_link {
display_link.setDelegate(None);
}
self.retire_display_link();
stop_event_loop(&self.ivars().application);
}

fn apply_link_directive(&self, directive: DisplayLinkDirective) {
let Some(display_link) = &self.ivars().display_link else {
return;
};
// A retired link has no delegate and must never resume.
if self.ivars().display_link_retired.get() {
return;
}
match directive {
DisplayLinkDirective::None => {}
DisplayLinkDirective::Resume => display_link.setPaused(false),
DisplayLinkDirective::Pause => display_link.setPaused(true),
DisplayLinkDirective::Invalidate => self.invalidate_display_link(),
DisplayLinkDirective::Invalidate => self.retire_display_link(),
}
}

// Ends callbacks without invalidate(). invalidate() races QuartzCore's
// display-change callback (#533), so only owner teardown, after the close,
// invalidates.
fn retire_display_link(&self) {
if self.ivars().display_link_retired.replace(true) {
return;
}
if let Some(display_link) = &self.ivars().display_link {
display_link.setPaused(true);
display_link.setDelegate(None);
}
}

// Owner teardown only, after the window close and just before release.
// Idempotent, so the validation probe counts only a real invalidate().
fn invalidate_display_link(&self) {
if self.ivars().display_link_invalidated.replace(true) {
Expand Down Expand Up @@ -4703,6 +4730,7 @@ impl NativeSurface {
last_display_link_update: Cell::new(None),
frame_progress_seen_updates: Cell::new(0),
frame_progress_timer: Cell::new(None),
display_link_retired: Cell::new(false),
display_link_invalidated: Cell::new(false),
native_close_finished: Cell::new(false),
#[cfg(alpine_native_validation)]
Expand Down Expand Up @@ -6141,20 +6169,19 @@ impl Drop for NativeSurface {
if self.lifecycle.load(Ordering::Acquire) == SURFACE_LIVE {
begin_close_observer_state(&self.lifecycle);
}
// A close whose drain never finished still owns a live link. This is a
// no-op after a finished close, so every path invalidates exactly once.
self.delegate.invalidate_display_link();
// Pause and detach the link if no close did. It stays valid until
// the window close below has returned.
self.delegate.retire_display_link();
#[cfg(alpine_native_validation)]
if let Some(probe) = &self.validation_probe {
probe.record_delegate_revocation();
}
// A reentrant windowWillClose callback may be unable to borrow the
// driver while it still revokes callback admission. Owner teardown is
// therefore the final idempotent shutdown boundary in both paths.
if let Ok(mut driver) = self.driver.try_borrow_mut() {
driver.shutdown(&self.counters);
}
self.display_link.setDelegate(None);
#[cfg(alpine_native_validation)]
if let Some(probe) = &self.validation_probe {
probe.record_delegate_revocation();
}
self.window.setDelegate(None);
#[cfg(alpine_native_validation)]
if let Some(pasteboard) = self
Expand All @@ -6174,6 +6201,13 @@ impl Drop for NativeSurface {
probe.record_window_close();
}
}
// Every close path has returned, so invalidate exactly once here,
// just before the fields release the link.
#[cfg(alpine_native_validation)]
if let Some(probe) = &self.validation_probe {
probe.record_window_close_finished();
}
self.delegate.invalidate_display_link();
finish_close_observer_state(&self.lifecycle);
}
}
Expand Down Expand Up @@ -6306,11 +6340,6 @@ impl Drop for NativeSurfaceBuilder {
}
if let Some(display_link) = &self.display_link {
display_link.setPaused(true);
display_link.invalidate();
#[cfg(alpine_native_validation)]
if let Some(probe) = &self.validation_probe {
probe.record_link_invalidation();
}
display_link.setDelegate(None);
#[cfg(alpine_native_validation)]
if let Some(probe) = &self.validation_probe {
Expand All @@ -6326,6 +6355,19 @@ impl Drop for NativeSurfaceBuilder {
probe.record_window_close();
}
}
// As in NativeSurface::drop: invalidate after the close, just before
// the fields release the link.
#[cfg(alpine_native_validation)]
if let Some(probe) = &self.validation_probe {
probe.record_window_close_finished();
}
if let Some(display_link) = &self.display_link {
display_link.invalidate();
#[cfg(alpine_native_validation)]
if let Some(probe) = &self.validation_probe {
probe.record_link_invalidation();
}
}
#[cfg(alpine_native_validation)]
if let Some(delegate) = &self.delegate {
if let Some(pasteboard) = delegate.ivars().validation_pasteboard.borrow().as_ref() {
Expand Down Expand Up @@ -6452,6 +6494,11 @@ pub(crate) fn validate_initialization_rollback() -> Result<(), SurfaceError> {
probe.0.link_invalidations.get(),
u64::from(owner_count > NativeOwnerKind::DisplayLink.index())
);
assert_eq!(
probe.0.early_link_invalidations.get(),
0,
"invalidation before window close after {stage:?}"
);
assert_eq!(
probe.0.delegate_revocations.get(),
u64::from(owner_count > NativeOwnerKind::Delegate.index())
Expand All @@ -6478,6 +6525,7 @@ pub(crate) fn validate_initialization_rollback() -> Result<(), SurfaceError> {
assert_eq!(active, [0; NATIVE_OWNER_KINDS]);
assert_eq!(probe.0.run_loop_registrations.get(), 1);
assert_eq!(probe.0.link_invalidations.get(), 1);
assert_eq!(probe.0.early_link_invalidations.get(), 0);
assert_eq!(probe.0.delegate_revocations.get(), 1);
assert_eq!(probe.0.window_closes.get(), 1);
assert_eq!(probe.0.release_order_violations.get(), 0);
Expand Down
18 changes: 17 additions & 1 deletion crates/alpine-platform-macos/tests/native_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,19 @@ mod validation {
assert_eq!(snapshot.submitted_frame_slots(), 0);
assert!(snapshot.display_link_paused());

// The link ran until the close drained; the close paused and detached
// it, so several display intervals deliver no update to the delegate.
let delivered = native_validation::pause_confirmation_evidence(&surface);
NSRunLoop::mainRunLoop().runUntilDate(&NSDate::dateWithTimeIntervalSinceNow(0.1));
let after_close = native_validation::pause_confirmation_evidence(&surface);
assert_eq!(
after_close.callback_observations(),
delivered.callback_observations(),
"a display-link update reached the delegate after close: {after_close:?}"
);
let owners = native_validation::owner_evidence(&surface).ok_or("closed owner evidence")?;
assert_eq!(owners.link_invalidations(), 0, "{owners:?}");

let admitted = observer.callback_count();
let rejected = observer.rejected_callback_count();
native_validation::inject_late_callback(&surface);
Expand Down Expand Up @@ -470,7 +483,9 @@ mod validation {
timeout.cancel();

assert_eq!(observer.lifecycle(), SurfaceLifecycle::Closing);
assert_eq!(owners.link_invalidations(), 1, "{owners:?}");
// The close pauses and detaches the link; teardown invalidates it.
assert_eq!(owners.link_invalidations(), 0, "{owners:?}");
assert_eq!(owners.early_link_invalidations(), 0, "{owners:?}");
assert_eq!(owners.window_closes(), 1, "{owners:?}");
assert_eq!(progress.timer_close_drains(), 1, "{progress:?}");
assert!(!progress.timer_armed(), "{progress:?}");
Expand Down Expand Up @@ -999,6 +1014,7 @@ mod validation {
assert_eq!(evidence.active(), [0; OWNER_KINDS]);
assert_eq!(evidence.run_loop_registrations(), 1);
assert_eq!(evidence.link_invalidations(), 1);
assert_eq!(evidence.early_link_invalidations(), 0, "{evidence:?}");
assert_eq!(evidence.delegate_revocations(), 1);
assert_eq!(evidence.window_closes(), 1);
assert_eq!(evidence.pasteboard_releases(), 0);
Expand Down
1 change: 1 addition & 0 deletions crates/alpine-platform-macos/tests/native_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ mod validation {
|| evidence.active() != [0; 10]
|| evidence.run_loop_registrations() != 1
|| evidence.link_invalidations() != 1
|| evidence.early_link_invalidations() != 0
|| evidence.delegate_revocations() != 1
|| evidence.window_closes() != 1
|| evidence.pasteboard_releases() != 0
Expand Down
20 changes: 20 additions & 0 deletions crates/alpine-platform-macos/tests/native_wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

// invalidate() can race QuartzCore's display-change callback during a
// close (#533), so the close only pauses and detaches the link.
fn assert_link_kept_through_close(
surface: &alpine_platform_macos::NativeSurface,
) -> Result<(), Box<dyn std::error::Error>> {
let owners = native_validation::owner_evidence(surface).ok_or("close owner evidence")?;
assert_eq!(owners.link_invalidations(), 0, "{owners:?}");
assert!(surface.snapshot().display_link_paused());
Ok(())
}

fn assert_link_invalidated_after_close(owners: native_validation::NativeOwnerEvidence) {
assert_eq!(owners.link_invalidations(), 1, "{owners:?}");
assert_eq!(owners.early_link_invalidations(), 0, "{owners:?}");
}

eprintln!("native_wake phase=revoked_surface.create.begin");
let revoked_descriptor = SurfaceDescriptor::new("Alpine revoked worker wake", 96.0, 64.0, 1.0)?;
let revoked_surface = native_validation::new_surface(&revoked_descriptor)?;
Expand All @@ -64,6 +80,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("native_wake phase=revoked_surface.close.begin");
native_validation::close_window(&revoked_surface);
assert_eq!(revoked_observer.lifecycle(), SurfaceLifecycle::Closing);
assert_link_kept_through_close(&revoked_surface)?;
eprintln!("native_wake phase=revoked_surface.closed drain.begin");
// The timer alone is not evidence that the queued main-queue wake ran.
drain_native_callbacks(&revoked_surface, || {
Expand All @@ -78,6 +95,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("native_wake phase=revoked_surface.drained release.begin");
let revoked_owners = native_validation::close_with_owner_evidence(revoked_surface)?;
assert_eq!(revoked_owners.active(), [0; 10]);
assert_link_invalidated_after_close(revoked_owners);
assert_eq!(revoked_owners.pasteboard_releases(), 0);
assert_eq!(revoked_owners.release_order_violations(), 0);
eprintln!("native_wake phase=revoked_surface.released");
Expand Down Expand Up @@ -148,6 +166,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
assert!(received[1] > received[0]);
drop(received);
assert_eq!(observer.lifecycle(), SurfaceLifecycle::Closing);
assert_link_kept_through_close(&surface)?;
assert_eq!(surface.snapshot().submission_count(), 0);
assert_eq!(
*admissions.lock().map_err(|_| "admissions poisoned")?,
Expand All @@ -172,6 +191,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
let surface = Rc::try_unwrap(surface).map_err(|_| "native wake surface remained retained")?;
let owners = native_validation::close_with_owner_evidence(surface)?;
assert_eq!(owners.active(), [0; 10]);
assert_link_invalidated_after_close(owners);
assert_eq!(owners.pasteboard_releases(), 0);
assert_eq!(owners.release_order_violations(), 0);
eprintln!("native_wake phase=worker_surface.released");
Expand Down
Loading