diff --git a/AGENTS.md b/AGENTS.md index df7870c..c7faa71 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -37,6 +37,14 @@ There are **no credentials** in this project, so there is no `SECRETS.md`. 6. **Never write encode output straight to a network share.** Encode to local disk, verify, then copy the complete file across; a streaming `.part` write leaves a corrupt stub on any blip. If the destination is remote, keep the output local. +7. **Never read one child PIPE while leaving the other undrained.** `subprocess.run(..., + capture_output=True, timeout=…)` (or sending the noisy fd to a file) is the default; a + `while p.stdout.read():` loop with `p.stderr` still holding a PIPE will deadlock the moment + the child fills that pipe — measured 2026-11-15: a corrupt Matroska emitted 219 KB of + container errors, ffmpeg blocked writing stderr, the gate sat 42 min at 2.1 s CPU and the + supervisor killed the item three times (see `STATE.md` bug 14). `p.wait(timeout=)` does + NOT save you: it is unreachable until that read loop ends. A loop that drains a child owes + itself a deadline. ## 3. Layout diff --git a/STATE.md b/STATE.md index cb80c3a..ae88888 100644 --- a/STATE.md +++ b/STATE.md @@ -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` 是唯一的干净档案)。 diff --git a/scripts/tracks.py b/scripts/tracks.py index 26db767..7a444c7 100644 --- a/scripts/tracks.py +++ b/scripts/tracks.py @@ -38,7 +38,6 @@ same plan (`verify`). """ from __future__ import annotations -import hashlib import json import os import subprocess @@ -478,6 +477,17 @@ def stream_signature(path, sel, timeout=1800): `-bsf:a aac_adtstoasc` agree (955905a0 / 955905a0), and the decoded PCM agrees too. So for AAC the dump goes through that bitstream filter on BOTH sides - it strips ADTS where it exists and is a pass-through where it does not. + + One hard-won constraint on HOW it is computed (measured 2026-11-15, IMOG-159.mkv): the + payload must NOT be piped through this process. The previous version ran + `ffmpeg ... -f data -` with `stdout=PIPE, stderr=PIPE` and drained stdout in a read loop + while stderr was read only after EOF. A source that floods stderr - that Matroska emitted + 219,020 bytes / 1,630 container-error lines - fills the ~64 KB stderr pipe, ffmpeg blocks + writing it, the stdout pipe never closes and the loop blocks for ever: 42 minutes at 2.1 s + of CPU, and the supervisor's 60 min stall watchdog had already killed the same item twice. + `-f streamhash` lets FFMPEG do the md5 itself (same 32 hex, byte-identical to the pipe + dump: 5cb7483040621df964779a9e0fb21d45 both ways on that file, 2.3 s instead of a 48.9 MB + pipe), and `capture_output=True` drains both pipes concurrently, so nothing can wedge. """ bsf = [] r = subprocess.run([FFPROBE, "-v", "error", "-select_streams", sel, "-count_packets", @@ -495,24 +505,28 @@ def stream_signature(path, sel, timeout=1800): # 「copy」这个动作,不是流的真实编码;而这一条 bsf 必须按**真实编码**决定。 if codec_name.lower() in ("aac", "aac_latm"): bsf = ["-bsf:a", "aac_adtstoasc"] - p = subprocess.Popen([FFMPEG, "-v", "error", "-i", path, "-map", "0:" + sel, "-c", "copy"] + bsf + - ["-f", "data", "-"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, - creationflags=NOWIN) - h = hashlib.md5() + # `-f streamhash` = ffmpeg's own md5 of the packet payloads (no container bytes), so there is + # no payload in flight between us and it. ONE capture_output call drains stdout AND stderr on + # separate threads (`communicate`), which is what makes the stderr flood harmless - and on + # `timeout` the child is killed, so a wedged demuxer costs one bounded wait instead of a lane. try: - while True: - chunk = p.stdout.read(1 << 20) - if not chunk: - break - h.update(chunk) - finally: - p.stdout.close() - err = p.stderr.read() if p.stderr else b"" - p.stderr.close() - p.wait(timeout=timeout) + p = subprocess.run([FFMPEG, "-v", "error", "-i", path, "-map", "0:" + sel, "-c", "copy"] + bsf + + ["-f", "streamhash", "-hash", "md5", "-"], + capture_output=True, text=True, encoding="utf-8", errors="replace", + timeout=timeout, creationflags=NOWIN) + except subprocess.TimeoutExpired: + return None if p.returncode != 0: return None - return nb, h.hexdigest() + md5 = "" + for line in (p.stdout or "").splitlines(): + # `0,a,MD5=` - the leading index is the output stream, `a` its type + if "MD5=" in line: + md5 = line.rsplit("MD5=", 1)[1].strip().lower() + break + if len(md5) != 32: + return None + return nb, md5 def copy_streams_intact(plan_, src_path, out_path):