Repository navigation
[flutter_svg] Add SvgLoader.provideSvgInIsolate for unsendable loaders - #13598
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates flutter_svg to version 2.3.3. It introduces a top-level _compileSvg function and helper classes (_SvgSource, _SvgCompileRequest) to prevent sending non-sendable asset bundles or HTTP clients to compute isolates during SVG compilation. It also ensures that custom subclasses of SvgLoader retain their provideSvg overrides by checking if the loader is a built-in type before bypassing it. Unit tests have been added to verify these behaviors. No review comments were provided, so there is no feedback to address.
There was a problem hiding this comment.
Code Review
This pull request updates flutter_svg to version 2.3.3. It refactors SVG compilation to prevent sending non-sendable asset bundles or HTTP clients to compute isolates by introducing a top-level _compileSvg function and a sealed _SvgSource class hierarchy. Built-in loaders are updated to override _sourceFor to bypass provideSvg where appropriate, while custom loader subclasses retain their provideSvg overrides. New tests are added in isolate_safe_loaders_test.dart to verify isolate safety and subclass behavior. There are no review comments, and I have no feedback to provide.
1773d5c to
6104774
Compare
b-luk
left a comment
There was a problem hiding this comment.
This adds a lot of boilerplate around the built-in loader classes, and I don't like the runtime type detection of our specific list of built-in classes. This means that any custom loader subclasses, including subclasses of the built-in ones, would end up calling provideSvg on the UI isolate. That would be bad for things like subclasses of SvgFileLoader.
As an alternative, how about keeping the existing behavior, but having an on-error fallback that calls provideSvg on the UI isolate?
Something like
Future<ByteData> _load(BuildContext? context) {
final vg.SvgTheme theme = getTheme(context).toVgTheme();
return prepareMessage(context).then((T? message) {
return compute(
(T? message) => _compileSvg(_SvgCompileRequest(provideSvg(message), theme, colorMapper)),
message,
debugLabel: 'Load Bytes',
).onError((_, _) {
// The failure may be due to the loader not being sendable to an isolate.
// Retry, calling `provideSvg` directly.
final String svg = provideSvg(message);
return compute(
_compileSvg,
_SvgCompileRequest(svg, theme, colorMapper),
debugLabel: 'Load Bytes',
);
});
});
}_SvgCompileRequest would hold the SVG string instead of an _SvgSource.
This gets rid of the _SvgSource classes, the _sourceFor overrides, and the runtime type check.
It still kind of ugly, but with the existing API for these classes, I can't come up with a more elegant approach.
|
Thanks, done in c6acd43 (rebased on main). I switched to your suggested approach: the One side effect of the catch-all |
… extracting sources Keeps the existing compute call, and on failure (for example, a loader holding an unsendable asset bundle or HTTP client) calls provideSvg on the calling isolate and compiles the resulting string in compute. This removes the per-loader source classes and the runtime type check. Tests now run compute in a real isolate via debugComputeOverride.
6c9be62 to
c6acd43
Compare
b-luk
left a comment
There was a problem hiding this comment.
Please also update the PR title to reflect the new fallback approach.
…tests Replaces debugComputeOverride with a bool and keeps compute's branching in one getter. The tests now cover one sendable and one unsendable asset loader and check on which isolate provideSvg runs.
I think the catch-all is fine. The |
Can you apply this feedback? |
Already applied: the title was changed from "[flutter_svg] Avoid sending loader state to compute isolates" to "[flutter_svg] Fall back to provideSvg on the calling isolate for unsendable loaders", and the description was updated to match. Please let me know if you would like different wording. |
Equivalent newly allocated HTTP header maps currently miss the compiled SVG cache. Compare headers by value, and snapshot headers in cache keys so caller mutation cannot corrupt retained entries. HTTP clients stay out of equality and hashing, as before. The const loader constructor is preserved. Addresses the header equality bug reported in flutter/flutter#158818 (the report is closed, but this behavior remains in current main). Tests cover header order, distinct authorization values, mutation and eviction, concurrent de-duplication, and theme isolation. Validation on Windows: `flutter analyze` passed; complete `flutter test` passed (82 passed, one existing skip); formatting and `git diff --check` passed. Existing golden tolerances reported small platform differences; no golden files were updated. Merged with main (ca0a2d3) and bumped flutter_svg to 2.4.1, above the 2.4.0 entry from #13596. Analyze and the complete test suite pass after the merge (89 passed, one existing skip). Overlap with open PRs: #12808, #13599, #13600, and #13628 conflict only in release metadata. #13598 also conflicts in loaders.dart. The current heads of #13596 and #13613 conflict in cache.dart against the main changes included here. AI-assisted; independently checked with focused and complete tests. CLA status is left for the contributor/CLA bot to verify. ## Pre-Review Checklist
…isolate-safety # Conflicts: # third_party/packages/flutter_svg/CHANGELOG.md
Replaces the onError retry with a protected SvgLoader.provideSvgInIsolate getter. It defaults to true, so custom loaders and subclasses keep running provideSvg in the compute isolate. SvgAssetLoader and SvgNetworkLoader return false, because their bundle or client may retain unsendable objects.
|
This works for me. I suggested the fallback as an alternative to the original approach, which did work in the ui isolate for all custom subclasses. This new approach doesn't have that issue. Its downsides compared to the fallback approach are:
The downsides of the fallback approach are the things you mentioned about errors driving control flow and unrelated errors will end up doing the work twice. So both of these approaches have certain disadvantages. I don't think there's an ideal solution, so either of these approaches seems good enough for me. |
…er#197056) flutter/packages@81b7c44...898818d 2026-11-20 [email protected] [material_ui] Use correct time format for Sinhala locale (flutter/packages#13645) 2026-11-20 [email protected] [material_ui] Fix showModalBottomSheet safe area on display-feature sub-screens (flutter/packages#13428) 2026-11-20 [email protected] [material_ui] Fix M3 doc errors in the `ElevatedButton` and `FilledButton` (flutter/packages#13685) 2026-11-19 [email protected] [video_player] Convert implementation to Kotlin (flutter/packages#13683) 2026-11-19 [email protected] [flutter_svg] Add SvgLoader.provideSvgInIsolate for unsendable loaders (flutter/packages#13598) 2026-11-19 [email protected] [VPAT] Expose modal drawers with the dialog semantics role (flutter/packages#13660) 2026-11-19 [email protected] [VPAT] Keep a11y_assessments tooltips open while hovered (flutter/packages#13657) 2026-11-19 [email protected] [flutter_svg] Stabilize network SVG cache keys (flutter/packages#13597) 2026-11-19 [email protected] [image_picker_ios] Return an error instead of an empty file when an image cannot be saved (flutter/packages#12998) 2026-11-19 [email protected] [cupertino_ui] Fix CupertinoNavigationBarBackButton assertion debug (flutter/packages#13397) 2026-11-19 [email protected] [go_router] Document when GoRouterState.name and path are null (flutter/packages#13486) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages-flutter-autoroll Please CC [email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
`SvgPicture.debugFillProperties` compares `Clip` values to `BoxFit.contain`, so the default `Clip.hardEdge` is incorrectly reported as a non-default diagnostic property. Compare against the actual constructor default, `Clip.hardEdge`. The regression test checks that the default is omitted and a non-default `Clip.none` is reported. It fails against the original implementation and passes after the fix. Validation on Windows: `dart format lib/svg.dart test/clip_diagnostics_test.dart`, `flutter analyze` (no issues), and complete `flutter test` (72 passed, one existing skipped test). Existing platform golden differences are tolerated by the repository comparator. Final checks ran after rebasing on latest upstream/main. Prepared with AI assistance and reviewed by the contributor. Fixes flutter/flutter#197014. Original fork bug report: Otzaria#4. Each issue is submitted independently. Overlap: `git merge-tree --write-tree upstream/pr-12808 HEAD` reports a CHANGELOG-only conflict with #12808; no code overlap. Merged with main (including 2.5.0 from #13598) and bumped to 2.5.1; analyze and the complete test suite pass after the merge (94 passed, one existing skip). ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
The compute callback captures the loader, including its asset bundle or HTTP client. Bundles retaining Futures can therefore cause an unsendable-message exception in profile/release, although debug tests pass.
Fixes flutter/flutter#158835.
Adds a protected
SvgLoader.provideSvgInIsolategetter. It defaults to true, so custom loaders and subclasses keep callingprovideSvgin the compute isolate as before.SvgAssetLoaderandSvgNetworkLoaderreturn false:provideSvg(a UTF-8 decode) runs on the calling isolate, and only the SVG string, theme and color mapper are sent to a top-level_compileSvgcallback. No errors drive control flow, so an unrelated failure is not retried.The package's
computeruns inline in debug builds, so it now has a@visibleForTestingdebugRunComputeInlineflag; the tests set it to false and run in a real isolate. They check that a default loader callsprovideSvgin the isolate (0 calls on the test isolate), that the asset loader calls it on the calling isolate (1 call) with a bundle holding aFuture, that the network loader works with a client holding aFuture, and that an invalid SVG reports an error after a singleprovideSvgcall. WithprovideSvgInIsolatereturning true for the built-ins, the last three fail.Validation on Windows:
flutter analyzepassed; completeflutter testpassed (93 tests, one existing skip); formatting passed.Overlap: #12808 touches the same CHANGELOG (CHANGELOG-only conflict). Release numbering/changelog entries need coordination with other pending flutter_svg fixes.
AI-assisted; reviewed and checked with focused and complete tests.
Pre-Review Checklist
[shared_preferences]///).