leakcanary icon indicating copy to clipboard operation
leakcanary copied to clipboard

Running heap analysis in separate process does not invoke custom OnLeakFoundCallback callback

Open oliviernotteghem opened this issue 4 years ago • 4 comments

Description

Not sure if this is the expected behavior or not. I was expected to have background process trigger callback on the main process with leak analysis data.

Steps to Reproduce

  1. Follow https://square.github.io/leakcanary/recipes/#running-the-leakcanary-analysis-in-a-separate-process
  2. Use a app that has custom OnLeakFoundCallback callback
  3. Make sure dumpHeapWhenDebugging is set to true if using debugger
  4. Verify once that callback is not invoked once analysis complete (via debugger breakpoint or logcat)

Expected behavior:

Callback to be invoked in main process.

Version Information

  • LeakCanary version: 2.2
  • Android OS version: API 29
  • Gradle version: n/a

oliviernotteghem avatar Mar 30 '20 06:03 oliviernotteghem

Thanks for the bug report, and sorry for the delay.

Currently, the callback (OnHeapAnalyzedListener) must be set in the background process and will be invoked in the background process. Then you'll need to send that result back to the main process your own way.

To be honest, I didn't think this through too much when I ripped out the old code that used a second process. I think the proper approach here would be to provide a default callback in the background process that then kicks things back to the main process and invokes the main process defined callback.

We should do that, and also fully document how to set up LeakCanary with a background process. Contributions welcome!

pyricau avatar Apr 21 '20 23:04 pyricau

Hi guys, I'm new over here, just trying to contribute since I use LeakCanary a lot... and I was just looking into this since it's labeled as "good first issue" 😊

However, It's not clear to me how to reproduce this behavior...

I did setup leakcanary-android-process artifact at v2.2 on an app, and then added this custom config inside my Application#onCreate:

        LeakCanary.config = LeakCanary.config.copy(
            dumpHeapWhenDebugging = true,
            onHeapAnalyzedListener = object : OnHeapAnalyzedListener {
                override fun onHeapAnalyzed(heapAnalysis: HeapAnalysis) {
                    Log.d("SPELLNET_LC_HEAP", "triggered p")
                }
            }
        )

And the log was made on logcat after dump analysis was completed, so I guess everything worked as expected... (?)

tdopires avatar Oct 11 '20 15:10 tdopires

@tdopires : thanks for looking into this. To repro this, you need to make set leak canary to do the analysis on separate thread (it's a config option).

oliviernotteghem avatar Oct 12 '20 17:10 oliviernotteghem

you need to make set leak canary to do the analysis on separate thread

There's no option for separate thread, but you can have it do the analysis in a separate process: https://square.github.io/leakcanary/recipes/#running-the-leakcanary-analysis-in-a-separate-process

Once you do that, things still work and the analysis will run in that separate process. And then, the heap analysis listener will be called in that separate process. And that's the crux of the issue, ie the callback is called in the background process and not the main process. The default callback when in a separate process should send the notification back to the main process callback

pyricau avatar Oct 12 '20 20:10 pyricau