Skip to content

Commit 7ce9047

Browse files
authored
[web] Defer disposal of stale cached paths to FrameArena (flutter#192072)
When mutating an `EnginePath` after building and drawing it in the same frame, `_invalidateCachedPath()` was prematurely disposing `_cachedPath`. In multi-threaded WebAssembly (`skwasm`), this eager mid-frame deallocation violated the lifecycle contract of `FrameArena` and caused native allocations to be freed on the main thread while background raster thread operations were deallocating previous display lists in the same linear memory space, leading to heap corruption and `RuntimeError: memory access out of bounds` in `$emscripten_builtin_free`. This change keeps invalidated `BackendPath` instances in `_stalePaths` until the end of the frame when `FrameArena.collect()` runs. Fixes flutter#191976 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 3761056 commit 7ce9047

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/primitives/path.dart‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,17 @@ class EnginePath implements ui.Path, Collectable {
673673
return _cachedPath!;
674674
}
675675

676+
// Invalidated backend paths that must remain alive until the end of the frame
677+
// when [collect] is called by the [FrameArena]. Disposing them immediately
678+
// here can cause premature deallocation while recorded display list operations
679+
// are being rasterized concurrently on the worker thread.
680+
List<BackendPath>? _stalePaths;
681+
676682
void _invalidateCachedPath() {
677-
_cachedPath?.dispose();
678-
_cachedPath = null;
683+
if (_cachedPath != null) {
684+
(_stalePaths ??= <BackendPath>[]).add(_cachedPath!);
685+
_cachedPath = null;
686+
}
679687
}
680688

681689
void _addCommand(PathCommand command) {
@@ -875,6 +883,12 @@ class EnginePath implements ui.Path, Collectable {
875883

876884
@override
877885
void collect() {
886+
if (_stalePaths != null) {
887+
for (final BackendPath path in _stalePaths!) {
888+
path.dispose();
889+
}
890+
_stalePaths = null;
891+
}
878892
_cachedPath?.dispose();
879893
if (!identical(_cachedBuilder, _cachedPath)) {
880894
_cachedBuilder?.dispose();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:test/bootstrap/browser.dart';
6+
import 'package:test/test.dart';
7+
import 'package:ui/ui.dart' as ui;
8+
9+
import '../common/rendering.dart';
10+
import '../common/test_initialization.dart';
11+
12+
void main() {
13+
internalBootstrapBrowserTest(() => testMain);
14+
}
15+
16+
Future<void> testMain() async {
17+
setUpUnitTests(withImplicitView: true, setUpTestViewDimensions: false);
18+
19+
ui.Picture recordPicture() {
20+
final recorder = ui.PictureRecorder();
21+
final canvas = ui.Canvas(recorder, const ui.Rect.fromLTWH(0, 0, 500, 500));
22+
final path = ui.Path()..lineTo(100.0, 100.0);
23+
final paint = ui.Paint();
24+
25+
canvas.drawPath(path, paint);
26+
path.close();
27+
canvas.drawPath(path, paint);
28+
29+
return recorder.endRecording();
30+
}
31+
32+
test('mutating a path after drawing does not crash during rasterization', () async {
33+
final sceneBuilder = ui.SceneBuilder()..addPicture(ui.Offset.zero, recordPicture());
34+
final Future<void> renderFuture = renderScene(sceneBuilder.build());
35+
await Future<void>.delayed(Duration.zero);
36+
37+
for (var i = 0; i < 5; i++) {
38+
recordPicture().dispose();
39+
}
40+
41+
await renderFuture;
42+
});
43+
}

0 commit comments

Comments
 (0)