fix(android): cap console message length before String.format to prevent OOM (#8532)#8533
Open
codebyshoaib wants to merge 1 commit into
Open
fix(android): cap console message length before String.format to prevent OOM (#8532)#8533codebyshoaib wants to merge 1 commit into
codebyshoaib wants to merge 1 commit into
Conversation
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.
Fixes #8532
Problem
BridgeWebChromeClient.onConsoleMessagerunsString.format(...)on the entire JS console message unconditionally. When JS logs a very large value (a serialized IndexedDB row, an axios error/response object, etc.), the message can be tens or hundreds of MB. Materializing that into a JavaStringblows the WebView heap on low-RAM Android devices, producingjava.lang.OutOfMemoryErrorand a hard crash.In our production app this was the #2 fatal crash; one device attempted a single ~266 MB allocation on every occurrence.
loggingBehavior: 'none' / 'production'does not prevent it, because the format cost is paid before logging is gated.Fix
Cap the console message length before it is passed to
String.format. Anything overMAX_CONSOLE_MESSAGE_LENGTH(8 KB) is truncated with a… [truncated]marker. This keeps the log readable while making the OOM structurally impossible, and touches nothing else.Reproduction
In any Capacitor Android app, on a low-RAM device (or an emulator with a small WebView heap cap):
Before:
java.lang.OutOfMemoryError. After: app stays alive; the log line is truncated.Possible follow-up (not included, to keep this minimal)
Short-circuiting before
String.formatwhen logging is disabled would also avoid paying the cost for logs that are never emitted. I left it out to avoid assumptions about the preferredLogger/loggingBehaviorAPI — happy to add it if the team wants it.Notes