Android OkHttp3 instrumentation prefetches entire response body

We have recently switched from OkHttp2 to OkHttp3 and upgraded to NewRelic Agent 5.9 for Android. We have some code that is using the OkHttp3’ HttpURLConnection interface to perform downloads. After the switch we noticed that the download progress was no longer reported linearly throughout the download. Instead, the download was stuck at 0% for a long time before going to 100% instantly.

After some debugging, we have traced the source of this issue to New Relic’s OkHttp3 instrumentation. Specifically, NewRelic injects OkHttp3 interceptors that prebuffer the response body stream. The classes responsible for this behavior are ResponseBuilderExtension and PrebufferedResponseBody in the NewRelic instrumentation.

Can you update your instrumentation code to avoid pre-fetching the entire response body stream? The existing behavior limits the use of OkHttp3 for streaming applications like downloads and potentially also uses a large amount of memory to buffer and prefetch the data.

@tjalba thank you for providing this information. I’d like to gather a bit more information:

  • Which version of OkHttp3 is being used?
  • Is a WebSocket used?
  • Is caching enabled for the connection?

@spotts: Here is the info you requested:

  1. OkHttp3 v3.5.0 (com.squareup.okhttp3:okhttp-urlconnection:3.5.0)
  2. No websockets used.
  3. Caching is disabled explicitly using the Cache-Control header:
    connection.setRequestProperty(“Cache-Control”, “no-cache”);

@tjalba Thank you for the additional details. I’ve passed this information onto our Mobile Engineering team for further investigation. This post will be updated to notify when a fix is available.

@tjalba and anyone else following along at home. Android agent version 5.12.1 has been released which resolves this pre-fetch/buffer issue with OkHttp3.

Updating to this version of the agent (or newer) should resolve the issue.