StrictMode java.lang.Throwable:检测到未标记的套接字 [英] StrictMode java.lang.Throwable: Untagged socket detected

查看:1532
本文介绍了StrictMode java.lang.Throwable:检测到未标记的套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启用StrictMode后,我刚刚开始遇到此异常:

With StrictMode enabled I just started getting this exception:

java.lang.Throwable:检测到未标记的套接字;使用TrafficStats.setThreadSocketTag()跟踪所有网络使用情况

java.lang.Throwable: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage

推荐答案

有两种方法可以处理此异常.首先,您必须检查堆栈跟踪,并确保它是您的代码报告违规情况.例如,查看以下跟踪.

There are a couple of ways to handle this exception. First, you have to inspect the stack trace and make sure it is your code reporting the violation. For example, have a look at the following trace.

D/StrictMode: StrictMode policy violation: android.os.strictmode.UntaggedSocketViolation: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage
    at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2124)
    at com.android.server.NetworkManagementSocketTagger.tag(NetworkManagementSocketTagger.java:82)
    at libcore.io.BlockGuardOs.tagSocket(BlockGuardOs.java:52)
    at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:372)
    at libcore.io.ForwardingOs.socket(ForwardingOs.java:217)
    at libcore.io.IoBridge.socket(IoBridge.java:658)
    at java.net.PlainSocketImpl.socketCreate(PlainSocketImpl.java:128)
    at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:128)
    at java.net.ServerSocket.createImpl(ServerSocket.java:306)
    at java.net.ServerSocket.getImpl(ServerSocket.java:259)
    at java.net.ServerSocket.bind(ServerSocket.java:377)
    at java.net.ServerSocket.<init>(ServerSocket.java:237)
    at java.net.ServerSocket.<init>(ServerSocket.java:128)
    at com.java42.android03.binder.peer.messaging.J42PM_ServerConnection.run(J42PM_ServerConnection.java:52)
    at com.java42.base.components.utility.impl.Utility_Thread$1.run(Utility_Thread.java:137)
    at java.lang.Thread.run(Thread.java:919)
    at com.java42.utility.base.J42Thread.run(J42Thread.java:38)

违反的应用调用为:

J42PM_ServerConnection.run(J42PM_ServerConnection.java:52)

接下来,看看该代码:

public void run() {
    try (ServerSocket serverSocket = new ServerSocket(port)) {
        this.serverSocket = serverSocket;
        CallerId.identifyBasic("J42PSC0179D: listening Inbound....");
        while (!Thread.currentThread().isInterrupted()) {
        ...

由于这是服务器端循环,并且线程ID在服务器进程的生命周期内不会更改,因此可以通过使用当前线程ID向TrafficStats.setThreadStatsTag()添加调用来解决此冲突.

Since this is the server-side loop, and the thread ID does not change for the life of server process, you can fix the violation by adding a call to TrafficStats.setThreadStatsTag() using the current thread id.

public void run() {
    TrafficStats.setThreadStatsTag((int) Thread.currentThread().getId()); // <---
    try (ServerSocket serverSocket = new ServerSocket(port)) {
        this.serverSocket = serverSocket;
        CallerId.identifyBasic("J42PSC0179D: listening Inbound....");
        while (!Thread.currentThread().isInterrupted()) {
        ...

让我们看看另一条痕迹:

Let's have a look at another trace:

D/StrictMode: StrictMode policy violation: android.os.strictmode.UntaggedSocketViolation: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage
    at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2124)
    at com.android.server.NetworkManagementSocketTagger.tag(NetworkManagementSocketTagger.java:82)
    at libcore.io.BlockGuardOs.tagSocket(BlockGuardOs.java:52)
    at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:372)
    at libcore.io.ForwardingOs.socket(ForwardingOs.java:217)
    at libcore.io.IoBridge.socket(IoBridge.java:658)
    at java.net.PlainSocketImpl.socketCreate(PlainSocketImpl.java:128)
    at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:128)
    at java.net.Socket.createImpl(Socket.java:489)
    at java.net.Socket.<init>(Socket.java:446)
    at java.net.Socket.<init>(Socket.java:218)
    at com.java42.android03.binder.peer.messaging.J42PM_ClientConnection.run(J42PM_ClientConnection.java:54)
    at com.java42.android03.binder.peer.messaging.J42PM_Client$2.run(J42PM_Client.java:86)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)

与第一个示例一样,您可以确定违反的应用程序调用:

As in the first example, you identify the app call that is in violation:

J42PM_ClientConnection.run(J42PM_ClientConnection.java:54)

接下来,看看该代码:

InetSocketAddress socketAddress = serviceResolver.getSocketAddress();
    connection = new J42PM_ClientConnection(socketAddress, responseHandler);
    executorService.execute(new Runnable() {
        @Override
        public void run() {
        connection.run();
        ...

请注意,此代码使用执行程序服务来运行客户端连接.在创建和销毁连接时,线程ID在该过程的生命周期内将发生变化.我不确定这是否会使流量报告产生偏差,但我认为最好在此时使用恒定值来跟踪使用情况.选择一些唯一的号码.使用文件的行号是一个简单的选择.

Notice this code uses an executor service to run the client-side connection. The thread ID will change during the life of the process as connections are created and destroyed. I'm not certain if this will skew traffic reports but I think it best to track usage at this point using a constant value. Pick some unique number. Using the line number of the file is an easy choice.

    InetSocketAddress socketAddress = serviceResolver.getSocketAddress();
    connection = new J42PM_ClientConnection(socketAddress, responseHandler);
    executorService.execute(new Runnable() {
        @Override
        public void run() {
        TrafficStats.setThreadStatsTag(42); // <--
        connection.run();
        ...

最后一条评论,如果在堆栈跟踪中没有看到对代码的调用,则您可以做很多事情来制止这种违规行为.问题出在另一个图书馆,除非您可以与图书馆所有者联系,否则请忽略违规行为.

One last comment, if you don't see a call to your code in the stack trace, there is not much you can do to stop the violation. The problem is in another library and unless you can contact the library owner, simply ignore the violation.

这篇关于StrictMode java.lang.Throwable:检测到未标记的套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆