fix(ai): avoid native WebView2 crash in book_content_search on Windows - #978
Open
bazzdug-arch wants to merge 1 commit into
Open
fix(ai): avoid native WebView2 crash in book_content_search on Windows#978bazzdug-arch wants to merge 1 commit into
bazzdug-arch wants to merge 1 commit into
Conversation
Creating a second headless WebView (for AI book_content_search) while the reader WebView is alive crashes the app natively on Windows (flutter_inappwebview #2840): a hard access violation with no Dart exception, regardless of WebViewEnvironment/userDataFolder separation. On Windows, run the search on the live reader controller instead of spawning a 2nd headless WebView: - EpubPlayerState.runAiBookSearch(): resets tocSearchProvider and runs the book-scoped search() on the existing reader WebView (with jsonEncode keyword escaping), reusing the reader's onSearch handler. - BookContentSearchRepository.search(): on Windows, if the target book is the currently-open one, drive runAiBookSearch() and collect results from tocSearchProvider (poll until progress >= 1.0); for a non-current book, return a graceful StateError instead of crashing. - Non-Windows keeps the existing headless session path (unchanged). Verified: ~12 consecutive book_content_search calls on the open book, all completed:true, no crash (previously crashed on the first call).
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Windows, invoking the AI
book_content_searchtool crashes the app hard(native access violation, no Dart exception, the process just dies). It is
reproducible whenever the AI searches the currently-open book. Related:
#844 (P1, open), #888 (closed stale).
Root cause
BookContentSearchRepositoryserves the search by spinning up a secondHeadlessInAppWebViewthat re-loads the book and runs foliate-jssearch().On Windows (WebView2), creating a 2nd headless WebView while the reader
WebView is alive triggers a native crash in
flutter_inappwebview(upstream issue #2840). The crash is deterministic on affected machines and
is not mitigated by giving the 2nd WebView its own
WebViewEnvironment/userDataFolder— I tested that first (still crashed;the log showed the dedicated environment being created, then the process
died mid-search with no
completedevent).Fix (Option A: reuse the live reader controller)
On Windows, do not create a 2nd WebView. Run the search on the existing
reader WebView and collect the results.
EpubPlayerState.runAiBookSearch(keyword)— resetstocSearchProviderand runs the book-scoped
search()on the live reader WebView, reusingthe reader's existing
onSearchhandler (results flow totocSearchProvider). UsesjsonEncodefor safe keyword escaping.BookContentSearchRepository.search():runAiBookSearch()and collect results fromtocSearchProvider(polluntil
progress >= 1.0), thenclearSearch()to clean up.StateErrorin the tool result instead of crashing. The AI gets aclear message to ask the user to open the book.
(the native crash is Windows/WebView2-specific).
Why not fix the plugin / keep the dedicated environment
A separate
WebViewEnvironment(separateuserDataFolder) was the firstattempt and does not help — the crash happens at 2nd-WebView creation
itself, independent of environment separation. The real fix belongs in the
flutter_inappwebviewnative Windows plugin (#2840); this PR is anapp-level workaround that avoids the 2nd WebView entirely on Windows so the
AI feature works today, with zero behavior change on other platforms.
Verification
Built and tested on Windows (Flutter 3.35.3, dev build 1.15.0):
book_content_searchcrashed the app on the first call (log cutoff mid-search, no
completedevent, process restarted).book_content_searchcalls on the open book,all
completed:true(~1-1.9 s each), real results (CFI + snippets), appstayed alive throughout. A search with zero matches returned
completed:truewith emptyresults(graceful, no crash).flutter analyzeon the changed files: no issues.Files
lib/page/book_player/epub_player.dart— addrunAiBookSearch().lib/service/ai/tools/repository/book_content_search_repository.dart—Windows live-reader path + graceful fallback; non-Windows unchanged.
lib/service/ai/tools/ai_tool_registry.dart— passWidgetRefto therepository (needed to read
tocSearchProvider/currentReadingProvider).Notes / limitations
error) until the native plugin issue is fixed. Acceptable trade-off vs. a
hard crash; the common case (AI asked about the book the user is reading)
works.
drawer briefly reflects the AI's search (results are cleared on
completion). Concurrent manual TOC search + AI search on the same book
could conflict; AI tool calls are normally sequential.