React-native: [Android] `Cannot close WebSocket.` repeatedly

Created on 21 Jul 2016  路  66Comments  路  Source: facebook/react-native

I upgraded my project from 0.28 to 0.30 and since I have, on Android, this warning message every 2-3 seconds:

Cannot close WebSocket. Unknown WebSocket id 0

With the id increasing.

screen shot 2016-07-21 at 20 34 07

I did a fresh react-native init and it also appears. So it's not my project related.

Android Ran Commands Locked

Most helpful comment

+1
We are having A LOT of crashes in production! Is there any progress on this issue?

All 66 comments

@facebook-github-bot label Android

cc @frantic

I forgot to mention that it appears both on Genymotion and on a real device.

After a bit of debugging it seems to be related to https://github.com/facebook/react-native/commit/22fbb6d46d4b91bfe08b83aaba46ad92a32bf984.

A commit is in the pipe https://github.com/facebook/react-native/commit/46417dd26a4ab247d59ad147fdfe1655cb23edf9 but even by cherry-picking it I still have the same problem.

I also see this.

E unknown:JSPackagerWebSocketClient: Error occurred, shutting down websocket connection: Websocket exception
E unknown:JSPackagerWebSocketClient: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8081
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:187)
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:170)
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187)
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
E unknown:JSPackagerWebSocketClient:    at okhttp3.RealCall.getResponse(RealCall.java:243)
E unknown:JSPackagerWebSocketClient:    at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
E unknown:JSPackagerWebSocketClient:    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
E unknown:JSPackagerWebSocketClient:    at okhttp3.RealCall.access$100(RealCall.java:30)
E unknown:JSPackagerWebSocketClient:    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
E unknown:JSPackagerWebSocketClient:    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
E unknown:JSPackagerWebSocketClient:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
E unknown:JSPackagerWebSocketClient:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
E unknown:JSPackagerWebSocketClient:    at java.lang.Thread.run(Thread.java:818)
W unknown:React: Cannot close WebSocket. Unknown WebSocket id 36

Yeah, I can confirm that this is happening for me as well.

I have same problem, using v0.30

Seeing the same thing on 0.30, although I can't say for sure that it didn't _also_ occur on 0.29(it's possible that it went unnoticed).

+1

+1,V0.30

+1

I'm having the same problem, using v0.30.

+1 / v0.30

+1 v0.30

+1, v0.30

+1 v0.30

+1 I just upgraded from 0.29 which didn't have this issue.

+1 v0.30

I would like to add after upgrading to v0.30 I am seeing this too. When you enable remote debugging the log also spams a lot more.

I started getting these warning messages after creating and adding some native Android modules.

v0.30

+1. Started seeing it with 0.30 as well (0.29 did not have this).
[update] Upgrading to 0.31 fixed the issue for me

+1 v0.30

+1 v0.30 (w/ custom native packages registered)

+1 v0.30

+1 v0.30

+1 v0.30

+1 Upgraded from 0.24 to 0.30. It has caused all firebase communication to halt.

Upgrading to 0.31.0-rc.1 seems to resolve this error.

Anyone still having this issue after upgrading to 0.31.0? I personally stopped seeing it after the upgrade.

+1 on v0.30.0.

Upgrading to v0.31.0 fixes this for me.

It's not fixed on v0.31.0

Any news on this?
is causing me 8% of android crashes in production using 0.32.1. No issues on iOS.

馃憤 Having same problem 0.30

RN.0.34.0.RC.0 - same problem.
The problem occurs only when "Debug JS Remotely" is active.

+1 to the point of application crashing due to out of memory exception. Literally happens after executing network request once and leaving app open causes a force close.

`10-03 14:12:09.245 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3118
10-03 14:12:09.282 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3119
10-03 14:12:09.310 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3120
10-03 14:12:09.348 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3121
10-03 14:12:09.381 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3122
10-03 14:12:09.414 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3123
10-03 14:12:09.449 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3124
10-03 14:12:09.480 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3125
10-03 14:12:09.510 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3126
10-03 14:12:09.553 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3127
10-03 14:12:09.587 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3128
10-03 14:12:09.620 10449 10492 W unknown:React: Cannot close WebSocket. Unknown WebSocket id 3129

10-03 14:12:09.639 10449 15937 W libc : pthread_create failed: couldn't allocate 1069056-bytes mapped space: Out of memory
10-03 14:12:09.639 10449 15937 W art : Throwing OutOfMemoryError "pthread_create (1040KB stack) failed: Try again"
10-03 14:12:09.640 10449 15937 E AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
10-03 14:12:09.640 10449 15937 E AndroidRuntime: Process: com.mypackage, PID: 10449
10-03 14:12:09.640 10449 15937 E AndroidRuntime: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at java.lang.Thread.nativeCreate(Native Method)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at java.lang.Thread.start(Thread.java:1063)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1339)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.ConnectionPool.put(ConnectionPool.java:134)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.OkHttpClient$1.put(OkHttpClient.java:96)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:183)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.RealCall.getResponse(RealCall.java:240)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.RealCall.access$100(RealCall.java:30)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
10-03 14:12:09.640 10449 15937 E AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
10-03 14:12:09.643 1796 8718 W ActivityManager: Force finishing activity com.ironman/.MainActivity
10-03 14:12:09.644 1796 8718 I ActivityManager: moveHomeStack, setupComplete:true
10-03 14:12:09.686 1796 4360 I MotoSensors: Sensorhub hal enable: 1 - 0
`

Any workaround for this, until we get a fix for this?

+1 v0.35.0 firebase 3.5.1

Using react-native v0.35.0 & firebase v3.5.0. All I'm doing is load an image from Firebase Storage. Works fine on iOS.

Error in emulator on crash:

Firebase Storage: Max retry time for operation exceeded, please try again.

Console in Android Studio:

E/unknown:React: Could not close WebSocket connection for id 1
                 java.net.SocketException: Socket is closed
                     at com.android.org.conscrypt.OpenSSLSocketImpl.checkOpen(OpenSSLSocketImpl.java:270)
                     at com.android.org.conscrypt.OpenSSLSocketImpl.-wrap0(OpenSSLSocketImpl.java)
                     at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:809)
                     at okio.Okio$1.write(Okio.java:78)
                     at okio.AsyncTimeout$1.write(AsyncTimeout.java:180)
                     at okio.RealBufferedSink.emit(RealBufferedSink.java:178)
                     at okhttp3.internal.ws.WebSocketWriter.writeControlFrameSynchronized(WebSocketWriter.java:152)
                     at okhttp3.internal.ws.WebSocketWriter.writeClose(WebSocketWriter.java:111)
                     at okhttp3.internal.ws.RealWebSocket.close(RealWebSocket.java:168)
                     at com.facebook.react.modules.websocket.WebSocketModule.close(WebSocketModule.java:197)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke(BaseJavaModule.java:318)
                     at com.facebook.react.cxxbridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:158)
                     at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
                     at android.os.Handler.handleCallback(Handler.java:751)
                     at android.os.Handler.dispatchMessage(Handler.java:95)
                     at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
                     at android.os.Looper.loop(Looper.java:154)
                     at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196)
                     at java.lang.Thread.run(Thread.java:761)

+1 0.36 firebase 3.5.2

Any workaround to avoid this error in logs temporarily? 50% of my logs are blotted up because of this error.

+1 0.38

+1 0.38, happens rarely. Both iOS and Android.

  • 1

+1
Causing my app to crash

+1 with RN 0.44 and Firebase 3.9.0, both Android and iOS

Could #10856 be a potential fix? This is causing most of our crashes in production as well ...

We're using RN 0.43.4, Firebase 3.9.0 and experiencing this on Android only.

Windows 8.1
RN 0.45.1
Firebase 4.1.3
API 23

Could not close WebSocket connection for id 2
java.net.SocketException: Socket is closed
    at com.android.org.conscrypt.OpenSSLSocketImpl.checkOpen(OpenSSLSocketImpl.java:247)
    at com.android.org.conscrypt.OpenSSLSocketImpl.-wrap0(OpenSSLSocketImpl.java)
    at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:756)
    at okio.Okio$1.write(Okio.java:78)
    at okio.AsyncTimeout$1.write(AsyncTimeout.java:180)
    at okio.RealBufferedSink.emit(RealBufferedSink.java:178)
    at okhttp3.internal.ws.WebSocketWriter.writeControlFrameSynchronized(WebSocketWriter.java:152)
    at okhttp3.internal.ws.WebSocketWriter.writeClose(WebSocketWriter.java:111)
    at okhttp3.internal.ws.RealWebSocket.close(RealWebSocket.java:168)
    at com.facebook.react.modules.websocket.WebSocketModule.close(WebSocketModule.java:206)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.facebook.react.cxxbridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
    at com.facebook.react.cxxbridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)
    at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
    at android.os.Looper.loop(Looper.java:148)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:199)
    at java.lang.Thread.run(Thread.java:818)

Same here, on RN latest version

Similar Issue:
RN : 0.42
Happens so often on remote debugging.

`
E/unknown:React: Exception in native call
java.lang.RuntimeException: Cannot send a message. Unknown WebSocket id 2
at com.facebook.react.modules.websocket.WebSocketModule.send(WebSocketModule.java:221)
at java.lang.reflect.Method.invoke(Native Method)
at com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke(BaseJavaModule.java:345)
at com.facebook.react.cxxbridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:136)
at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
at android.os.Looper.loop(Looper.java:148)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196)
at java.lang.Thread.run(Thread.java:818)

                                                                       --------- beginning of system

`

same issue as @rimzici on RN 42.3

@ThaJay @rimzici I'm having the same issue on production. Any updates on this?
Are you guys having this issue only in debug mode?

I've got a similar issue. After a network reconnect I've got this one:

screenshot 2017-10-21 14 30 20

On iOS there is no such error. I've got the app logic to close the websocket after network reconnect and open a new one. On iOS this logic works perfectly. But on Android I've this error. I guess the nature of this error is the same as in the original post. What are we going to do about it?

Same. I get this issue both in debug and production releases on Google Play. I only get this issue on Android.
skaermbillede 2017-11-05 kl 12 14 51

+1
We are having A LOT of crashes in production! Is there any progress on this issue?

@guysegal It's the most common crash in production for me as well. They need to fix this ASAP

+1

Any updates?

React native 0.51.0

Getting this:
skaermbillede 2017-12-07 18 39 22

@guysegal @danieldelouya guys, does anyone know how can we draw the RN's team attention to this?

+ 1
0.50.3 android

before you construct a new WebSocket instance, you need release your prev instance

this.ws.close() && this.ws = null

it works for me

@jiyarong the thing is that this error appears after network reconnect on ws.send(). This happens only on Android. iOS is good. The correct behaviour must be like on iOS: the application must go to websocket onError and there must be graceful websocket recreation. But the Android version just crashes.

@jiyarong I have the following code in place:

    try {this.ws.close()} catch (e) {}
    this.ws = undefined

but I can still get the error.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

Any update?

@LaloHao maybe this one is relevant for you? https://github.com/facebook/react-native/issues/3346

It looks like this issue is no longer tracking a particular failure. If you run into a WebSocket error like the above in 0.55 or newer, please open a new issue with more information.

Was this page helpful?
0 / 5 - 0 ratings