Skip to content

fix(search): stop each removed place-search field leaving a timer running #487

Backlog

Description

@Ardnived

Summary

Each place-search field (a <mapbox-geocoder> from @mapbox/[email protected]) starts a 1-second telemetry timer that keeps running after the field is removed. Every field the widget mounts and unmounts during a visit adds another timer, and nothing clears them. On a host page that keeps the widget open for a long session, the count grows without bound, and phones never get to idle. #485 mounts the field on every country, region and city page, so the count now grows with each page a visitor opens.

Approach

The library code is at node_modules/@mapbox/search-js-web/dist/index-esm.js, MapboxEventManager, around lines 2266–2520.

  • The constructor arms setTimeout(flush, 1000) (line ~2518).
  • #flush sends any queued events. Whenever #flushInterval is set (always 1000) it re-arms itself, unconditionally (lines ~2497–2505).
  • remove() is this.remove = () => this.#flush() (line ~2512), so tearing a field down re-arms the timer instead of clearing it.
  • Each geocoder constructs its own manager: new MapboxEventManager(1 /* Geocoding */) (line ~3322). #timer and #flushInterval are private fields, so nothing in our code can reach them.

1.6.0 is the newest release (npm view @mapbox/search-js-web version, checked 2026-11-25), so there is no upgrade to take. Mapbox's search-js repository is not public, so there is no upstream issue to follow.

Fix it with a patches/ entry, the repo's last resort for defects our own code cannot reach (see src/components/AGENTS.md → Patch register):

  • remove() flushes once more, then clears #timer and stops re-arming. For example, set #flushInterval to 0 after the final flush, so the re-arm guard skips it.
  • Patch every build our bundle and the lane load, as the Schedule-X patch does. Check which of index-esm.js and the CJS build each consumer resolves.
  • Add a row to the patch register in src/components/AGENTS.md, and a drift pin plus a behavioural spec in src/lib/shadow-patches.test.ts, which fails on a patch it does not pin.

Rejected: mounting the field only on first focus. It reduces how many timers pile up but does not remove the leak, and it changes how the field behaves.

Acceptance criteria

  • Removing a geocoder leaves no timer of its own scheduled. Queued events are still sent once on removal.
  • A geocoder that stays mounted still sends its search telemetry as it does today.
  • The patch is registered in the patch register and pinned in src/lib/shadow-patches.test.ts.

Verification checklist

  • pnpm install applies the patch cleanly. pnpm lint && pnpm typecheck && pnpm test:run pass, including the new pin.
  • In a browser on pnpm dev, wrap setTimeout to count calls whose delay is 1000 ms. Navigate / → /gb → /gb/south-east → /gb/south-east/london → back to /, then wait 5 s with no interaction. The 1000 ms reschedules per second equal the number of mounted geocoders (1), not the number ever mounted.
  • Type in the field and pick a place. A request still goes to Mapbox's events endpoint, so telemetry is unchanged.

Notes

Measured during the review of #485, in Playwright's Chromium against the library's own element: 20 geocoders created and removed gave about 17 timer wakeups per second with none mounted, against about 1 per second for a single instance. Each tick costs microseconds, roughly 0.03% of a core at 200 timers. The cost is that the count never stops growing on a long-lived host page, not CPU.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    Priority

    Low

    Hold Until

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions