Repository navigation
fix(macos): invalidate the display link only after the window close - #644
Conversation
…inishes QuartzCore can walk an invalidated but unreleased link while a window orders out, so a close only pauses and detaches it and teardown invalidates it.
invalidate() races QuartzCore's display-change callback; it is not a walk of an invalidated link. Adds the lifecycle rule to crates/AGENTS.md.
|
Lead gate on 1760a87, the code head; later commits are comments and docs only:
Root |
There was a problem hiding this comment.
Code Review
This pull request addresses a race condition (SIGSEGV, #533) where invalidating a display link during a window close raced with QuartzCore's display-change callback. The fix introduces a retire_display_link step that pauses and detaches the link during the close, deferring the actual invalidate() call to owner teardown. Additionally, new validation metrics (early_link_invalidations and callback_observations) and corresponding test assertions have been added to ensure display links are not invalidated prematurely. There are no review comments, so no further feedback is provided.
|
Independent review handled.
Main (with #643) is merged in, and the lead gate passes on 3d19e64. |
* ci: require the native_wake and native_process runs again Both pass on main after #641 and #644, so they move back into the required metal-validation job and the known-flaky job and script are deleted. * docs: correct the .ips claim, keep crash reports, start M1 Hosted runners do write .ips reports; metal-validation now uploads them on failure. M1 is active with its four slices in next: (owner approved). * docs: say the 2.1 cold path, not cold start
Context
#533: native_wake crashes with SIGSEGV on hosted runners, about 1 in 800 runs: 3 of 2,400 in the same-VM A/B (run 37015643917), at the same rate on main and with M0e.1. Core dumps from that run gave the first stacks. All three show:
+[CADisplayLink(CADisplayLinkInternal) notifyDisplayChange:] + 152faulting at address 0x20 on a dispatch worker thread (invoke_display_change_callback);-[NSWindow close], after our windowWillClose handler had invalidated the CAMetalDisplayLink.Root cause
From disassembling QuartzCore on macOS 26.6.2 (done in the independent review):
notifyDisplayChange:+152loads the layer pointer of aCA::Display::MetalLinkItem, then itsCAMetalLayer._privivar at offset 0x20.ImageQueueDidChangeDisplaymessage to a block that-[CAMetalLayer init]installs. The block finds our item inMetalLinkItem::_listand reads it without a lock.MetalLinkItem::invalidate()both nulls the layer pointer and removes the item from that list.So the crash is a true race: an
invalidate()on the main thread overlaps a callback that has already passed its list lookup. Main invalidated inside windowWillClose, at the start of a close and shortly after the window was shown. Only shown layers get these messages, which fits: the revoked surface, never shown, never crashed.This PR moves the single
invalidate()away from the close, to owner teardown. It narrows the race; it does not make QuartzCore's callback safe. A late callback at teardown could still overlap, and the A/B measures whether it does.Change
retire_display_linkpauses the link and clears its delegate, but does not invalidate it. Later directives are ignored, so a retired link never resumes.invalidate()happens at owner teardown (NativeSurfaceDrop and the builder's failure path), after the window close has returned and just before the fields release the link.early_link_invalidations, counts invalidations made before teardown marks the close finished. Tests assert:Evidence
link_invalidations: 1, early_link_invalidations: 1). The silent-close child and native_initialization failed the same way.Not covered
close()returns. QuartzCore's display-change callback is asynchronous, so the race may survive on that path. The user-close and quit paths close the window before Drop.early_link_invalidationscounts invalidations made outside teardown; it is a regression guard and fails on base.invalidate().Risk and scope
This changes the product close path in native.rs (+83/-19, including the probe). No API changes, no new dependencies, and no relaxed invariant: one invalidation per surface, and no callbacks after close. crates/AGENTS.md gains the lifecycle rule, and the #533 known defect is updated from the A/B result.
Test plan
🤖 Generated with Claude Code