2

I am using OkHttp (first the original verison, then I upgraded to OkHttp3), some users of my App have been reporting significant battery life loss when the App isn't running.

I ran a profiler and this is the result:

profiler of App

As you can see, Okio Watchdog is running the whole time. At roughly the halfway point, my App is fully in the background. There are no HTTP tasks taking place at this point in time. I started profiling after the last HTTP task ended.

Is it normal that the Watchdog runs throughout like that? If so, am I right in assuming this thread is causing a lot of battery waste? If it isn't normal, could something like a leaked Context keep the Watchdog running?

The Watchdog code runs here, it seems like to runs without a termination condition:

private static final class Watchdog extends Thread {
    public Watchdog() {
        super("Okio Watchdog");
        setDaemon(true);
    }

    public void run() {
        while (true) {
            try {
                AsyncTimeout timedOut = awaitTimeout();

                // Didn't find a node to interrupt. Try again.
                if (timedOut == null) continue;

                // Close the timed out node.
                timedOut.timedOut();
            } catch (InterruptedException ignored) {
            }
        }
    }
}
Knossos
  • 15,802
  • 10
  • 54
  • 91

3 Answers3

4

Looks like a severe & unexpected bug in Okio. I'll try to reproduce & fix. If you're able to produce this consistently, please comment on this bug!

https://github.com/square/okio/issues/185

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
2

For me it was caused by proguard's optimization. After some investigation - see the okio issue linked above - a workaround (if not final fix?) is to disable optimization or add this to your proguard-rules.pro:

-optimizations !method/marking/static,!method/removal/parameter,!code/removal/advanced
NeilS
  • 625
  • 7
  • 13
1

I find a NOTE in this manual

Note: the configuration specifies that none of the methods of class '...' have any side effects

Your configuration contains an option -assumenosideeffects to indicate that the specified methods don't have any side effects. However, the configuration tries to match all methods, by using a wildcard like "*;". This includes methods from java.lang.Object, such as wait() and notify(). Removing invocations of those methods will most likely break your application. You should list the methods without side effects more conservatively. You can switch off these notes by specifying the -dontnote option.

You should specify the method name in the -assumenosideeffects block.

I add this comment at https://github.com/square/okio/issues/185#issuecomment-220520926

Community
  • 1
  • 1
Bill Lv
  • 71
  • 1
  • 3