[Bug][WebSocket] Crash when use 'ws' for websocket
I use okhttp-ws:2.6 for websocket support. When I use 'wss' to create websocket, there's nothing bad happend, but when I change to use 'ws' to create websocket, there are quite a lot of crashes. The stack info is below:
Fatal Exception: java.lang.IllegalStateException: state: 4 at com.squareup.okhttp.internal.http.HttpConnection.writeRequest(SourceFile:171) at com.squareup.okhttp.internal.http.HttpTransport.writeRequestHeaders(SourceFile:75) at com.squareup.okhttp.internal.http.HttpEngine.readResponse(SourceFile:744) at com.squareup.okhttp.Call.getResponse(SourceFile:277) at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(SourceFile:234) at com.squareup.okhttp.Call.getResponseWithInterceptorChain(SourceFile:196) at com.squareup.okhttp.Call.access$100(SourceFile:34) at com.squareup.okhttp.Call$AsyncCall.execute(SourceFile:162) at com.squareup.okhttp.internal.NamedRunnable.run(SourceFile:33) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841)
I cannot figure out why HttpConnection state change to STATE_OPEN_RESPONSE_BODY(value 4) and how the crash is happened.
Here is the crash device infos:

If you can provide us with an executable test case, it'll speed up our ability to fix this.
I'm sorry I still can't find a test case to produce the crash, any work around for this?
from the source code, this crash happens when the websocket create
HttpEngine.class
if (forWebSocket) { transport.writeRequestHeaders(networkRequest); networkResponse = readNetworkResponse(); }
When the code run to HttpTransport.writeRequestHeaders(networkRequest); the crash happens.
The HttpEngine class is not thread-safe, is there any chance that another thread run "readNetworkResponse" first and set the state to STATE_OPEN_RESPONSE_BODY(value 4) and then the current thread run transport.writeRequestHeaders(networkRequest); and check the state value and crash?
I face the same problem too, the server guys said wss consume much more cpu, so we change to ws temporarily,then the crashes happened. So the is it interrelated to the difference between wss and ws?
I think this is related to #2581. I can get a similar stack trace with this test case against 2.6:
@Test public void redirect() throws IOException {
server.enqueue(new MockResponse().setResponseCode(307).addHeader("Location", "/bar"));
WebSocketRecorder serverListener = new WebSocketRecorder();
server.enqueue(new MockResponse().withWebSocketUpgrade(serverListener));
WebSocket webSocket = awaitWebSocket();
webSocket.sendMessage(RequestBody.create(TEXT, "Hello, WebSockets!"));
serverListener.assertTextMessage("Hello, WebSockets!");
}
@EricRGC @dreamtalee could you try upgrading to 3.3.1?
I check the source code, and I don't think this bug is the same one of #2581
If you can reproduce this on OkHttp 3.3.1, that’d help a lot.
@EricRGC @swankjesse If this is not reproducible in OkHttp 3, can we just close this?
No obvious action to take on this.