From 8a96a5a9416618de58a71ba056ec005f018a22ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 02:41:39 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(engine):=20cancel=20an=20mp4?= =?UTF-8?q?=20import=20mid-moov,=20same=20as=20the=20mkv=20arm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mp4Reader::read_header ran the whole moov parse uncancellable while the Matroska arm's walk polled the flag per element -- a cancelled import of a big mp4 blocked until the header finished (debts #57/#58). Wraps the BufReader handed to Mp4Reader in a CancelableReader that checks demux::cancelled() on every read(), surfacing a "read cancelled" io::Error the mp4 crate propagates as-is, so is_cancelled classifies it the same way as the mkv path. No fork of the mp4 crate. --- crates/engine/src/demux.rs | 48 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/demux.rs b/crates/engine/src/demux.rs index e68508c..c8c1978 100644 --- a/crates/engine/src/demux.rs +++ b/crates/engine/src/demux.rs @@ -77,6 +77,31 @@ pub fn is_cancelled(error: &crate::Error) -> bool { error.to_string() == CANCELLED } +/// The mp4 arm's half of [`cancelled`]: the Matroska walk polls the flag itself +/// between elements, but `Mp4Reader::read_header` is the `mp4` crate's own loop +/// over the `moov`, called once and not returned to until the whole header is +/// parsed. This is the same poll wearing a `Read`/`Seek` face instead, so a +/// cancel mid-`moov` surfaces exactly where the crate next asks this file for +/// bytes -- as an [`io::Error`](std::io::Error) that `Mp4Reader::read_header` +/// has no choice but to propagate, in [`CANCELLED`]'s own words so +/// [`is_cancelled`] tells it apart from the file the same way either arm does. +struct CancelableReader(R); + +impl Read for CancelableReader { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + if cancelled() { + return Err(std::io::Error::other(CANCELLED)); + } + self.0.read(buf) + } +} + +impl Seek for CancelableReader { + fn seek(&mut self, pos: SeekFrom) -> std::io::Result { + self.0.seek(pos) + } +} + /// What the video track is coded with. Not a decoder choice by itself: only /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`] /// says for the others. @@ -602,7 +627,7 @@ pub fn is_matroska(path: &Path) -> bool { } pub struct Mp4Demuxer { - reader: Mp4Reader>, + reader: Mp4Reader>>, track_id: u32, sample_count: u32, /// 1-based sample id of *frame 0*: the edit list can start the presentation @@ -639,7 +664,7 @@ impl Mp4Demuxer { fn open(path: &Path) -> crate::Result<(VideoMeta, Self)> { let file = File::open(path)?; let size = file.metadata()?.len(); - let reader = Mp4Reader::read_header(BufReader::new(file), size)?; + let reader = Mp4Reader::read_header(CancelableReader(BufReader::new(file)), size)?; // In *file* order, out of `moov.traks` rather than `Mp4Reader::tracks`, // which is a `HashMap`: iterating that would make "the video track" a @@ -4203,6 +4228,25 @@ mod tests { ); } + /// **A cancelled mp4 open stops reading the header too.** The `mp4` crate + /// has no cancel of its own -- [`CancelableReader`] is what stands in -- + /// so this pre-sets the flag before [`Mp4Demuxer::open`] ever calls + /// `Mp4Reader::read_header` and asserts the open comes back [`is_cancelled`] + /// rather than a parsed header. On the reader that only wraps `BufReader` + /// (pre-[`CancelableReader`]) the flag is never polled and this test fails + /// with `Ok(_)`. + #[test] + fn a_cancelled_mp4_open_gives_up_and_says_so() { + let path = asset("test_av.mp4"); + let flag = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true)); + let opened = with_cancel(&flag, || Mp4Demuxer::open(&path)); + let error = opened.err().expect("a cancelled open must not parse a header"); + assert!( + is_cancelled(&error), + "a cancelled mp4 open reported {error} -- a verdict about the file" + ); + } + /// **The lazy index is the same index.** For every Matroska fixture, the /// window built off the file's `Cues` and the whole-segment walk it replaced /// agree on: the block list, the frame count, and where every one of the