Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RealWebSocket send throws writerTask NPE #5705

Closed
Naitbit opened this issue Jan 6, 2020 · 5 comments · Fixed by #5707
Closed

RealWebSocket send throws writerTask NPE #5705

Naitbit opened this issue Jan 6, 2020 · 5 comments · Fixed by #5707
Labels
bug Bug in existing code
Milestone

Comments

@Naitbit
Copy link

Naitbit commented Jan 6, 2020

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)

@Naitbit Naitbit added the bug Bug in existing code label Jan 6, 2020
@swankjesse
Copy link
Member

Will fix.

@swankjesse
Copy link
Member

Great bug report. Will try to get a release out tonight.
#5707

@swankjesse
Copy link
Member

FYI, OkHttp 4.3.1 is released.

@simtse
Copy link

simtse commented Apr 16, 2020

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.

@tomsammons
Copy link

tomsammons commented May 12, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants