Repository navigation
Fix crash when switching light/dark mode repeatedly - #350
Merged
Merged
Conversation
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.
andples
force-pushed
the
fix/restart-crash
branch
from
November 20, 2026 02:03
4e24048 to
6389c94
Compare
Owner
|
Thanks for the PR, will do a new release next week |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_ACCESSinfacebook::jsi::Value::~Value(). Two thread variants show up in crash reports:com.apple.root.user-initiated-qos:_Block_release→jsi::Value::~Value()(thegetApplicationsscan'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
onColorSchemeChangecallsRNRestart.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'sresolve/reject(std::shared_ptr<jsi::Value>) and ajsi::Runtime &. The new runtime callsgetApplicationsstraight away. If the next restart lands before that work finishes:jsi::Valueon a GCD queue after its runtime is gone, orsol::invoker, which by then points at the new runtime, so the old runtime's values (and a danglingrt) 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.makePromisekeepsresolve/rejectinside the runtime that made them (aMaponglobal) and gives native code aDeferred: a key plus that runtime'sCallInvoker. 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.KeptFunctiondoes the same for the folder watcher's callback.invokeris removed so nothing can post to the wrong runtime.onColorSchemeChangecoalesces 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]underSchedulerDelegateProxy::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 installfinds 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,installcould pick up the wrong runtime. I haven't reproduced that, so it is left for a separate change (self.bridgewould avoid it).Tested
Release build on macOS 27, with the same stress test (3 rounds × 20 appearance flips, 100 ms apart):
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.