Skip to content

fix(android): don't leak a fused-location thread when maps swap location - #4397

Merged
mfazekas merged 1 commit into
mainfrom
fix/android-location-puck-leak
Oct 25, 2026
Merged

mfazekas merged 1 commit into
mainfrom
fix/android-location-puck-leak

Conversation

@mfazekas

@mfazekas mfazekas commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #4225

All maps share one Mapbox device location provider (LocationService caches it per request). When a native-stack pop removes the detail map's LocationPuck and reattaches the base map in the same frame, the provider is stopped and started again before the stop completes. Mapbox Common's GoogleDeviceLocationProvider then creates a new fusedLocationClientHandlerThread and 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), LocationComponentManager keeps a no-op consumer on the map's provider for 1s, so another map can take over without a stop/start. It also calls LocationComponentManager.onDestroy() from onDropViewInstance, so a Camera with followUserLocation no longer leaves the shared LocationManager running after its map is gone.

Measured on an Android 14 emulator (Play Services 23.18) with play-services-location:21.3.0 added to the example app; without it Mapbox uses the platform provider and nothing leaks. fusedLocation threads / 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:

cycles main this PR
0 3 / 79 / 275 MB 3 / 81 / 264 MB
10 10 / 85 / 310 MB 3 / 80 / 283 MB
20 12 / 89 / 306 MB 3 / 81 / 285 MB
30 3 / 81 / 282 MB
~200 at 100 ms 197 / 284 3 / 85

Control without LocationPuck on 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 followUserLocation follows, UserLocation onUpdate (the locationManager JS 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

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder
  • I have tested the new feature on /example app.
    • In V11 mode/ios
    • In New Architecture mode/ios
    • In V11 mode/android
    • In New Architecture mode/android
  • I added/updated a sample - if a new feature was implemented (/example)

Component to reproduce the issue you're fixing

Replace example/src/examples/BugReportExampleTS.tsx with this and add implementation("com.google.android.gms:play-services-location:21.3.0") to example/android/app/build.gradle, then press Start.

import { useEffect, useState } from 'react';
import { Button, Text, View } from 'react-native';
import {
  useNavigation,
  type NavigationProp,
  type ParamListBase,
} from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { Camera, LocationPuck, MapView } from '@rnmapbox/maps';

// Repro for https://github.com/rnmapbox/maps/issues/4225: a base screen with a
// MapView + LocationPuck repeatedly pushes and pops a detail screen that also has
// a MapView + LocationPuck. Count fused-location threads with:
//   adb shell "for t in /proc/$(adb shell pidof com.rnmapboxexample)/task/*/comm; do cat \$t; done" | grep -c fusedLocation
const INTERVAL_MS = 1500;

let detailNavigation: { goBack: () => void } | null = null;

const PuckMap = () => (
  <MapView style={{ flex: 1 }}>
    <Camera centerCoordinate={[-74.00597, 40.71427]} zoomLevel={14} />
    <LocationPuck />
  </MapView>
);

const Base = () => {
  const navigation = useNavigation<NavigationProp<ParamListBase>>();
  const [running, setRunning] = useState(false);
  const [cycles, setCycles] = useState(0);

  useEffect(() => {
    if (!running) {
      return;
    }
    const id = setInterval(() => {
      if (detailNavigation) {
        detailNavigation.goBack();
        setCycles((c) => c + 1);
      } else {
        navigation.navigate('Detail');
      }
    }, INTERVAL_MS);
    return () => clearInterval(id);
  }, [navigation, running]);

  return (
    <View style={{ flex: 1 }}>
      <Button
        testID="repro-start"
        title={running ? 'Stop' : 'Start'}
        onPress={() => setRunning((r) => !r)}
      />
      <Text testID="repro-cycles">cycles: {cycles}</Text>
      <PuckMap />
    </View>
  );
};

const Detail = () => {
  const navigation = useNavigation();

  useEffect(() => {
    detailNavigation = navigation;
    return () => {
      detailNavigation = null;
    };
  }, [navigation]);

  return <PuckMap />;
};

const Stack = createNativeStackNavigator();

const BugReportExample = () => (
  <Stack.Navigator>
    <Stack.Screen name="Base" component={Base} />
    <Stack.Screen name="Detail" component={Detail} />
  </Stack.Navigator>
);

export default BugReportExample;

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

1 inactive deployment
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.

[Bug]: LocationPuck leaks one fused-location provider (thread + native memory) per MapView destroy on Android

1 participant