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

NetworkOnMainThreadException during disconnect() #21

Closed
jhansche opened this issue Mar 23, 2017 · 2 comments
Closed

NetworkOnMainThreadException during disconnect() #21

jhansche opened this issue Mar 23, 2017 · 2 comments

Comments

@jhansche
Copy link
Contributor

jhansche commented Mar 23, 2017

The act of disconnecting the client attempts to perform a network request on the main thread, leading to a crash:

Caused by android.os.NetworkOnMainThreadException
       at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
       at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java:1115)
       at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:1110)
       at com.firebase.tubesock.WebSocket.closeSocket(WebSocket.java:223)
       at com.firebase.tubesock.WebSocket.close(WebSocket.java:197)
       at com.parse.TubeSockWebSocketClient.close(TubeSockWebSocketClient.java:65)
       at com.parse.ParseLiveQueryClientImpl.disconnect(ParseLiveQueryClientImpl.java:102)

All other network-related tasks are done in a background TaskExecutor. But disconnect(), which calls WebSocket.close(), is not happening on that background thread.

@jhansche jhansche changed the title NetworkOnMainThreadException during close() NetworkOnMainThreadException during close() Mar 23, 2017
@jhansche jhansche changed the title NetworkOnMainThreadException during close() NetworkOnMainThreadException during disconnect() Mar 23, 2017
@rogerhu
Copy link
Contributor

rogerhu commented Mar 23, 2017

One more reason I'd use the OkHttp version. It handles its own threading.

https://medium.com/square-corner-blog/web-sockets-now-shipping-in-okhttp-3-5-463a9eec82d1

@jhansche
Copy link
Contributor Author

Fair enough. But if that's going to be a longer discussion or longer implementation time, I'm just going to work on fixing it in TubeSock for now.

jhansche pushed a commit to wondrous-io/ParseLiveQuery-Android that referenced this issue Mar 23, 2017
…utor

TubeSock already uses a background thread for `open()`, but it keeps the
`close()` call on the calling thread. So `open()` is safe on the main thread,
but `close()` is not.
jhansche pushed a commit to wondrous-io/ParseLiveQuery-Android that referenced this issue Mar 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants