Fix Quick Look preview deactivation crash
Fixes #4453.
Repro
Per the user override, I built and launched the tagged dev app locally before fixing:
CMUX_SKIP_ZIG_BUILD=1 ./scripts/reload.sh --tag issue-4453-quicklook-deactivated-crash --launch
I reproduced the crash in that dev app with:
printf '\000\001\002\003cmux quicklook repro\000\004' > /tmp/cmux-ql-repro.issue4453.bin
CMUX_TAG=issue-4453-quicklook-deactivated-crash scripts/cmux-debug-cli.sh open /tmp/cmux-ql-repro.issue4453.bin --workspace workspace:1
Before the fix, the tagged app aborted and wrote ~/Library/Logs/DiagnosticReports/cmux DEV-2026-05-20-172719.ips with the Quick Look assertion:
[QL] -[QLPreviewView setPreviewItem:blockingUntilLoading:timeoutDate:transition:]: item == nil || _reserved->internalState != QLPreviewDeactivatedInternalState
Test Approach
The first commit adds FilePreviewReviewFeedbackTests.testQuickLookSessionCloseDoesNotDeactivateMountedRepresentableView. It creates a Quick Look file preview, calls the session close path while the representable view is still mounted, then updates that same view. On the un-fixed implementation this crashes through the same QLPreviewView.setPreviewItem path; with the fix it leaves the retired view unconfigured.
Additional regression coverage verifies stale SwiftUI dismantle callbacks do not reset the active preview item, and that PanelOwnedNativeViewSession still performs one dismantle callback for a view that was previously retired by close().
Fix
PanelOwnedNativeViewSession now separates three native-view states:
- active owned view: may be configured by SwiftUI updates
- retired view: closed by the model and blocked from future configuration
- dismantled view: already torn down, so duplicate SwiftUI dismantle calls are ignored
FilePreviewQuickLookSession now clears previewItem and removes the view from its superview for both close and dismantle. It deliberately does not call QLPreviewView.close(): that API permanently rejects future preview items and also asserts when Quick Look considers the view inactive. The session retirement guard prevents stale updates from reconfiguring old views without entering Quick Look's deactivated state.
QuickLookPreviewView still routes SwiftUI dismantleNSView into the session so SwiftUI-owned teardown participates in the same lifecycle path.
Verification
- Reproduced the crash before fixing in the tagged dev app.
- Confirmed the first regression test failed pre-fix with
Crash: cmux DEV ... libsystem_c.dylib: abort() called. - Confirmed the stale-dismantle regression failed before the ownership fix, then passed.
- Confirmed the close-then-dismantle regression failed before retired/dismantled state separation, then passed.
- Confirmed the focused Quick Look lifecycle tests pass after the fix.
- Rebuilt with the required command and reran the CLI Quick Look repro;
openreturned OK, the tagged socket stayed reachable, and no newer DiagnosticReports crash was created.
No cloud-mac video was used.
Summary by CodeRabbit
-
Bug Fixes
- Improved cleanup for file preview sessions with explicit teardown to prevent stale native view state.
- Strengthened lifecycle handling to avoid resetting active Quick Look preview items when retired views are removed.
- Made teardown idempotent and prevented retired views from being reconfigured.
-
Tests
- Added tests covering Quick Look preview lifecycle and post-teardown preview-item behavior.
- Added a temporary binary-file helper to support the tests.