[Android][Bug] 主线程在 PAGPlayer.getComposition(Native Method) 中发生死锁永久卡死
【版本信息】
libpag 版本:4.5.98(4.5.84 也稳定复现)
环境信息 libpag 版本:4.5.98(4.5.84 也稳定复现) 系统版本:Android 13 / Android 14 (ARM64-v8a) 硬件芯片:高通骁龙 8295(车机座舱芯片平台) 素材特征:内嵌视频流、依赖 HardwareDecoder 高通硬件解码器的 .pag 素材
问题描述
在 Android UI 主线程调用 pagView.getComposition() 时,底层的 Native 方法 org.libpag.PAGPlayer.getComposition(Native Method) 会发生不可恢复的死锁挂起。
主线程陷入永久阻塞,系统卡顿监控记录卡死时间从 450ms 持续单调增长至 338,135ms(超 5 分钟未恢复),主线程消息队列彻底饥饿,引发严重 ANR。
分析推断:PAGPlayer::getComposition() 在 Native 内部需要获取 locker 互斥锁;当后台渲染线程或硬件解码线程正在执行渲染/硬解调度或处于超时异常时持有该锁未释放,主线程调用该属性接口直接被死锁卡死。
关键日志与卡顿堆栈
-
主线程发起 PAG 播放动效(TID 4019 为 Android 主线程) text 10-08 13:23:26.652955 4019 4019 I tag = VRService VpaWindowManager: show request screen=1, resolvedScreen=1, state=1, isAsrRecognizing=true, pcmAudioEnabled=false, text=, volume=0.0 10-08 13:23:26.657375 4019 4019 E tag = VRService VpaBackgroundLayer: play assetName=vpa/flowing_light_normal.pag, repeatCount=-1
-
主线程卡死在 Native getComposition 堆栈(初始卡顿 450ms) text 10-08 13:23:27.103880 4019 6079 W JankMonitor: We've detected that the current program:com..ai.voice is lagging!450.180937ms Main thread=Thread[main,5,main], its stackTrace is: [org.libpag.PAGPlayer.getComposition(Native Method), org.libpag.PAGView.getComposition(SourceFile:1), com.voice.ui.aitoast.animation.PagLayerPlayer.play(PagLayerPlayer.kt:115), com.voice.ui.aitoast.animation.PagLayerPlayer.playLoop(PagLayerPlayer.kt:43), com.voice.ui.aitoast.animation.VpaPagAnimator.playBackgroundLoop(VpaPagAnimator.kt:193), com.voice.ui.aitoast.animation.VpaPagAnimator.play(VpaPagAnimator.kt:82), com.voice.ui.aitoast.view.VpaView.render(VpaView.kt:65), com.voice.ui.aitoast.VpaWindowManager$VpaWindow]
-
锁永久未释放,主线程卡死时间持续递增(从 450ms 涨到 338,135ms,共 645 起无一次恢复) text 10-08 13:23:29.207 4019 6079 W JankMonitor: We've detected that the current program:com.ai.voice is lagging!2553.12ms ... 10-08 13:25:32.317118 4019 6079 W JankMonitor: We've detected that the current program:com.ai.voice is lagging!125663.455525ms ... 10-08 13:29:04.821034 4019 6079 W JankMonitor: We've detected that the current program:com.ai.voice is lagging!338135.192310ms
-
辅助证据:卡死前 19 秒同进程 Native 取消操作已有长时间阻塞 text 10-08 13:23:07.412 4019 6079 W JankMonitor: We've detected that the current program:com.ai.voice is lagging!300.12ms Main thread stackTrace: [org.libpag.PAGAnimator.cancel(Native Method), org.libpag.PAGView.stop(SourceFile:1), ...]
感谢你提供的详细日志和分析,非常有帮助。我们定位到了问题方向:卡死的源头不在 getComposition(),它是受害者。以下是分析:
根因分析
PAGView默认异步渲染(isSync() == false),flush()在后台渲染线程执行,全程持有渲染锁(rootLocker);- 素材内嵌视频序列,渲染时 GPU 纹理实例化会同步等待后台硬件解码任务完成,而这个等待没有超时;
MediaCodec的stop/release/configure/start等平台调用内部以阻塞方式等待 codec 消息处理线程返回,上层无法打断。当底层 media server / vendor codec 进入坏状态时,这些调用会无限期阻塞——这是 Android 平台的已知问题类型(Google ExoPlayer 团队也官方确认过同类 ANR:google/ExoPlayer#4352),我们此前也在其他案例中观测到过硬件解码器挂死并做过部分防护(#3554);- 于是后台渲染线程永久等待解码线程(渲染锁不释放)→ 主线程调用
getComposition()抢锁永久阻塞 → ANR 不可恢复。你日志中 19 秒前PAGAnimator.cancel阻塞 300ms 是锁竞争的早期信号。
你使用的 4.5.98 已包含 #3554、#3730 的防护但未能阻止此问题,说明现有防护确实无法覆盖 codec 永久挂死的场景,我们会在库层面评估进一步修复(将 codec 创建/销毁移出关键路径,先例:#3694)。
为了加快定位,请协助提供:
- 可复现的 Demo +
vpa/flowing_light_normal.pag素材(最重要):你提到「稳定复现」,如果 demo 在其他 8295 设备上也能复现,我们可以直接抓取完整线程栈分析; - 「稳定复现」的具体条件:是每次播放必现,还是特定操作序列(反复 stop/play、前后台切换等)后必现?大约多久触发?
- 如暂无法提供 demo,请在 ANR 发生时抓一份包含所有线程的 native trace(
/data/anr/下的 trace 文件,或adb shell debuggerd -b <pid>),重点确认解码线程卡在哪个AMediaCodec_*调用上; - 可临时尝试
VideoDecoder.SetMaxHardwareDecoderCount(0)强制软解,验证问题是否随硬件解码消失。