Skip to content

[flutter_svg] Add SvgLoader.provideSvgInIsolate for unsendable loaders - #13598

Merged
auto-submit[bot] merged 8 commits into
flutter:mainfrom
palmoni5:codex/upstream-svg-isolate-safety
Nov 19, 2026
Merged

auto-submit[bot] merged 8 commits into
flutter:mainfrom
palmoni5:codex/upstream-svg-isolate-safety

Conversation

@palmoni5

@palmoni5 palmoni5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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.provideSvgInIsolate getter. It defaults to true, so custom loaders and subclasses keep calling provideSvg in the compute isolate as before. SvgAssetLoader and SvgNetworkLoader return 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 _compileSvg callback. No errors drive control flow, so an unrelated failure is not retried.

The package's compute runs inline in debug builds, so it now has a @visibleForTesting debugRunComputeInline flag; the tests set it to false and run in a real isolate. They check that a default loader calls provideSvg in the isolate (0 calls on the test isolate), that the asset loader calls it on the calling isolate (1 call) with a bundle holding a Future, that the network loader works with a client holding a Future, and that an invalid SVG reports an error after a single provideSvg call. With provideSvgInIsolate returning true for the built-ins, the last three fail.

Validation on Windows: flutter analyze passed; complete flutter test passed (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

@github-actions github-actions Bot added p: flutter_svg triage-engine Should be looked at in engine triage labels Nov 9, 2026
@palmoni5
palmoni5 marked this pull request as draft November 9, 2026 13:36

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@palmoni5
palmoni5 force-pushed the codex/upstream-svg-isolate-safety branch from 1773d5c to 6104774 Compare November 12, 2026 23:24
@gaaclarke
gaaclarke requested a review from b-luk November 16, 2026 19:25

@b-luk b-luk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@palmoni5

palmoni5 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, done in c6acd43 (rebased on main). I switched to your suggested approach: the _SvgSource classes, _sourceFor overrides and runtime type check are gone. _load keeps the existing compute call, and onError retries with provideSvg on the calling isolate, sending only the SVG string, theme and color mapper to a top-level _compileSvg.

One side effect of the catch-all onError: an invalid SVG is compiled twice before the error surfaces. I kept it as you suggested and added a test that the error still propagates. I can narrow it to ArgumentError (what Isolate.spawn throws for unsendable messages) if you prefer.

… 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.
@palmoni5
palmoni5 force-pushed the codex/upstream-svg-isolate-safety branch from 6c9be62 to c6acd43 Compare November 17, 2026 01:18
@palmoni5
palmoni5 requested a review from b-luk November 17, 2026 01:19
@github-actions github-actions Bot added the P2 Important issues not at the top of the work list label Nov 17, 2026
@b-luk b-luk added the CICD Run CI/CD label Nov 17, 2026

@b-luk b-luk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 17, 2026
@palmoni5 palmoni5 changed the title [flutter_svg] Avoid sending loader state to compute isolates [flutter_svg] Fall back to provideSvg on the calling isolate for unsendable loaders Nov 17, 2026
@b-luk b-luk added the CICD Run CI/CD label Nov 17, 2026
@b-luk

b-luk commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

One side effect of the catch-all onError: an invalid SVG is compiled twice before the error surfaces. I kept it as you suggested and added a test that the error still propagates. I can narrow it to ArgumentError (what Isolate.spawn throws for unsendable messages) if you prefer.

I think the catch-all is fine. The SendPort.send docs don't specify what a failed send throws, so narrowing to ArgumentError would depend on undocumented behavior. The double compile only happens on the failure case and not the common case, so I think it's fine.

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 17, 2026
@palmoni5
palmoni5 requested a review from b-luk November 17, 2026 21:40
@b-luk b-luk added the CICD Run CI/CD label Nov 17, 2026
b-luk
b-luk previously approved these changes Nov 17, 2026

@b-luk b-luk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@b-luk
b-luk requested a review from gaaclarke November 17, 2026 21:50
@Piinks

Piinks commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Please also update the PR title to reflect the new fallback approach.

Can you apply this feedback?

@palmoni5

Copy link
Copy Markdown
Contributor Author

Please also update the PR title to reflect the new fallback approach.

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.

auto-submit Bot pushed a commit that referenced this pull request Nov 19, 2026
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.
@palmoni5 palmoni5 changed the title [flutter_svg] Fall back to provideSvg on the calling isolate for unsendable loaders [flutter_svg] Add SvgLoader.provideSvgInIsolate for unsendable loaders Nov 19, 2026
@palmoni5
palmoni5 requested review from b-luk and gaaclarke November 19, 2026 21:38

@gaaclarke gaaclarke left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, what do you think @b-luk? Sorry if I stepped on your toes, I didn't read through your review notes.

@b-luk

b-luk commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

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:

  1. Unsendable custom loaders will still hit the original issue until the authors update them to use the new provideSvgInIsolate API. I don't know how common this case would be. I suspect not very common.
  2. The asset and network loaders (and any subclasses of them) always call provideSvg on the UI isolate, even in common cases where they are sendable and could run in the compute isolate. Their provideSvg only does utf8 decoding, which I think is fast enough to be fine on the ui isolate.

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.

@b-luk b-luk added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Nov 19, 2026
@auto-submit
auto-submit Bot merged commit a7ea227 into flutter:main Nov 19, 2026
15 checks passed
@palmoni5
palmoni5 deleted the codex/upstream-svg-isolate-safety branch November 19, 2026 22:33
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-flutter__flutter that referenced this pull request Nov 20, 2026
…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
auto-submit Bot pushed a commit that referenced this pull request Nov 25, 2026
`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD p: flutter_svg P2 Important issues not at the top of the work list triage-engine Should be looked at in engine triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flutter_svg] Illegal argument in isolate message: object is unsendable - Library:'dart:async' Class: _Future

4 participants