Skip to content

fix: prevent the infinite loop of feature layers in web debug builds (DDC, Dart 3.14) - #2299

Open
ZaraclaJ wants to merge 2 commits into
fleaflet:masterfrom
ZaraclaJ:fix/ddc-labeled-break-infinite-loop
Open

ZaraclaJ wants to merge 2 commits into
fleaflet:masterfrom
ZaraclaJ:fix/ddc-labeled-break-infinite-loop

Conversation

@ZaraclaJ

@ZaraclaJ ZaraclaJ commented Nov 17, 2026 •

Copy link
Copy Markdown

Fixes #2234.

What happens

Since Flutter 3.50.0 / Dart 3.14.0, in web debug builds, CircleLayer, PolylineLayer and PolygonLayer throw on paint and on hit test:

Assertion failed: "Infinite loop going beyond 30 for world width 2097152"

The shapes are not drawn, and the error is thrown again on every repaint and pointer move over the map.

Release web builds, mobile and desktop are not affected. I hit this in our app right after upgrading from Flutter 3.47 to 3.50.

Root cause

It is a Dart compiler bug, not a flutter_map bug: dart-lang/sdk#64941. DDC (the web debug compiler) of Dart 3.14 drops the label of a break label; placed inside a switch whose last case is empty. workAcrossWorlds has exactly that shape:

negativeWorldsLoop:
for (double shift = -worldWidth;; shift -= worldWidth) {
  protectInfiniteLoop();
  switch (work(shift)) {
    case WorldWorkControl.hit:
      return true;
    case WorldWorkControl.invisible:
      break negativeWorldsLoop;
    case WorldWorkControl.visible:
  }
}

I compiled a copy of this function with dartdevc from two SDKs (dartaotruntime bin/snapshots/dartdevc_aot.dart.snapshot --dart-sdk-summary=lib/_internal/ddc_outline.dill -o out.js probe.dart):

Dart 3.10.9 (Flutter 3.38.10), correct:

L0:
  for (let shift = -worldWidth;; shift = shift - worldWidth) {
    protectInfiniteLoop();
    switch (work(shift)) {
      case C[0] || CT.C0: { return true; }
      case C[2] || CT.C2: { break L0; }
      case C[1] || CT.C1:
    }
  }

Dart 3.14.0 (Flutter 3.50.0), wrong: the label is gone, so break only leaves the switch and the loop never ends, until protectInfiniteLoop throws:

for (let shift = -worldWidth;; shift = shift - worldWidth) {
  protectInfiniteLoop();
  switch (work(shift)) {
    case C[0] || CT.C0: { return true; }
    case C[2] || CT.C2: { break; }
    case C[1] || CT.C1:
  }
}

The VM and dart2js compile the same code correctly, which is why only web debug builds break. This function is identical in 8.2.2, 8.3.2 and master, so the regression comes from the SDK upgrade alone. I did not test Dart 3.11 to 3.13.

The fix

The two world loops are now one local function called once per direction, which leaves with return, so no label is needed. The switch stays exhaustive over WorldWorkControl and the duplicated loop is gone. The behaviour is unchanged: same iteration order, same return values.

I preferred this to adding an explicit break; to the empty case WorldWorkControl.visible: (the workaround mentioned in dart-lang/sdk#64941): that also works, but it looks redundant and could easily be removed later. A short comment links the Dart issue so the labeled break is not reintroduced.

This was the only labeled break/continue in lib/.

Tests

New test/layer/shared/feature_layer_utils_test.dart covers workAcrossWorlds directly: it stops in each direction at the first invisible world, keeps going while worlds are visible, returns true on a hit, and only works in the primary world when the CRS is not replicated.

With Flutter 3.50.0 stable:

flutter test (VM) flutter test --platform chrome (DDC)
master, new test file 4 pass 3 fail (Infinite loop going beyond 30 for world width 2048)
master, existing test/layer/circle_layer_test.dart pass fails (Infinite loop going beyond 30 for world width 2097152)
this branch, both files pass pass

Full VM suite on this branch: 144 tests pass. dart format reports no change.

Two tests of network_image_provider_test.dart ("Non-image response … exceptions enabled") fail with --platform chrome on master as well as on this branch; they are unrelated to this change.

How to reproduce

Quickest, in this repository with Flutter 3.50.0:

git checkout master
flutter test --platform chrome test/layer/circle_layer_test.dart   # fails
git checkout fix/ddc-labeled-break-infinite-loop
flutter test --platform chrome test/layer/circle_layer_test.dart   # passes

In an app (no tile layer needed), run with flutter run -d chrome:

import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';

void main() => runApp(const ReproApp());

class ReproApp extends StatelessWidget {
  const ReproApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.ltr,
      child: ColoredBox(
        color: const Color(0xFFE8E8E8),
        child: FlutterMap(
          options: const MapOptions(
            initialCenter: LatLng(48.85, 2.35),
            initialZoom: 11,
          ),
          children: [
            PolygonLayer(
              polygons: [
                Polygon(
                  points: const [
                    LatLng(48.95, 2.20),
                    LatLng(48.95, 2.30),
                    LatLng(48.90, 2.25),
                  ],
                  color: const Color(0x5500AA00),
                  borderColor: const Color(0xFF00AA00),
                  borderStrokeWidth: 2,
                ),
              ],
            ),
            PolylineLayer(
              polylines: [
                Polyline(
                  points: const [
                    LatLng(48.80, 2.20),
                    LatLng(48.85, 2.35),
                    LatLng(48.80, 2.50),
                  ],
                  color: const Color(0xFF6A4CD6),
                  strokeWidth: 4,
                ),
              ],
            ),
            const CircleLayer(
              circles: [
                CircleMarker(
                  point: LatLng(48.85, 2.35),
                  radius: 5000,
                  useRadiusInMeter: true,
                  color: Color(0x33FF0000),
                  borderColor: Color(0xFFFF0000),
                  borderStrokeWidth: 2,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
  • With flutter_map: 8.3.2: grey screen, none of the three shapes is drawn, and the console fills with Infinite loop going beyond 30 for world width 524288 (first thrown from _PolygonPainter.paint).
  • With this branch (git: {url: https://github.com/ZaraclaJ/flutter_map, ref: fix/ddc-labeled-break-infinite-loop-8.3.2}, the same commit on top of v8.3.2): the polygon, the polyline and the circle are drawn, panning and zooming work, and the console stays empty.

Notes

  • I did not touch the version or the CHANGELOG.
  • If you would rather wait for the Dart fix, this change is harmless to keep: it does not depend on the compiler bug being present.
  • As asked in CONTRIBUTING: I used an AI assistant to help investigate this, write the change and draft this description. I have reviewed the code and the results above.

This branch has not been deployed

No deployments
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.

Infinite loop when using feature layers on web

2 participants