Fix NPE in Gson when serializing request headers on cold start#1603
Merged
Conversation
cortinico
requested changes
Apr 11, 2026
| try { | ||
| JsonConverter.instance.toJson(headers) | ||
| } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { | ||
| headers.joinToString(separator = "\n") { "${it.name}: ${it.value}" } |
Member
There was a problem hiding this comment.
This won't work sadly. The caller will always assume the field contains a JSON string:
So you should store it as valid JSON, not as plain string.
What would happen is that you will show nothing to the user even if headers are there.
| try { | ||
| JsonConverter.instance.toJson(headers) | ||
| } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { | ||
| headers.joinToString(separator = "\n") { "${it.name}: ${it.value}" } |
Member
There was a problem hiding this comment.
Once you do the other changes, can you refactor so that this code is inside a util function + add tests?
e5d69ff to
302e09d
Compare
3 tasks
Contributor
Author
|
Thanks @cortinico, good catch. Pushed an update that addresses both points:
|
Contributor
Author
|
@cortinico — CI is also showing |
Wrap `JsonConverter.instance.toJson(headers)` in try-catch in both `setRequestHeaders` and `setResponseHeaders` to prevent NPE from crashing the request thread when Gson's TypeVariableImpl.hashCode() fails during cold start reflection. On failure, falls back to a plain-text representation of the headers so the transaction is still recorded without crashing the app. Fixes ChuckerTeam#1602 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
302e09d to
d3f19bb
Compare
cortinico
approved these changes
Apr 23, 2026
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.
Summary
ChuckerInterceptorcrashes on cold start withNullPointerExceptioninTypeVariableImpl.hashCode()when Gson tries to serialize request headers via reflectionJsonConverter.instance.toJson(headers)in try-catch in bothsetRequestHeadersandsetResponseHeadersso the interceptor never crashes the request threadRoot cause
On cold start (after long idle), Gson's internal reflection for
List<HttpHeader>type resolution triggers aNullPointerExceptioninlibcore.reflect.TypeVariableImpl.hashCode(). This is a known JVM/Android runtime edge case where type metadata hasn't been fully initialized.Changes
HttpTransaction.kt— Added defensive try-catch around bothsetRequestHeadersandsetResponseHeadersserialization callsname: valueper line) so the transaction is still recordedTest plan
testDebugUnitTest)🤖 Generated with Claude Code