TakahikoKawasaki / nv-websocket-client

High-quality WebSocket client implementation in Java.
Apache License 2.0
2.03k stars 292 forks source link

Android O StrictMode: Untagged socket detected #150

Open XDex opened 6 years ago

XDex commented 6 years ago

Running into the same issue as reported in OkHttp with Android O default StrictMode enabled (StrictMode.enableDefaults())..

java.lang.Throwable: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage
                                                                        at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2010)
                                                                        at com.android.server.NetworkManagementSocketTagger.tag(NetworkManagementSocketTagger.java:78)
                                                                        at libcore.io.BlockGuardOs.tagSocket(BlockGuardOs.java:47)
                                                                        at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:310)
                                                                        at libcore.io.IoBridge.socket(IoBridge.java:667)
                                                                        at java.net.PlainSocketImpl.socketCreate(PlainSocketImpl.java:116)
                                                                        at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:98)
                                                                        at java.net.Socket.createImpl(Socket.java:484)
                                                                        at java.net.Socket.connect(Socket.java:614)
                                                                        at com.neovisionaries.ws.client.SocketConnector.doConnect(SocketConnector.java:110)
                                                                        at com.neovisionaries.ws.client.SocketConnector.connect(SocketConnector.java:81)
                                                                        at com.neovisionaries.ws.client.WebSocket.connect(WebSocket.java:2022)
                                                                        at io.github.sac.Socket.connect(Socket.java:447)
                                                                        at com.adobe.target.mobile.networking.NaradaClientService$3.run(NaradaClientService.java:253)
                                                                        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                                        at java.lang.Thread.run(Thread.java:764)
TakahikoKawasaki commented 6 years ago

I'm sorry I don't know technical details about StrictMode. However, if it is an Android-specific issue, nv-websocket-client cannot implement a workaround for the issue internally (unless reflection is used).

WebSocketListener.onThreadCreated() is called between after a thread is created and before the thread's start() method is called. You may be able to write a workaround in the callback method.

andrew-ld commented 2 years ago

it is not possible to do this because onThreadCreated is not called on the SocketRacer since it does not extend WebSocketThread but uses Thread directly

andrew-ld commented 2 years ago

https://github.com/andrew-ld/nv-websocket-client/commit/3a8156a2afdcf3dc4560da2ff9aaf149552a73ff workaround