Repository navigation
fix(android): don't leak a fused-location thread when maps swap location - #4397
Merged
Merged
Conversation
All maps share one Mapbox device location provider. When a screen transition removes one map's location puck and reattaches another map in the same frame, the provider is restarted before its stop completes and Mapbox Common orphans its fusedLocationClientHandlerThread. Keep a consumer on the provider for a moment whenever a map gives up location so the next map takes over without a stop/start. Also stop the shared LocationManager when a map is dropped, so a Camera following the user doesn't keep it running after the map is gone. Fixes #4225
This branch was previously deployed
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.
Description
Fixes #4225
All maps share one Mapbox device location provider (
LocationServicecaches it per request). When a native-stack pop removes the detail map'sLocationPuckand reattaches the base map in the same frame, the provider is stopped and started again before the stop completes. Mapbox Common'sGoogleDeviceLocationProviderthen creates a newfusedLocationClientHandlerThreadand never quits the old one ("Skipped stop state change: current state is STARTING" in its debug log).Reported upstream with a native-only repro: mapbox/mapbox-maps-android#2738
The fix: when a map gives up location (puck/follow disabled, view detached, or dropped),
LocationComponentManagerkeeps a no-op consumer on the map's provider for 1s, so another map can take over without a stop/start. It also callsLocationComponentManager.onDestroy()fromonDropViewInstance, so aCamerawithfollowUserLocationno longer leaves the sharedLocationManagerrunning after its map is gone.Measured on an Android 14 emulator (Play Services 23.18) with
play-services-location:21.3.0added to the example app; without it Mapbox uses the platform provider and nothing leaks.fusedLocationthreads / total threads / native heap alloc while pushing and popping a screen that has a map with a puck, from a base screen that has one too:Control without
LocationPuckon main: 1 fused thread at 0/10/20 cycles. Camera follow map opened and closed: main leaves 2 fused threads, this PR returns to 1.I couldn't reproduce with the issue's single-map toggle on the emulator; the leak needs the stop and the next start to land in the same frame, which the two-map navigation (the issue's "1 live map" baseline) does reliably.
Also checked on this branch: the puck shows and tracks on fresh mount and after 30 remounts,
Camera followUserLocationfollows,UserLocationonUpdate(thelocationManagerJS API) keeps delivering, and ~220 rapid push/pops at 100 ms didn't crash. Native heap grows at that 100 ms rate on main as well, which is a separate issue.Checklist
CONTRIBUTING.mdyarn generatein the root folder/exampleapp./example)Component to reproduce the issue you're fixing
Replace
example/src/examples/BugReportExampleTS.tsxwith this and addimplementation("com.google.android.gms:play-services-location:21.3.0")toexample/android/app/build.gradle, then press Start.