mapbox / mapbox/mapbox-gl-draw

Infinite Loop when Changing Mode in onStop with draw.create Event

Open
#1,347 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged bug
Dominant language
JavaScript
Stars
1.1k
Forks
612
Avg merge
8d 9h
Merged PRs (30d)
5

Description

Title: Infinite Loop when Changing Mode in onStop with draw.create Event

Description:
I'm encountering an infinite loop issue when trying to switch to a custom mode after completing a drawing using mapbox-gl-draw.

When the drawing is finished, my onStop function is triggered, where I fire a draw.create event. After that, I attempt to change the mode to a custom mode using changeMode(). However, this results in a recursive loop with the following error:

mapbox-gl.js?v=40f4fdd7:1833 Uncaught RangeError: Maximum call stack size exceeded
    at Map.fire (mapbox-gl.js?v=40f4fdd7:1833:15)
    at CustomDrawRectangle.onStop (mono.ts:123:14)
    at Object.stop (@mapbox_mapbox-gl-dr…s?v=40f4fdd7:939:14)
    at Object.stop (@mapbox_mapbox-gl-dr…s?v=40f4fdd7:178:27)
    at Object.changeMode (@mapbox_mapbox-gl-dr…?v=40f4fdd7:1090:17)
    at api.changeMode (@mapbox_mapbox-gl-dr…?v=40f4fdd7:3425:16)
    at Map.<anonymous> (roof-zone.vue?t=1737448820365:79:16)
    at Map.fire (mapbox-gl.js?v=40f4fdd7:1838:39)
    at CustomDrawRectangle.onStop (mono.ts:123:14)
    at Object.stop (@mapbox_mapbox-gl-dr…s?v=40f4fdd7:939:14)
Steps to Reproduce:
  1. Start drawing using a custom mode (e.g., CustomDrawRectangle).
  2. When the drawing is completed, onStop is triggered.
  3. In onStop, I fire a draw.create event and immediately call changeMode() to switch to another mode.
  4. This results in an infinite recursive loop and the error above.
Expected Behavior:

I expect to be able to switch to another mode without triggering a recursive loop.

Actual Behavior:

Calling changeMode() inside onStop causes an infinite loop.

Code Snippet:
CustomDrawRectangle.onStop = function () {
    this.map.fire('draw.create', { features: [this.feature] });
    this.changeMode('custom-mode'); // Switching mode triggers an infinite loop
};
Environment:
  • mapbox-gl version: 3.9.3
  • mapbox-gl-draw version: 1.5.0
Possible Cause:

It seems like changeMode() internally calls stop(), which in turn calls onStop again, causing a loop. Perhaps an internal mechanism prevents mode changes inside onStop?

Suggested Fix:

Is there a recommended approach for switching modes after drawing is complete? Should there be a debounce or asynchronous delay before calling changeMode()?

Any insights or workarounds would be appreciated!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the custom mode's onStop handler and trace the stop and changeMode call path shown in the stack trace. Reproduce the draw.create and immediate mode switch from the snippet, then verify that completing a drawing changes modes without recursive onStop calls or a stack overflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.