Skip to content

Comments

[webview_flutter_wkwebview] Fix crash when calling setOnConsoleMessage multiple times#10922

Merged
auto-submit[bot] merged 9 commits intoflutter:mainfrom
rickhohler:fix/webview-console-crash
Feb 20, 2026
Merged

[webview_flutter_wkwebview] Fix crash when calling setOnConsoleMessage multiple times#10922
auto-submit[bot] merged 9 commits intoflutter:mainfrom
rickhohler:fix/webview-console-crash

Conversation

@rickhohler
Copy link
Contributor

@rickhohler rickhohler commented Jan 29, 2026

Fixes flutter/flutter#180914

Description

This PR fixes a crash that occurs when setOnConsoleMessage is called multiple times. The WKUserContentController throws an exception if a script message handler with the same name is added more than once. This change ensures that any existing handler is removed before adding the new one.

Tests

  • Added setOnConsoleMessage called twice does not throw to webkit_webview_controller_test.dart.
  • Verified locally with flutter test.

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. 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.

Footnotes

  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. 2 3

Copy link

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

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 introduces two separate and valuable improvements. The main change fixes a crash in webview_flutter_wkwebview when setOnConsoleMessage is called multiple times by correctly removing any existing script message handler before adding a new one. This is accompanied by a solid test case. The second change improves error handling in camera_avfoundation by checking the result of AVAssetWriter.startWriting() and propagating errors, also with a corresponding test. Both changes are correct and well-implemented. For future contributions, consider submitting unrelated changes in separate pull requests to improve review focus and maintain a clean commit history.

@rickhohler rickhohler force-pushed the fix/webview-console-crash branch from 6adc861 to 754ec75 Compare January 29, 2026 03:48
@stuartmorgan-g
Copy link
Collaborator

Please see this comment, which applies here as well.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft January 29, 2026 14:20
) throws {
// WKUserContentController will crash if a script message handler with the same name
// is added twice. We remove the existing one (if any) to ensure the new one replaces it.
pigeonInstance.removeScriptMessageHandler(forName: name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

As a high-level review, this is not consistent with the design of this plugin, which is to use intentionally minimal native wrappers, and implement as much logic as possible in Dart. We would not add remove logic to the add wrapper when removing can be done from Dart.

@rickhohler rickhohler marked this pull request as ready for review January 29, 2026 17:21
Copy link

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

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 fixes a crash that occurs when setOnConsoleMessage is called multiple times by ensuring any existing script message handler is removed before adding a new one. The change is correct and is accompanied by a new test case that properly verifies the fix. I have one minor suggestion regarding code formatting consistency in the new test.

@rickhohler
Copy link
Contributor Author

rickhohler commented Jan 29, 2026

@stuartmorgan-g I've refactored the fix to be purely in Dart as requested. The native changes have been reverted, and the fix is now implemented in webkit_webview_controller.dart with a corresponding regression test. I also restored the PR checklist.

@stuartmorgan-g stuartmorgan-g requested review from bparrishMines and removed request for LouiseHsu January 29, 2026 17:50
@stuartmorgan-g
Copy link
Collaborator

  • Added testAddScriptMessageHandlerHandlesDuplicates to UserContentControllerProxyAPITests.swift.
  • Verified locally with xcodebuild (Exit Code 0).

The first entry here is no longer accurate, and it's not clear how the second is a test.

These changes don't appear to have been pushed to the PR.

@rickhohler
Copy link
Contributor Author

@stuartmorgan-g Thanks for catching that. I've updated the PR description to accurately reflect that the regression test is now in Dart (testAddScriptMessageHandlerHandlesDuplicates was replaced by setOnConsoleMessage called twice does not throw in webkit_webview_controller_test.dart).

I also added the explanatory comment suggested by @hellohuanlin regarding the crash prevention logic. CI checks should be green.

addJavaScriptChannel(channelParams);
// If fltConsoleMessage is already present, it would crash when adding it again.
if (_javaScriptChannelParams.containsKey('fltConsoleMessage')) {
await removeJavaScriptChannel('fltConsoleMessage');
Copy link
Contributor

Choose a reason for hiding this comment

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

If you are just changing the callback, I dont think you need to remove and add a channel. You should just make this check right after setting _onConsoleMessageCallback above and return in this if statement.


addJavaScriptChannel(channelParams);
// If fltConsoleMessage is already present, the callback is already registered.
if (_javaScriptChannelParams.containsKey('fltConsoleMessage')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since fltConsoleMessage is used multiple times in this method, can you put it into a variable? Also these 3 lines can be moved above the channelParams declaration.

@rickhohler rickhohler force-pushed the fix/webview-console-crash branch from 408b1d4 to 3c17cd2 Compare February 6, 2026 17:06
Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

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

LGTM

Sorry for the delay. I updated the branch and made the channel name a constant since it is now used three times in the file.

@bparrishMines bparrishMines added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 20, 2026
@auto-submit auto-submit bot merged commit 12b43a1 into flutter:main Feb 20, 2026
81 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 23, 2026
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Feb 23, 2026
flutter/packages@9da22bf...12b43a1

2026-02-20 rick.hohler@gmail.com [webview_flutter_wkwebview] Fix crash
when calling setOnConsoleMessage multiple times (flutter/packages#10922)
2026-02-20 engine-flutter-autoroll@skia.org Manual roll Flutter from
c023e5b to 91b2d41 (31 revisions) (flutter/packages#11088)
2026-02-20 kallentu@google.com [rfw] Remove old TODOs for code block
languages. (flutter/packages#11080)
2026-02-20 matt.boetger@gmail.com [google_maps_flutter] Remove
usesCleartextTraffic (flutter/packages#11079)
2026-02-20 stuartmorgan@google.com [google_maps_flutter] Improve iOS
shared code validation (flutter/packages#11070)
2026-02-20 mdebbar@google.com Revert "#167410: _initCalled completed
twice (#9694)" (flutter/packages#11084)
2026-02-20 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump androidx.activity:activity from 1.12.2 to 1.12.4 in
/packages/image_picker/image_picker_android/android
(flutter/packages#11033)
2026-02-20 matt.boetger@gmail.com [interactive_media_ads] Remove
usesCleartextTraffic (flutter/packages#11065)
2026-02-19 matt.boetger@gmail.com [image_picker] Remove
usesCleartextTraffic (flutter/packages#11076)
2026-02-19 matt.boetger@gmail.com [quick_actions_android] Remove
deprecated usesCleartextTraffic (flutter/packages#11063)
2026-02-19 matt.boetger@gmail.com [quick_actions] Remove
usesCleartextTraffic (flutter/packages#11064)
2026-02-19 matt.boetger@gmail.com [google_maps_flutter_android] Remove
usesCleartextTraffic (flutter/packages#11078)
2026-02-19 louisehsu@google.com [google_maps_flutter_ios] Migrate to
UIScene (flutter/packages#11001)
2026-02-19 matt.boetger@gmail.com [image_picker_android] Remove
deprecated usesCleartextTraffic (flutter/packages#11059)
2026-02-19 matt.boetger@gmail.com [file_selector_android] Remove
deprecated usesCleartextTraffic (flutter/packages#11057)
2026-02-19 matt.boetger@gmail.com [webview_flutter_android] Remove
usesCleartextTraffic (flutter/packages#11066)
2026-02-19 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump exoplayer_version from 1.9.1 to 1.9.2 in
/packages/video_player/video_player_android/android
(flutter/packages#10985)
2026-02-19 stuartmorgan@google.com [google_maps_flutter] Fix add-marker
crash on Android (flutter/packages#11061)
2026-02-19 42980667+srivats22@users.noreply.github.com #167410:
_initCalled completed twice (flutter/packages#9694)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com 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
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 p: webview_flutter platform-ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[webview_flutter] Calling setOnConsoleMessage multiple times throws

4 participants