tracks: hash the carried payload with -f streamhash, never through a PIPE pair

stream_signature() ran ffmpeg -f data - with stdout=PIPE, stderr=PIPE and drained
stdout first, reading stderr only after EOF. A source that floods stderr (a Matroska
with 1,612 'exceeds containing master element' errors = 219,020 bytes) fills the 64 KB
stderr buffer, ffmpeg blocks writing it, stdout never closes and the read loop blocks
for ever - 42 min at 2.1 s of CPU, and the supervisor's gate watchdog killed the same
item three times at 60 min each. p.wait(timeout=1800) cannot help: it is unreachable
until that loop ends.

-f streamhash -hash md5 - lets ffmpeg do the md5 itself (byte-identical: 5cb7483040...
both ways on that file, 2.3 s instead of a 48.9 MB pipe) and one
subprocess.run(capture_output=True, timeout=...) drains both fds on threads, so the
stderr flood cannot wedge and a wedged demuxer costs a bounded wait that returns None
(the gate then says 'could not hash payload', never 'payload differs').

Return shape (nb_packets, md5) and every caller unchanged. test_tracks 55/55,
including 'e2e mkv/mp4: copied streams are byte-identical' on the real fixtures.
This commit is contained in:
hoelee committed 2026-11-16 00:36:07 +08:00
1 parent 7c1a243aad
commit b1b7dbdff1
3 files changed
+73 -16

No files matched your search

+35
View File
@@ -446,6 +446,41 @@ work 盘只剩 3.9 GB → 队列守卫拒绝所有新项,两条 lane 空转 30
回归:`scripts/test_stage_cleanup.py` 6 项(另一 lane 不动 / 死残留删 / 在编 tag 保全 / 台账不动 / 字节数正确)。
## 第 14 个 bug:gate 阶段的 payload 指纹把 stderr 管道锁死(2026-11-15)
**症状**:控制面看板显示某条 CPU 车道「校验」42 分钟不动(`phase_s` 一直涨),引擎日志停在
`carry: …` 那一行之后再没写过字。看门狗按 `stalled in gate for 60 min with no I/O progress`
连杀三次,每条白烧一小时;同一个铁轨上还有 1,484 条待办。
**真因**:`tracks.stream_signature()` 用
`ffmpeg … -f data -` 把整个音频 payload(这个片 48.9 MB)打到 stdout,`stdout=PIPE,
stderr=PIPE`,然后 `while True: p.stdout.read(1 << 20)` 读干 stdout,**stderr 只有 EOF 之后
才读**。这个 Matroska 结构是坏的(1,612 处 `Element … exceeds containing master element`),
ffmpeg 往 stderr 吐了 219,020 字节 / 1,630 行;Windows 匿名管道缓冲 4–64 KB,写满后
ffmpeg 阻塞在写 stderr,stdout 永不 EOF,父进程永久阻塞。`p.wait(timeout=1800)` 的 timeout
在 stdout 读完之前根本用不上 —— 等于没有超时。
**证据(同一文件、同一 ffmpeg)**:
| 跑法 | 结果 |
|---|---|
| 引擎里的写法(stderr 挂管道不读) | 42 分钟/2.1 s CPU,永远不回 |
| 同命令 pipe → `md5sum`(stderr 走终端) | 11.7 s,`5cb7483040621df964779a9e0fb21d45` |
| `-f streamhash -hash md5 -` | 2.3 s,**同一 md5** |
**修**:`-f streamhash -hash md5 -` 让 ffmpeg 自己算 md5(payload 不再穿过本进程),
并改成**一次** `subprocess.run(capture_output=True, timeout=…)` —— `communicate()` 用线程
同时抽干 fd,stderr 再大也堵不住,超时就杀子进程并返回 `None`(闸门按「拿不到指纹」处理,
绝不会把一次超时误判成「payload 不同」)。包数仍由 ffprobe 给,返回形状
`(nb_packets, md5)` 不变。
**回归**:`python -m unittest test_tracks` 55/55(含 `e2e mkv/mp4: copied streams are
byte-identical`);真文件上 `stream_signature(src) == stream_signature(out)` 且 6.9 s 完成。
**教训(已写进 AGENTS §2 第 7 条)**:子进程的 stdout/stderr 要么都交给
`subprocess.run(capture_output=True)`,要么至少有一个落文件;**不许**在一个 PIPE 上边读边
等另一个 PIPE。任何"读干子进程输出"的循环都必须带自己的 deadline,不能指望 `wait(timeout=)`。
## 红线(用户明确要求过)
- **绝不删除/覆盖快照原档**(`#snapshot` 是唯一的干净档案)。