Repository navigation
Conversation
a4f1312 to
caa3c77
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3822 +/- ##
==========================================
+ Coverage 83.42% 83.70% +0.28%
==========================================
Files 748 762 +14
Lines 105944 113586 +7642
Branches 29587 31607 +2020
==========================================
+ Hits 88386 95081 +6695
- Misses 10766 11230 +464
- Partials 6792 7275 +483 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
请问这个问题修复在哪个版本上了 |
目前修改还没合入,合入之后我们会发布新版本并告知您的 |
caa3c77 to
a61d644
Compare
| namespace pag { | ||
| /** | ||
| * Carries the pixel buffer produced by one VideoToolbox decode call. VideoToolbox may invoke the | ||
| * output callback on another thread and does not promise that it has returned by the time |
There was a problem hiding this comment.
建议把根因依据写进代码,否则后续维护者按 SDK 文档读一遍,很可能把堆上 context 当成多余防御删掉。VTDecompressionSession.h 对 decodeFlags 的说明是:两个 flag 都清零时,回调一定在 VTDecompressionSessionDecodeFrame 返回前被调用;而本仓库传的就是 0。真正支持迟到投递的是 FinishDelayedFrames 的 may-return-before-all-frames-emitted,以及 issue #3780 里的迟到投递实测(8 次),这两点目前只存在于 issue 讨论里。建议此处补一句实测依据,并说明为什么 infoFlags 这个栈出参不受影响。堆上 context 本身是更彻底的修法,不建议回退。
| // return first, and only invalidate underneath it once it is really stuck, because unblocking a | ||
| // stuck decode is what this method exists for. | ||
| const bool hasDecodeLocker = | ||
| decodeLocker.try_lock_for(std::chrono::milliseconds(MAX_INVALIDATE_WAIT_TIME_MS)); |
There was a problem hiding this comment.
这里的等待发生在 willResignActive 观察者里,而该循环(74-79 行)是在持有 GetActiveDecodersMutex 的情况下逐个 decoder 调用 invalidateSession,每个最多 200ms,N 个即 N×200ms;didBecomeActive 与它同处一条串行队列(maxConcurrentOperationCount = 1),会推迟 appInactive 复位,让后续解码持续返回 Error,触发 VideoReader 的 destroyVideoDecoder + factoryIndex++ 降级。同一条锁也是构造/析构必须持有的,会让解码线程在 device.locker 内被额外阻塞,与这段代码尽快让 worker 释放 device.locker 的目的相反。建议:锁内只做快照、锁外 invalidate(需配生命周期令牌避免与析构并发),或整个循环共用一个 now+200ms 预算,让总耗时封顶。
| cleanResources(); | ||
| // Released only after the session has been invalidated and released, so no callback delivered by | ||
| // VideoToolbox can write into them anymore. | ||
| for (auto* context : staleContexts) { |
There was a problem hiding this comment.
这里的释放与 staleContexts 的保活理由互相矛盾:保活是为了防失败后仍回调(HardwareDecodeContext.h:67-69),而这里在 Invalidate + CFRelease 之后立即 delete,又假设此时不可能再有回调。若迟到回调真能跨越 session 释放,这里就是把栈 UAF 换成堆 UAF(complete 会锁一个已释放的 mutex);若不能,整个 staleContexts 机制就只是泄漏。另外 macOS 侧没有 FinishDelayedFrames(mac:52-53)却做同样的释放,两平台必有一侧不成立。建议二选一:用 VTDecompressionSessionWaitForAsynchronousFrames 建立明确屏障后统一释放,或信任失败无回调查文档、直接立即释放并删掉 staleContexts。
| if (callbackDone) { | ||
| shouldDelete = true; | ||
| } else if (!callbackExpected) { | ||
| abandoned = true; | ||
| } |
There was a problem hiding this comment.
当 DecodeFrame 返回 noErr 但回调始终不到(session 先被 invalidate、帧被丢弃等)时,这里既不删除也不上报 abandoned,此后没有任何指针能到达该 context,会永久泄漏;这也与错误分支不信任 VT 承诺的立场不一致。类注释(29-31 行)的 whichever finishes last releases it 在 abandoned 路径上同样不成立,两侧都不释放,靠 decoder 兜底。建议改为 decoder 统一持有所有在飞 context,session 拆除后统一回收。
|
|
||
| void DidDecompress(void*, void* sourceFrameRefCon, OSStatus status, VTDecodeInfoFlags, | ||
| CVImageBufferRef pixelBuffer, CMTime, CMTime) { | ||
| auto* context = static_cast<HardwareDecodeContext*>(sourceFrameRefCon); |
There was a problem hiding this comment.
DidDecompress 在本文件 97-108 行与 ios 175-186 行逐字节相同,且都是 namespace pag 下的外部链接函数:目前靠恰好一致满足 ODR,任何单边修改都会变成静默的 ODR 违规。本次还新增了 4 份相同的 stale 回收循环(ios 143/243、mac 65/158)和 2 份解码交接块。建议把 DidDecompress 与 abandoned 池挪到本 PR 已创建的 cocoa/private/HardwareDecodeContext(顺带补一个 .mm),两个 decoder 头里的裸 vector 也可一并去掉。两个文件历史重复的 200 行不必在本 PR 处理。
| * Aborts any in-progress synchronous decompression by invalidating the underlying VT session. | ||
| * Safe to call from any thread. Used by the willResignActive observer to unblock the worker | ||
| * thread so it can release device.locker before the main thread enters EAGLDevice::finish(). | ||
| * A decode that is still running is given a short grace period to return first, the session is |
There was a problem hiding this comment.
注释与实现不符:实际是无条件调用 VTDecompressionSessionInvalidate(:99-102),hasDecodeLocker 只决定要不要解锁;按 PR 描述里探针的结论(并发 invalidate 10/10 崩溃),解码卡死超过 200ms 时仍会走回这条崩溃路径,这里只是把窗口收窄。另外既没写最多阻塞 200ms,Safe to call from any thread 在改后也不成立(从解码线程自身调用会对已持有的 timed_mutex 做 try_lock_for,属 UB;与析构的免竞态还依赖调用方持有 GetActiveDecodersMutex)。建议按实际语义重写,并说明 200ms 来自哪个预算。
| * whichever finishes last releases it, which keeps the storage and the buffer valid until both | ||
| * sides are done. | ||
| */ | ||
| class HardwareDecodeContext { |
There was a problem hiding this comment.
命名与接口建议:该类只服务 VideoToolbox(持有 CVPixelBufferRef,仅因 DecodeFrame 的 refcon 约定而存在),HardwareDecodeContext 与 HardwareDecoder 概念容易混淆,建议 VTDecodeOutputContext;complete 建议改为 deliverBuffer/abandon 自解释;takeOutput(bool, bool*) 用可空返回值加出参加自删编码了 4 种状态,建议返回一个小 struct(buffer + 是否 abandoned)。
…date sessions off the registry lock via a lifecycle token.
|
@shlzxjp 感谢细致评审,已按建议整体收敛为单一所有权模型(commit 42fc773),逐条回应: 1(HardwareDecodeContext.h:27,根因依据) 类注释已补:VTDecompressionSession.h 明确 flags=0 回调必在 4 + 2(永久泄漏 / willResignActive 循环)→ 统一所有权 + 锁外令牌
3(staleContexts 矛盾)→ 显式屏障(选"WaitForAsynchronousFrames 屏障后统一释放"):reset/析构在 5(ODR / 重复) 6(ios HardwareDecoder.h:51 注释与实现不符) 文档重写为真实语义:无条件 7(命名/接口) 类改名 验证: |
…e matches its header forward declaration.
背景
关联 issue:#3780
线上 iOS 崩溃发生在
pag::HardwareDecoder::~HardwareDecoder()内部,栈顶是std::mutex::lock()加一段无法符号化的帧。社区提供的反汇编指向了问题的位置:根因
onDecodeFrame()把栈上局部变量outputPixelBuffer的地址作为sourceFrameRefCon传给VTDecompressionSessionDecodeFrame。flags = 0时回调一定在VTDecompressionSessionDecodeFrame返回前被调用(见 VTDecompressionSession.h),但VTDecompressionSessionFinishDelayedFrames允许在所有帧吐出前返回,被 session 推迟的帧会在这之后才回调(issue #3780 实测 8 次迟到)。析构函数里的这次冲刷会让回调落在早已返回的栈帧上,写入的CVPixelBufferRef(堆指针)覆盖了当前调用链保存的帧指针,后续mov sp, x29把 sp 置成堆地址,于是删除析构器取this时崩溃。4.5.97/98 引入的 fallback 逻辑让解码器销毁重建更频繁,而每次析构都会调一次冲刷,命中窗口随之变大。
改动
新增
src/platform/cocoa/private/HardwareDecodeContext.{h,mm}(iOS / macOS 共用),统一所有权模型:VTDecodeOutputContext:回调把CVPixelBuffer写进上下文而非调用方栈槽,彻底消除原始崩溃。上下文由VTDecodeContextPool持有,回调只写数据、从不删除对象。onDecodeFrame()内就地回收(reclaimResolved()),延迟帧 / 被丢弃帧 / 失败后迟到的上下文,在resetVideoToolBox()或析构的 teardown 屏障之后统一回收(reclaimAll())。消除了旧设计里 noErr-无回调的永久泄漏,以及"Invalidate 后立即 delete"的堆 UAF。VTDecompressionSessionInvalidate之前统一调用VTDecompressionSessionWaitForAsynchronousFrames(mac 侧原本缺失,已补),保证回收时 VideoToolbox 不再有在飞回调。DidDecompress与上下文 / 池下沉到共享.mm(单一定义,消除两份逐字节相同函数的 ODR 风险),两个 decoder 头里的裸vector与重复回收循环删除。iOS 侧
willResignActive加固(与本次崩溃不同路径,并发 invalidate 可在本地稳定复现 VT 内部崩溃):weak_ptr<DecoderToken>;观察者在GetActiveDecodersMutex内只做快照,锁外逐个 invalidate,整批共享一个now + 200msdeadline —— 总等待封顶 200ms(不再 N×200ms),且不再在持有注册表锁时阻塞解码器的构造 / 析构。令牌的 mutex 保证观察者要么作用于存活 decoder、要么见decoder == nullptr跳过,与析构无竞态。验证
PAGFullTest_OpenGL构建通过;PAGSequenceTest、VideoSequenceFallbackTest、PAGDiskCacheTest、MultiThreadCase、PAGANRRegressionTest共 43 例全部通过。CVPixelBuffer引用计数无多释放 / 少释放。已知限制
本地环境(macOS / iOS 模拟器)无法复现"VideoToolbox 延迟帧"这一触发前提(连续 300 帧回调全部同步),因此线上崩溃的最终确认仍需报告者在必现的真机上验证。