#13731·mapbox-gl-js

Tile reload timers armed for aborted tiles survive map.remove() and retain the removed Map (regression in v3.26.0)

Author: shaunml09Created Sep 28, 2026Updated Sep 28, 2026

mapbox-gl-js version

v3.31.0. The bug first appears in v3.26.0 (v3.25.0 is clean with the same page).

Browser and version

Chromium 148.0.7778.96 (headless, via Playwright): reproduces on every run. With WebKit 26.4 the minimal page below does not reproduce; it seems to be timing-dependent there. We did see the same retention in WebKit in a larger app, and the fix below resolved it there too.

Expected behavior

After map.remove(), the map sets no more tile reload timers, and the removed Map can be garbage-collected.

Actual behavior

Suppose a basemap config property changes (setConfigProperty) while the camera is moving. Then SourceCache arms tile reload timers (~12 h, from the tile's cache headers) for tiles that were already aborted and dropped from _tiles. clearTiles() only walks _tiles, so map.remove() never clears these timers. Each timer's closure holds the SourceCache, and through it the removed Map, its container and anything that references them. In a single-page app every visit to a map view keeps the previous map alive, and memory grows for the life of the tab.

Link to the demonstration

A self-contained page is below; paste in an access token. It uses no framework, only the public API.

Steps to trigger the unexpected behavior

  1. Create a map with mapbox://styles/mapbox/standard and wait for idle.
  2. Call easeTo(...) (2 s), and 300 ms later map.setConfigProperty('basemap', 'showPointOfInterestLabels', false).
  3. Wait until map.loaded(), then call map.remove().
  4. Repeat. Count pending timers longer than 1 h, and removed maps still reachable after GC.
repro.html
xml
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.31.0/mapbox-gl.css" rel="stylesheet">
<style>#map { width: 800px; height: 600px; } pre { font: 13px/1.4 monospace; }</style>
</head>
<body>
<div id="map"></div>
<pre id="out">running…</pre>
<script>
// Count the long timers the page arms and clears (tile reload timers are ~hours long).
const pending = new Set();
const origSet = window.setTimeout, origClear = window.clearTimeout;
window.setTimeout = function (fn, ms, ...args) {
  const id = origSet.call(window, function (...a) { pending.delete(id); return fn.apply(this, a); }, ms, ...args);
  if (ms > 60 * 60 * 1000) pending.add(id);
  return id;
};
window.clearTimeout = function (id) { pending.delete(id); return origClear.call(window, id); };
</script>
<script src="https://api.mapbox.com/mapbox-gl-js/v3.31.0/mapbox-gl.js"></script>
<script>
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
const out = document.getElementById('out');
const wait = (ms) => new Promise((r) => origSet(r, ms));
const removed = [];

async function once(i) {
  const container = document.getElementById('map');
  const map = new mapboxgl.Map({ container, style: 'mapbox://styles/mapbox/standard', center: [-74.5, 40], zoom: 12 });
  await new Promise((r) => map.once('idle', r));

  // Move the camera, and while it moves change a basemap config property.
  map.easeTo({ center: [-74.45, 40.05], zoom: 14.5, duration: 2000 });
  await wait(300);
  map.setConfigProperty('basemap', 'showPointOfInterestLabels', false);
  await wait(2500);
  while (!map.loaded()) await wait(200);

  map.remove();
  removed.push(new WeakRef(map));
  out.textContent = `after map.remove() #${i + 1}: ${pending.size} tile reload timers (> 1 h) still pending`;
}

(async () => {
  for (let i = 0; i < 6; i++) await once(i);
  await wait(3000);
  if (window.gc) { for (let i = 0; i < 5; i++) { window.gc(); await wait(300); } }
  const alive = removed.filter((r) => r.deref()).length;
  out.textContent = `after 6 x (create, easeTo + setConfigProperty, remove):\n` +
    `  pending tile reload timers (> 1 h): ${pending.size}  (expected 0)\n` +
    (window.gc ? `  removed Map instances still reachable after GC: ${alive} / 6  (expected 0)` :
      `  (start Chrome with --js-flags=--expose-gc to also count removed maps still reachable after GC)`);
})();
</script>
</body>
</html>

Relevant log output

bash
# Chromium 148, --js-flags=--expose-gc; same page, only the version in the two URLs changed
# v3.29.0 dist with the two-line patch below: pending reload timers: 0   removed maps alive after GC: 0/6
v3.18.0  pending reload timers: 0   removed maps alive after GC: 0/6
v3.19.0  pending reload timers: 0   removed maps alive after GC: 0/6
v3.20.0  pending reload timers: 0   removed maps alive after GC: 0/6
v3.25.0  pending reload timers: 0   removed maps alive after GC: 0/6   (two runs)
v3.26.0  pending reload timers: 24  removed maps alive after GC: 6/6
v3.27.0  pending reload timers: 16  removed maps alive after GC: 4/6
v3.28.0  pending reload timers: 24  removed maps alive after GC: 6/6
v3.29.0  pending reload timers: 24  removed maps alive after GC: 6/6
v3.31.0  pending reload timers: 17  removed maps alive after GC: 5/6
v3.31.0  pending reload timers: 21  removed maps alive after GC: 6/6   (second run)

Investigation

Traced in the v3.31.0 sources, by instrumenting SourceCache.prototype at runtime to record the stack at each abort and each timer arm:

  1. setConfigProperty → on the next frame, Style.update → updateSourceCaches → reloadSource → SourceCache.reload() → _reloadTile(id, 'reloading') for every tile → VectorTileSource.loadTile.
  2. In the same frame, SourceCache.update() drops the retained tiles that are no longer in the ideal set while the camera moves. The tile is still loading or reloading, so _removeTile sets tile.aborted = true and calls _abortTile.
  3. VectorTileSource.loadTile is async since v3.26.0: await this.map._requestManager.transformRequest(...), then if (controller.signal.aborted) return callback(null);. That callback(null) runs SourceCache._tileLoaded(tile, id, 'reloading', null) → _setTileReloadTimer(id, tile). The tile keeps its expiry from its earlier load, so getExpiryTimeout() is ~12 h, and a timer is stored in this._timers[id] for an id that is no longer in this._tiles.
  4. clearTiles() (run by map.remove()) iterates this._tiles and clears timers only through _removeTile. Timers keyed to ids that are not in _tiles survive, and their closures (() => this._reloadTile(id, 'expired')) retain the SourceCache and the Map.

In v3.25.0, loadTile called transformRequest synchronously and handed the tile to the worker, so there was no async gap between the reload and the abort. That explains why v3.25.0 is clean. We measured the abort-to-timer gap at 2–3 ms.

Suggested fix

We run both changes as a local patch against the v3.29.0 dist build; with it the page above reports 0 timers and 0/6 maps retained. The first stops the orphaned timers from being armed. The second is a backstop so that remove() never leaves a reload timer behind:

diff
 // src/source/source_cache.ts
     _setTileReloadTimer(id: number, tile: Tile) {
         if (id in this._timers) {
             clearTimeout(this._timers[id]);
             delete this._timers[id];
         }
+        // An aborted tile has already left the pyramid; nothing will clear a timer set for it.
+        if (tile.aborted) return;

         const expiryTimeout = tile.getExpiryTimeout();
diff
 // src/source/source_cache.ts
     clearTiles() {
         this._shouldReloadOnResume = false;
         this._paused = false;

         for (const id in this._tiles)
             this._removeTile(+id);
+
+        for (const id in this._timers)
+            clearTimeout(this._timers[id]);
+        this._timers = {};

Another option is to not call back at all from VectorTileSource.loadTile when the request was aborted, which matches the v3.25.0 behaviour for aborted tiles. That may matter to other callers of the callback, though, so the SourceCache guard seems the more contained change.

Possibly related: #13625, whose fix made reload timers fire from cache headers again.