question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strict mode: Untagged network socket

See original GitHub issue

Describe the bug The underlying network socket managed by OkHttp isn’t tagged according to policy. Strict mode complains about it. While it’s a longstanding OkHttp issue other libraries apply a fixed tag.

To Reproduce Enable strict mode in an app’s Application class during onCreate():

if (BuildConfig.DEBUG) {
    StrictMode.enableDefaults()
}

Expected behavior A clear and concise description of what you expected to happen.

Logs/Screenshots

2019-08-19 16:00:08.151 17277-17363/foo 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:2023)
        at com.android.server.NetworkManagementSocketTagger.tag(NetworkManagementSocketTagger.java:82)
...
        at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:245)
...

Library version 0.6.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
colinrtwhitecommented, Aug 22, 2019

Unfortunately, I don’t think there’s a way to show a message for this issue without requiring the user to install a custom socket factory. I’m going to close this issue for the moment + post the work-around as applied to Coil. If we get duplicate Github issues, we can point users back to this thread.

Here’s the suggested work-around applied to Coil:

class TaggedSocketFactory(private val delegate: SocketFactory = getDefault()) : SocketFactory() {

    override fun createSocket(): Socket {
        val socket = delegate.createSocket()
        return configureSocket(socket)
    }

    override fun createSocket(host: String, port: Int): Socket {
        val socket = delegate.createSocket(host, port)
        return configureSocket(socket)
    }

    override fun createSocket(
        host: String,
        port: Int,
        localAddress: InetAddress,
        localPort: Int
    ): Socket {
        val socket = delegate.createSocket(host, port, localAddress, localPort)
        return configureSocket(socket)
    }

    override fun createSocket(host: InetAddress, port: Int): Socket {
        val socket = delegate.createSocket(host, port)
        return configureSocket(socket)
    }

    override fun createSocket(
        host: InetAddress,
        port: Int,
        localAddress: InetAddress,
        localPort: Int
    ): Socket {
        val socket = delegate.createSocket(host, port, localAddress, localPort)
        return configureSocket(socket)
    }

    private fun configureSocket(socket: Socket): Socket {
        TrafficStats.tagSocket(socket)
        return socket
    }
}

val imageLoader = ImageLoader(context) {
    val client = OkHttpClient.Builder().socketFactory(TaggedSocketFactory()).build()
    okHttpClient(client)
}

// If you use the Coil singleton...
Coil.setDefaultImageLoader(imageLoader)
0reactions
Kolyallcommented, Apr 20, 2022

See https://stackoverflow.com/a/36840978/2425851 By this answer we need also before TrafficStats.tagSocket(socket) call TrafficStats.setThreadStatsTag . And call untagSocket after transfer of data

TrafficStats.setThreadStatsTag(0xF00D);
TrafficStats.tagSocket(outputSocket);
// Transfer data using socket
TrafficStats.untagSocket(outputSocket);

How to make it with Coil?

Read more comments on GitHub >

github_iconTop Results From Across the Web

StrictMode java.lang.Throwable: Untagged socket detected
setThreadSocketTag () to track all network usage at android.os.StrictMode.onUntaggedSocket(StrictMode.java:2124) at com.android.server.
Read more >
Android O StrictMode: Untagged socket detected #3537 - GitHub
Throwable: Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage at android.os.StrictMode.
Read more >
UntaggedSocketViolation - Android Developers
android.os.strictmode.Violation. ↳, android.os.strictmode.UntaggedSocketViolation. Summary. Inherited methods. From class android.os.strictmode.Violation ...
Read more >
core/java/android/os/strictmode/UntaggedSocketViolation.java
package android.os.strictmode;. public final class UntaggedSocketViolation extends Violation {. /** @hide */ ... + "track all network usage");.
Read more >
Loading Error "Could not connect with Form Server" on ...
UntaggedSocketViolation : Untagged socket detected; use TrafficStats.setThreadSocketTag() to track all network usage at android.os.StrictMode ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found