smartdevicelink / sdl_java_suite

SmartDeviceLink libraries for Android, Java SE, and Java EE
BSD 3-Clause "New" or "Revised" License
187 stars 171 forks source link

NetworkOnMainThreadException can be thrown in SdlProxyBase.startService() and .dispose() #629

Closed shiniwat closed 4 years ago

shiniwat commented 7 years ago

I happened to notice NetworkOnMainThreadException was thrown on my Android device (Google Pixel; Android 8.0). The call stack looks as follows: === case 1: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:108) at java.net.SocketOutputStream.write(SocketOutputStream.java:153) at com.smartdevicelink.transport.TCPTransport.sendBytesOverTransport at com.smartdevicelink.transport.SdlTransport.sendBytes at com.smartdevicelink.SdlConnection.SdlConnection.onProtocolMessageBytesToSend at com.smartdevicelink.protocol.AbstractProtocol.handlePacketToSend at com.smartdevicelink.protocol.WiProProtocol.StartProtocolService at com.smartdevicelink.SdlConnection.SdlConnection.startService

=== case 2: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:108) at java.net.SocketOutputStream.write(SocketOutputStream.java:153) at com.smartdevicelink.transport.TCPTransport.sendBytesOverTransport at com.smartdevicelink.transport.SdlTransport.sendBytes at com.smartdevicelink.SdlConnection.SdlConnection.onProtocolMessageBytesToSend at com.smartdevicelink.protocol.AbstractProtocol.handlePacketToSend at com.smartdevicelink.protocol.WiProProtocol.EndProtocolService at com.smartdevicelink.SdlConnection.SdlConnection.closeConnection at com.smartdevicelink.SdlConnection.SdlConnection.unregisterSession at com.smartdevicelink.SdlConnection.SdlSession.close at com.smartdevicelink.proxy.SdlProxyBase.cleanProxy at com.smartdevicelink.proxy.SdlProxyBase.dispose

While NetworkOnMainThreadException may be specific to TCPTransport as you see the call stack above, we should use worker thread for any communication with HU regardless of the transport. This issue can happen if a) SdlProxyBase.startService() or .dispose() are called in main thread, and b) StrictMode is turned on the device.

shiniwat commented 7 years ago

I have a quick fix on this at this commit.

theresalech commented 4 years ago

This class has been deprecated and will be removed in the next major release (October, 2020). Therefore, we do not plan on creating a fix for this issue.