Skip to content

Fix crash in WKWebViewNetworkMonitor when WKWebViews share a WKUserContentController - #471

Open
xinnjie wants to merge 2 commits into
DebugSwift:mainfrom
xinnjie:fix/wkwebview-shared-usercontentcontroller-crash
Open

xinnjie wants to merge 2 commits into
DebugSwift:mainfrom
xinnjie:fix/wkwebview-shared-usercontentcontroller-crash

Conversation

@xinnjie

@xinnjie xinnjie commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #470

  • Move the WKWebView network monitoring deduplication associated object in WKWebView.injectNetworkMonitoring(into:) from WKWebViewConfiguration to configuration.userContentController.
  • WKWebViewConfiguration.copy() shallow-copies userContentController without copying Objective-C associated objects (and callers may also explicitly share a WKUserContentController across configurations), which previously caused a duplicate add(_:name: "__debugswift_webview_net__") call and an uncaught NSInvalidArgumentException — notably triggered by WebKit's WKWebExtensionController.
  • Add unit tests in WKWebViewNetworkMonitorTests covering copied configurations, explicitly shared WKUserContentControllers, and distinct configurations.

Type of change

  • Fix
  • Feature
  • Refactor
  • Docs
  • CI/CD

Test plan

  • Unit tests updated
  • Manual testing completed
  • CI passing

Manual test steps

  1. Run the standalone repro script from [Bug]: Crash in WKWebViewNetworkMonitor when two WKWebViews share a WKUserContentController (e.g. copied WKWebViewConfiguration or WKWebExtensionController) #470 against this branch — testSharedUserContentControllerCrashesDebugSwift now passes (TEST SUCCEEDED) instead of throwing NSInvalidArgumentException at WKWebViewNetworkMonitor.swift:107.
  2. Run xcodebuild test -project Example/Example.xcodeproj -scheme Example -destination 'platform=iOS Simulator,id=<sim-id>' -only-testing:ExampleTests/WKWebViewNetworkMonitorTests — all 3 tests pass.

Screenshots / recordings (if applicable)

N/A (no UI changes)

Checklist

  • I reviewed my own changes
  • I updated docs when needed
  • I considered backward compatibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant