Okhttp: RealWebSocket send throws writerTask NPE

Created on 6 Jan 2020  路  5Comments  路  Source: square/okhttp

okhttp 4.3.0 throws NPE each time I try to send command via websocket.
writerTask is null in RealWebSocket::runWriter

  private fun runWriter() {
    this.assertThreadHoldsLock()

    taskQueue.schedule(writerTask!!) // crashes in this line
  }

Stacktrace:

Caused by kotlin.KotlinNullPointerException
       at okhttp3.internal.ws.RealWebSocket.runWriter + 398(RealWebSocket.java:398)
       at okhttp3.internal.ws.RealWebSocket.send + 352(RealWebSocket.java:352)
       at okhttp3.internal.ws.RealWebSocket.send + 332(RealWebSocket.java:332)

test:

import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.WebSocketListener
import okhttp3.mockwebserver.MockWebServer
import org.junit.Rule
import org.junit.Test


class OkHttpBug {
    @get:Rule
    val server = MockWebServer()

    @Test
    fun test() {
        val request = Request.Builder()
            .url(server.url("/path"))
            .header("User-Agent", "Test Case")
            .build()
        val client = OkHttpClient()
        val listener = object : WebSocketListener() {

        }
        val command = """{ "id": "file" }"""
        val webSocket = client.newWebSocket(request, listener)
        webSocket.send(command)
        webSocket.close(1000, "RealWebSocket crashes with runWriter writerTask!! NPE")
    }
}

This seems to be 100% reproducible in 4.3.0. Had about 1000 crashes on that version on small amount of sessions.
This (almost) does not happen with the same code on okhttp 4.2.2 (2 crashes in 1000x user count in much longer period)

bug

Most helpful comment

FYI, OkHttp 4.3.1 is released.

All 5 comments

Will fix.

Great bug report. Will try to get a release out tonight.
https://github.com/square/okhttp/pull/5707

FYI, OkHttp 4.3.1 is released.

Hmm @swankjesse we ran into a similar issue and we're using OkHttp 4.5.0.

kotlin.KotlinNullPointerException
        at okhttp3.internal.ws.RealWebSocket.writePingFrame$okhttp(RealWebSocket:569)
        at okhttp3.internal.ws.RealWebSocket$initReaderAndWriter$$inlined$synchronized$lambda$1.runOnce(TaskQueue.kt:219)
        at okhttp3.internal.concurrent.TaskRunner.runTask(TaskRunner.kt:116)
        at okhttp3.internal.concurrent.TaskRunner.access$runTask(TaskRunner.kt:42)
        at okhttp3.internal.concurrent.TaskRunner$runnable$1.run(TaskRunner.kt:65)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)

nvm, i'm going to open up another issue for this.

Hey @simtse, are you still facing this issue and were you able to open an issue for it? It looks like I'm also facing the same issue on 4.5.0 and 4.6.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yschimke picture yschimke  路  3Comments

vanshg picture vanshg  路  3Comments

nikunjgundaniya picture nikunjgundaniya  路  3Comments

SandroMachado picture SandroMachado  路  3Comments

rfc2822 picture rfc2822  路  3Comments