Skip to content

fix(macos): order windows out on exit to prevent KVO crash#820

Draft
lklimek wants to merge 1 commit intov1.0-devfrom
fix/macos-touchbar-crash
Draft

fix(macos): order windows out on exit to prevent KVO crash#820
lklimek wants to merge 1 commit intov1.0-devfrom
fix/macos-touchbar-crash

Conversation

@lklimek
Copy link
Copy Markdown
Contributor

@lklimek lklimek commented Apr 9, 2026

Summary

  • Fixes a SIGILL crash on macOS during shutdown caused by _NSTouchBarFinderObservation KVO observers on inconsistent objects
  • Calls [window orderOut:nil] on all windows as the first action in on_exit(), giving AppKit a chance to properly tear down display-related observations while windows and views are still alive
  • No-op on non-macOS platforms

Root cause

During shutdown, winit's event handler teardown triggers a display cycle flush (CATransaction commit). This causes _NSTouchBarFinderObservation invalidate to remove KVO observers from objects that are already in an inconsistent state, throwing an NSException that AppKit catches and deliberately crashes on via _crashOnException:.

Fix

By ordering all windows out before returning from on_exit(), AppKit processes the window removal while everything is still alive and consistent — the KVO observers are properly detached during this orderly removal rather than during the chaotic winit teardown.

Test plan

  • Launch on macOS, close the app window — no crash on exit
  • Launch on macOS, verify the app still shuts down cleanly (background tasks stop, no hang)
  • Build on Linux — compiles without warnings (no-op path)

🤖 Co-authored by Claudius the Magnificent AI Agent

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 9, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 59dc93f8-4792-40c2-9583-2e4c276382f7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/macos-touchbar-crash

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@lklimek lklimek linked an issue Apr 9, 2026 that may be closed by this pull request
On macOS, winit's event handler teardown triggers a display cycle flush
that finds _NSTouchBarFinderObservation KVO observers on objects in an
inconsistent state, causing a SIGILL crash via _crashOnException:.

Fix: call [window orderOut:nil] on all windows as the first action in
on_exit(), before returning control to eframe/winit. This lets AppKit
properly tear down its display-related observations while the windows
and their views are still alive.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@lklimek lklimek force-pushed the fix/macos-touchbar-crash branch from ebf5e94 to 2a97da2 Compare April 9, 2026 08:37
@lklimek lklimek changed the title fix(macos): disable TouchBar to prevent KVO crash on shutdown fix(macos): order windows out on exit to prevent KVO crash Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DET crash on closure

1 participant