mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Race condition between GeolocateControl and map removal

Open tmcw opened this issue 1 year ago • 0 comments

mapbox-gl-js version: v2.9.1

browser: All

Steps to Trigger Behavior

  1. Add a GeolocateControl to a map
  2. Then remove the map before the geolocate control has finished its check for access

Link to Demonstration

      mapboxgl.accessToken =  "…;
      const map = new mapboxgl.Map({
        container: "map",
        style: "mapbox://styles/mapbox/streets-v11",
        center: [-79.4512, 43.6568],
        zoom: 8,
      });
      map.addControl(
        new mapboxgl.GeolocateControl({
          showUserLocation: false,
        })
      );
      map.remove();

Expected Behavior

There should be no uncaught errors doing this.

Actual Behavior

The _checkGeolocationSupport method calls its callback after the map has been removed, which them calls _setupUI, which refers to this.map._getUIString, which is not accessible because this.map has been destroyed.

A workaround would be to check that the this.map reference is still good after returning from the permission check. A fancier way to do this would be to "cancel" the promise by doing something like setting this._removed = true and throwing within the .then call if that's true.

tmcw avatar Jul 08 '22 19:07 tmcw