Skip to content

Fix crash when switching light/dark mode repeatedly - #350

Merged
ospfranco merged 1 commit into
ospfranco:mainfrom
andples:fix/restart-crash
Nov 24, 2026
Merged

ospfranco merged 1 commit into
ospfranco:mainfrom
andples:fix/restart-crash

Conversation

@andples

@andples andples commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #349

Fix crash when switching light/dark mode repeatedly

What happens

Switching macOS between light and dark a few times in a row (e.g. a theme switcher, or toggling quickly) crashes Sol with EXC_BAD_ACCESS in facebook::jsi::Value::~Value(). Two thread variants show up in crash reports:

  • com.apple.root.user-initiated-qos: _Block_release → jsi::Value::~Value() (the getApplications scan's block)
  • com.facebook.react.JavaScript: __CFRunLoopDoBlocks → jsi::Value::~Value()

Repro: open Sol, then run this ~12 times quickly:
osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to not dark mode'

Why

onColorSchemeChange calls RNRestart.restart(), so every appearance change tears down the JS runtime and makes a new one. The JSI bindings start background work that holds the runtime's resolve/reject (std::shared_ptr<jsi::Value>) and a jsi::Runtime &. The new runtime calls getApplications straight away. If the next restart lands before that work finishes:

  • the background block releases the last reference to a jsi::Value on a GCD queue after its runtime is gone, or
  • it posts through the global sol::invoker, which by then points at the new runtime, so the old runtime's values (and a dangling rt) run on the new JS thread.

Both segfault. The same applies to searchFilesIndexed, indexPaths, removeIndexedPath, the calendar calls and the folder watcher.

Fix

  • AsyncJS.h (new, header-only): native code never holds JS values across async work. makePromise keeps resolve/reject inside the runtime that made them (a Map on global) and gives native code a Deferred: a key plus that runtime's CallInvoker. Settling goes through the invoker, so it runs on the right JS thread with the right runtime, or is dropped if the runtime is gone. Dropping is safe because the queued work holds only plain C++/Obj-C data. KeptFunction does the same for the folder watcher's callback.
  • All async bindings use it; the global invoker is removed so nothing can post to the wrong runtime.
  • onColorSchemeChange coalesces restarts: it restarts once the appearance has stayed put for 1 s, and only if it ends up different from what the runtime started with. The restart itself stays (it is the workaround for [BUG] Problem with Theme: Darkmode broken #283). Restarting once per flip could also crash inside React Native: a new reload starting while the previous one was still settling segfaulted in -[RCTScheduler delegate] under SchedulerDelegateProxy::schedulerDidDispatchCommand, on the old runtime's JS thread. Sol is usually hidden when the appearance changes, so the delay isn't visible.
  • CalendarHelper requestCalendarAccess: now calls back when the request finishes, granted or not. Before, a denied request never settled the JS promise; both callers already re-read the authorization status in .then, so a denial now shows up straight away instead of the promise hanging.

Not in this PR

SolNative install finds the runtime through [RCTBridge currentBridge], a process-wide value that points at the newest bridge. If a restart begins while the previous bundle is still starting up, install could pick up the wrong runtime. I haven't reproduced that, so it is left for a separate change (self.bridge would avoid it).

Tested

Release build on macOS 27, with the same stress test (3 rounds × 20 appearance flips, 100 ms apart):

  • stock 2.1.364: crashes in round 1 (same stack as above)
  • this branch: survives all 60 flips, no crash report
  • also with a theme switcher changing appearance and accent together: 3 rounds of 8 switches 300 ms apart, and 30 switches 1–1.6 s apart (a restart between most of them), no crash report

I ran into this while working on a personal theme-switching project. This PR was written with the assistance of Claude Opus 5.5; all code and text have been human reviewed.

Sol restarts its JS runtime on every light/dark change. Native work started
before a restart (getApplications, file search, calendar, folder watcher) held
the old runtime's resolve/reject or callback, plus a `jsi::Runtime &`, and on
completion either freed them on a background queue after the runtime was gone
or ran them on the next runtime through the global `invoker`. Both segfault in
jsi::Value::~Value(); switching appearance a few times in a row reproduces it.

Native code now never holds JS values across async work: AsyncJS.h keeps them
in the runtime that made them (a Map on global) and hands native code a key
plus that runtime's own CallInvoker, which drops the work if the runtime is
gone. The global `invoker` is removed.

The colour-scheme listener now restarts once the appearance has stayed put for
a second, and only if it differs from what the runtime started with: a restart
per flip could also crash inside React Native's Fabric scheduler when a reload
started while the previous one was still settling. A denied calendar request
now settles its promise instead of hanging.
@ospfranco
ospfranco merged commit ad21f1c into ospfranco:main Nov 24, 2026
@ospfranco

Copy link
Copy Markdown
Owner

Thanks for the PR, will do a new release next week

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.

Sol crashes when macOS switches between light and dark mode several times in a row

2 participants