square / okio

A modern I/O library for Android, Java, and Kotlin Multiplatform.
https://square.github.io/okio/
Apache License 2.0
8.72k stars 1.17k forks source link

NullPointerException on commonCompleteSegmentByteCount #1392

Closed vitorpamplona closed 6 months ago

vitorpamplona commented 6 months ago

Looks like there is a possible NPE in commonCompleteSegmentByteCount.

I can't reproduce this bug but this is a PlayStore stack from an Xperia 5 II running Android 12 (SDK 31)

Exception java.lang.NullPointerException:
  at okio.internal.-Buffer.commonCompleteSegmentByteCount (-Buffer.java)
  at okio.Buffer.completeSegmentByteCount (Buffer.kt)
  at okio.RealBufferedSink.emitCompleteSegments (RealBufferedSink.kt)
  at okio.DeflaterSink.deflate (DeflaterSink.kt)
  at okio.DeflaterSink.finishDeflate$okio (DeflaterSink.kt)
  at okio.DeflaterSink.close (DeflaterSink.kt)
  at okhttp3.internal.ws.MessageDeflater.close (MessageDeflater.kt)
  at okhttp3.internal.ws.WebSocketWriter.close (WebSocketWriter.kt)
  at okhttp3.internal._UtilCommonKt.closeQuietly (-UtilCommon.kt)
  at okhttp3.internal.ws.RealWebSocket.failWebSocket (RealWebSocket.kt)
  at okhttp3.internal.ws.RealWebSocket$connect$1.onResponse (RealWebSocket.kt)
  at okhttp3.internal.connection.RealCall$AsyncCall.run (RealCall.kt)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:644)
  at java.lang.Thread.run (Thread.java:1012)

The code is using versions:

swankjesse commented 6 months ago

@vitorpamplona is it possible you’re operating on your Okio streams from multiple threads concurrently? Concurrent modifications can trigger errors like this.

swankjesse commented 6 months ago

Oooh, actually it’s OkHttp to blame for exactly that, and that bug is very recently fixed. https://github.com/square/okhttp/pull/8121

Will ship a new OkHttp 5.0 alpha very soon with that fix.

vitorpamplona commented 6 months ago

I don't use okio streams directly. I just used them through OkHttp. I assume OkHttp takes care of the thread handling part.

Do I need to take additional protections when multithreading?

vitorpamplona commented 6 months ago

Thank you! ☺️