Skip to content

ANR caused by main-thread work in onMalwareDetected (reflection + PackageManager calls) #214

Description

@ahan530

:::writing{variant=“standard” id=“92741”}
Title: ANR caused by main-thread work in onMalwareDetected (reflection + PackageManager calls)

Description

We encountered ANR issues caused by heavy work being executed on the main thread inside onMalwareDetected.

From the stack trace, the callback triggers:
• Reflection (Class.getMethod)
• OEM framework hooks (TranReflectHelper)
• PackageManager calls (getApplicationLabel)
• Resource loading

All of these are happening on the UI thread, leading to freezes and ANR.

Stack Trace (key part)

at java.lang.Class.getMethod
at com.transsion.hubcore.proxy.TranReflectHelper.getMethod
at android.app.ApplicationPackageManager.getApplicationLabel
at com.aheaditec.freerasp.ExtensionsKt.toPigeon
at com.aheaditec.freerasp.handlers.MethodCallHandler$sink$1.onMalwareDetected

Root Cause

Inside onMalwareDetected, this line performs heavy work on the main thread:

val pigeonPackageInfo = packageInfo.map { it.toPigeon(context) }

toPigeon(context) internally calls:
• PackageManager
• resource loading
• label resolution

This blocks the main thread.

Original Code

override fun onMalwareDetected(packageInfo: List) {
context?.let { context ->
val pigeonPackageInfo = packageInfo.map { it.toPigeon(context) }
talsecPigeon?.onMalwareDetected(pigeonPackageInfo) { result ->
result.getOrElse {
Log.e("MethodCallHandlerSink", "Result ended with failure")
throw it
}
}
}
}

Proposed Fix

Move heavy processing off the main thread:

override fun onMalwareDetected(packageInfo: List) {
val ctx = context ?: return
backgroundHandler.post {
val pigeonPackageInfo = packageInfo.map { it.toPigeon(ctx) }
mainHandler.post {
talsecPigeon?.onMalwareDetected(pigeonPackageInfo) { result ->
result.getOrElse {
Log.e("MethodCallHandlerSink", "Result ended with failure")
throw it
}
}
}
}
}

Expected Behavior
• No heavy work should run on the main thread
• Detection callbacks should be non-blocking

Impact
• UI freeze
• ANR on certain devices (especially Transsion / OEM-modified systems)

Please consider moving heavy operations (like toPigeon) off the main thread internally or documenting this requirement clearly.

Thanks.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions