Android: clear stale callback registrations on hot restart - #22
Closed
Colton127 wants to merge 1 commit into
Closed
Conversation
onDetachedFromEngine does not fire on hot restart. The FlutterEngine and its id are unchanged while the Dart isolate is replaced, so every registered NativeCallable silently goes stale with no notification -- and invoking a callable whose isolate is gone is undefined behaviour. The plugin now registers a FlutterEngine.EngineLifecycleListener and clears the registrations in onPreEngineRestart(). onEngineWillDestroy() clears too; it fires just before the plugin registry is destroyed, while the engine is still valid, so it is a slightly earlier point than the detach hook. The listener is removed on detach. Only the callback bridges are cleared here, not the whole engine: after a hot restart the new isolate's init() finds the native engine still initialized and deinits it itself. Compiled under -Xlint:all against stubs mirroring the real embedding API, and the JNI symbol name diffed against javac -h output. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Jul 25, 2026
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.
Summary
onDetachedFromEnginedoes not fire on hot restart. TheFlutterEngineand its id are unchanged while the Dart isolate is replaced, so every registeredNativeCallablesilently goes stale with no notification — and invoking a callable whose isolate is gone is undefined behaviour.Approach
The plugin registers a
FlutterEngine.EngineLifecycleListenerand clears the registrations inonPreEngineRestart().onEngineWillDestroy()clears too — it fires just before the plugin registry is destroyed, while the engine is still valid, so it is a slightly earlier point than the detach hook. The listener is removed on detach.Only the callback bridges are cleared here, not the whole engine: after a hot restart the new isolate's
init()finds the native engine still initialized and deinits it itself.Verification
Compiled under
-Xlint:allagainst stubs mirroring the real embedding API, and the JNI symbol name diffed againstjavac -houtput rather than checked by eye (theflutter_soloudpackage underscore mangles toflutter_1soloud).flutter analyzeandflutter testunchanged.Ordering
Uses the existing
nativeClearDartCallbackRegistrationsForEnginenative function; no C++ change.Generated by Claude Code