Support for Android StrictMode
VincentJian opened this issue · 2 comments
When enable Android StrictMode with detectNetwork
will show the following logs
java.lang.Throwable: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage
at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2012)
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.PlainDatagramSocketImpl.datagramSocketCreate(PlainDatagramSocketImpl.java:196)
at java.net.AbstractPlainDatagramSocketImpl.create(AbstractPlainDatagramSocketImpl.java:75)
at java.net.DatagramSocket.createImpl(DatagramSocket.java:356)
at java.net.DatagramSocket.<init>(DatagramSocket.java:258)
at java.net.DatagramSocket.<init>(DatagramSocket.java:215)
at org.minidns.source.NetworkDataSource.createDatagramSocket(NetworkDataSource.java:151)
......
I know it is a trivial issue when the StrictMode
is set to penaltyLog()
, but it is better that NetworkDataSource
can support this.
Currently I use the following workaround:
Create custom StrictModeNetworkDataSource
public final class StrictModeNetworkDataSource extends NetworkDataSource {
private static final int THREAD_STATS_TAG = 1;
public StrictModeNetworkDataSource() {
TrafficStats.setThreadStatsTag(THREAD_STATS_TAG);
}
}
and then register StrictModeNetworkDataSource with
ResolverApi.INSTANCE.getClient().setDataSource(new StrictModeNetworkDataSource());
Is there really a need to back support for this into the NetworkDataSource? If you use the default synchronous source, then the network I/O will happen within the thread calling into MiniDNS's API. So isn't it merely an issue that you, as user, should tag the thread?
Thanks for quick reply and you are right. I can just set the tag in the synchronous source and the warning logs go away.