fix(android): guard onDetachedFromEngine against null channel (NPE on headless engine teardown)#26
Open
Ant1Miller wants to merge 1 commit into
Open
fix(android): guard onDetachedFromEngine against null channel (NPE on headless engine teardown)#26Ant1Miller wants to merge 1 commit into
Ant1Miller wants to merge 1 commit into
Conversation
The MethodChannel is created in onAttachedToActivity, so a Flutter engine that registers the plugin without ever attaching an Activity (e.g. an FCM background isolate) reached onDetachedFromEngine with channel still null and crashed with a NullPointerException on teardown. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
Reported in ZD #6122 (SuperLyfe / MAHHP), reproducible on Android and confirmed against source.
MethodChannel channelis instantiated inonAttachedToActivity(line 364), not inonAttachedToEngine(which only stores theBinaryMessenger).onDetachedFromEnginethen calledchannel.setMethodCallHandler(null)unconditionally.A Flutter engine that registers the plugin but never attaches an Activity — the everyday case being an FCM background isolate, but also WorkManager / background-locator engines — reaches
onDetachedFromEnginewithchannel == nulland crashes:Present on 0.9.4 (this branch) and reported identical on 0.8.3.
Fix
Null-guard the teardown and clear the field:
Verification
No Android unit-test harness exists in this package (
src/testis empty), so this is verified by inspection rather than a runtime test: line 482 is the exact frame in the customer's crash trace, and the guard makes that dereference safe on the null-channel path while leaving the Activity-attached path untouched. Adversarial review confirmed no use-after-null on any other path (channel is read only here, written only inonAttachedToActivity).Scope / risk
Additive defensive guard on a path that previously always crashed for headless engines — no behaviour change for Activity-attached apps. Risk to other customers: none.
Deliberately out of scope (the customer's secondary observations, to be handled separately): the
success: truemasking in init callbacks, the emptyonDetachedFromActivity(handler leak on activity re-attach), and thegetAthletenotImplemented branch.🤖 Generated with Claude Code