forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-a4b3e198388f130b7'

Browse files at this commit

a72c5c5f6dd79bf9d04cb66091da81771a9983daAhmet Kaan GümüşParent: 4257e494de1efe
# Conflicts:
#	crates/engine/src/demux.rs
crates/app/src/importing.rsmodified+7 -1
@@ -86,7 +86,13 @@ pub(crate) enum Proxy {
8686 /// own state because it is what makes "how many are in flight" a number:
8787 /// the answer comes back on a worker, and until it does this file may still
8888 /// turn into an encode.
89- Asked,
89+ ///
90+ /// Carries the flag [`engine::proxy::generate_if_wanted`] polls at the door
91+ /// and around the header read: the same shape [`Self::Making`] carries a
92+ /// cancellable [`engine::proxy::Job`] with, one step earlier -- there is no
93+ /// `Job` yet to hold, so the raw flag is what a stop asked during this
94+ /// state has to set.
95+ Asked(std::sync::Arc<std::sync::atomic::AtomicBool>),
9096 /// A file this machine cuts at speed as it is: no stand-in, and none
9197 /// wanted ([`engine::proxy::wanted`]).
9298 Native,
crates/app/src/player/library.rsmodified+14 -5
@@ -621,10 +621,15 @@ impl Player {
621621 /// film's stand-in has asked, and is told by the row how far it has got.
622622 pub(crate) fn start_proxy_for(&mut self, path: &std::path::Path, cx: &mut Context<Self>) {
623623 let path = path.to_path_buf();
624- self.proxies.insert(path.clone(), Proxy::Asked);
624+ // The same flag [`engine::proxy::generate_if_wanted`] polls at the door
625+ // and around the header read: a click on the row while the header is
626+ // still being walked ([`Player::toggle_proxy`]'s `Asked` arm) sets it,
627+ // so the walk unwinds mid-read instead of running to completion.
628+ let cancel = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
629+ self.proxies.insert(path.clone(), Proxy::Asked(cancel.clone()));
625630 let started = cx.background_executor().spawn({
626631 let path = path.clone();
627- async move { engine::proxy::generate_if_wanted(&path) }
632+ async move { engine::proxy::generate_if_wanted(&path, &cancel) }
628633 });
629634 cx.spawn(async move |this, cx| {
630635 let started = started.await;
@@ -636,7 +641,7 @@ impl Player {
636641 // half of a click made during the ask is an encoder nothing in
637642 // the map can reach -- and it finishes minutes later and puts
638643 // back the stand-in somebody switched off.
639- let stopped = !matches!(this.proxies.get(&path), Some(Proxy::Asked));
644+ let stopped = !matches!(this.proxies.get(&path), Some(Proxy::Asked(_)));
640645 let state = match started {
641646 // Cancelled and *held*, exactly as a stop of a running one
642647 // is: the worker's own answer is what settles it, so an
@@ -710,7 +715,11 @@ impl Player {
710715 Some(Proxy::Making(_)) => self.cancel_proxy(path, cx),
711716 // Already winding down: the second click on a stop is nothing.
712717 Some(Proxy::Cancelling(_)) => {}
713- Some(Proxy::Asked) => {
718+ Some(Proxy::Asked(cancel)) => {
719+ // Wakes the header read mid-walk ([`engine::proxy::started`]):
720+ // without this the stop only ever landed once the read had
721+ // already run to completion on its own (DEBT #91).
722+ cancel.store(true, std::sync::atomic::Ordering::Release);
714723 engine::proxy::set_off(path, true);
715724 self.proxies.insert(path.to_path_buf(), Proxy::Cancelled);
716725 let text = format!("STOPPING the stand-in for {}…", file_name(path));
@@ -860,7 +869,7 @@ impl Player {
860869 pub(crate) fn in_flight_proxies(&self) -> usize {
861870 self.proxies
862871 .values()
863- .filter(|p| matches!(p, Proxy::Asked | Proxy::Making(_) | Proxy::Cancelling(_)))
872+ .filter(|p| matches!(p, Proxy::Asked(_) | Proxy::Making(_) | Proxy::Cancelling(_)))
864873 .count()
865874 }
866875
crates/app/src/tests/cards.rsmodified+25 -0
@@ -2084,3 +2084,28 @@ fn darkroom_source_rows_keep_proxy_add_and_media_audio_text_paths() {
20842084 );
20852085 }
20862086 }
2087+
2088+/// DEBT #91: a click-to-stop while a stand-in's header is still being read
2089+/// must reach the flag the read itself polls (`engine::proxy::started`), not
2090+/// only relabel the row. Read off the source and not run, the way the row
2091+/// above it is -- there is no `TestAppContext` in this binary to construct a
2092+/// `Context<Player>` and call `toggle_proxy` on. On the state this fix
2093+/// replaced, `start_proxy_for` handed the header read a flag nothing outside
2094+/// it could reach and `Proxy::Asked` carried no field to store a stop into,
2095+/// so `library.find("Proxy::Asked(cancel)")` below has nothing to find.
2096+#[test]
2097+fn toggle_proxy_wakes_the_asked_phase_it_stops() {
2098+ let library = src_text("player/library.rs");
2099+ let start = &library[library.find("fn start_proxy_for(").expect("start_proxy_for")..];
2100+ assert!(
2101+ start.contains("generate_if_wanted(&path, &cancel)"),
2102+ "start_proxy_for no longer hands the header read a cancel flag anyone can reach"
2103+ );
2104+ let toggle = &library[library.find("fn toggle_proxy(").expect("toggle_proxy")..];
2105+ let asked_arm =
2106+ &toggle[toggle.find("Proxy::Asked(cancel)").expect("the Asked arm's own flag")..];
2107+ assert!(
2108+ asked_arm.contains("cancel.store(true"),
2109+ "a stop asked during Asked no longer sets the flag the header read polls"
2110+ );
2111+}
crates/app/src/ui/dock_stance.rsmodified+1 -1
@@ -293,7 +293,7 @@ fn source_row(
293293 true,
294294 format!("Stopping the stand-in for {}…", row.name),
295295 ),
296- Some(Proxy::Asked) => (
296+ Some(Proxy::Asked(_)) => (
297297 "○",
298298 false,
299299 None,
crates/engine/src/demux.rsmodified+8 -0
@@ -102,6 +102,14 @@ impl<R: Seek> Seek for CancelableReader<R> {
102102 }
103103 }
104104
105+/// The same answer a walk gives up with on its own, for a caller that finds
106+/// its cancel already set before there is any walk to abandon -- a proxy's
107+/// header read finding its flag up the moment its seat is claimed, say. Kept
108+/// to one string so [`is_cancelled`] answers both the same way.
109+pub fn cancelled_read_error() -> crate::Error {
110+ CANCELLED.into()
111+}
112+
105113 /// What the video track is coded with. Not a decoder choice by itself: only
106114 /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`]
107115 /// says for the others.
crates/engine/src/proxy.rsmodified+80 -9
@@ -32,6 +32,8 @@
3232 //! cancel a file export has ([`Job`]).
3333
3434 use std::path::{Path, PathBuf};
35+use std::sync::Arc;
36+use std::sync::atomic::AtomicBool;
3537 use std::time::UNIX_EPOCH;
3638
3739 use crate::demux::{Codec, Demuxer, NoVideoTrack, VideoMeta};
@@ -439,7 +441,10 @@ impl Job {
439441 /// back through [`Job::outcome`], so a caller has two places to look and not
440442 /// twenty.
441443 pub fn generate(source: &Path) -> crate::Result<Job> {
442- Ok(started(source, |_| true)?.expect("nothing was filtered out"))
444+ // Nothing here has anything to cancel a header read with -- a flag that is
445+ // never set is the same as the read never checking one at all.
446+ let uncancelled = Arc::new(AtomicBool::new(false));
447+ Ok(started(source, |_| true, &uncancelled)?.expect("nothing was filtered out"))
443448 }
444449
445450 /// The same, for a file that is worth standing in for and nobody else
@@ -461,7 +466,17 @@ pub fn generate(source: &Path) -> crate::Result<Job> {
461466 /// header says so -- so the demuxer's own [`NoVideoTrack`] answer is taken here
462467 /// as the same quiet `None`. A film that *has* a picture and will not open is a
463468 /// different thing and stays loud.
464-pub fn generate_if_wanted(source: &Path) -> crate::Result<Option<Job>> {
469+///
470+/// `cancel` is polled through the whole Asked phase, the same flag the caller
471+/// hands the [`Job`] once one exists: a click to stop this file's stand-in
472+/// while the header is still being read (a Matroska's is a cluster walk, not
473+/// a stat) used to have nothing to set, since there was no [`Job`] yet to
474+/// [`cancel`](Job::cancel) -- so the read ran to completion regardless, and a
475+/// stop landed only once an encoder was already open. Set before this is
476+/// called or at any point while it runs, it is read at the door and again
477+/// wrapped around the header read itself ([`crate::demux::with_cancel`]), so a
478+/// cancelled read unwinds at its next element instead of finishing.
479+pub fn generate_if_wanted(source: &Path, cancel: &Arc<AtomicBool>) -> crate::Result<Option<Job>> {
465480 if crate::is_audio(source) || crate::is_image(source) {
466481 return Ok(None);
467482 }
@@ -472,7 +487,7 @@ pub fn generate_if_wanted(source: &Path) -> crate::Result<Option<Job>> {
472487 if is_off(source) {
473488 return Ok(None);
474489 }
475- match started(source, wanted) {
490+ match started(source, wanted, cancel) {
476491 Err(e) if NoVideoTrack::is_it(&e) => Ok(None),
477492 other => other,
478493 }
@@ -480,7 +495,18 @@ pub fn generate_if_wanted(source: &Path) -> crate::Result<Option<Job>> {
480495
481496 /// Both doors: the cache is looked at, the header is read once, and `only_if`
482497 /// decides off that header whether there is anything to do.
483-fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result<Option<Job>> {
498+///
499+/// `cancel` is checked the moment the seat is claimed and again around the
500+/// header read: a cancel that lands before either point is answered without
501+/// spending a read nobody wants the result of, with [`crate::demux::is_cancelled`]
502+/// true of the error -- and the seat this claimed is given back the same way
503+/// a cancelled encode's is, [`Claim`]'s own `Drop`, run on the early return
504+/// exactly as it is on the ordinary one.
505+fn started(
506+ source: &Path,
507+ only_if: fn(&VideoMeta) -> bool,
508+ cancel: &Arc<AtomicBool>,
509+) -> crate::Result<Option<Job>> {
484510 let out = path_for(source).ok_or("no cache directory to keep a proxy in")?;
485511 if out.is_file() {
486512 return Ok(Some(Job {
@@ -493,10 +519,18 @@ fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result<Opti
493519 // the job is dropped ([`IN_FLIGHT`]): the second start would unlink the
494520 // first's `.part` below and orphan an encode nothing can stop.
495521 let claim = claim(&out).ok_or("a stand-in for this film is already being made")?;
522+ // A cancel that landed before this ran at all, or while it queued behind
523+ // another film's seat: answered here, before a byte of the header is read,
524+ // rather than starting a read that would only be thrown away.
525+ if cancel.load(std::sync::atomic::Ordering::Acquire) {
526+ return Err(crate::demux::cancelled_read_error());
527+ }
496528 // The source's own header: its rate and length are the proxy's (a stand-in
497529 // that ran at another rate or stopped early would put every cut on another
498- // frame), and only its picture size changes.
499- let (meta, _) = Demuxer::open(source)?;
530+ // frame), and only its picture size changes. Wrapped in the same cancel an
531+ // encode itself is polled with, so a stop asked for mid-walk unwinds the
532+ // walk instead of waiting it out.
533+ let (meta, _) = crate::demux::with_cancel(cancel, || Demuxer::open(source))?;
500534 if !only_if(&meta) {
501535 return Ok(None);
502536 }
@@ -580,6 +614,12 @@ mod tests {
580614 use super::*;
581615 use crate::colorspace::ColorDescription;
582616
617+ /// A cancel flag no test in this file is exercising, for the calls that
618+ /// have nothing to cancel.
619+ fn uncancelled() -> Arc<AtomicBool> {
620+ Arc::new(AtomicBool::new(false))
621+ }
622+
583623 fn meta(width: u32, height: u32, codec: Codec) -> VideoMeta {
584624 VideoMeta {
585625 width,
@@ -738,7 +778,7 @@ mod tests {
738778 for name in ["tone.wav", "song.mp3", "track.mka", "still.png", "shot.jpg"] {
739779 let file = dir.join(name);
740780 std::fs::write(&file, b"not a film, and never read").expect("write");
741- let answer = generate_if_wanted(&file);
781+ let answer = generate_if_wanted(&file, &uncancelled());
742782 assert!(
743783 matches!(answer, Ok(None)),
744784 "{name} is not a film to stand in for: {:?}",
@@ -758,7 +798,7 @@ mod tests {
758798 crate::demux::NoVideoTrack::is_it(&refusal),
759799 "not the no-picture answer: {refusal}"
760800 );
761- let answer = generate_if_wanted(&silent_film);
801+ let answer = generate_if_wanted(&silent_film, &uncancelled());
762802 assert!(
763803 matches!(answer, Ok(None)),
764804 "an audio-only mp4 is not a film to stand in for: {:?}",
@@ -873,7 +913,7 @@ mod tests {
873913 assert!(is_off(&file), "the mark is beside the cache entry");
874914 // Which is what the sweep asks -- and it does not even read the header.
875915 assert!(
876- matches!(generate_if_wanted(&file), Ok(None)),
916+ matches!(generate_if_wanted(&file, &uncancelled()), Ok(None)),
877917 "a switched-off film was started again"
878918 );
879919 set_off(&file, false);
@@ -891,6 +931,37 @@ mod tests {
891931 std::fs::remove_file(stale).expect("the old mark");
892932 }
893933
934+ /// DEBT #91: a cancel already up when the Asked phase starts must answer
935+ /// promptly and never touch the header at all -- before this, the flag
936+ /// [`generate_if_wanted`] takes did not exist, so nothing here could be
937+ /// cancelled before a [`Job`] existed to call [`Job::cancel`] on, and the
938+ /// only way to see this pass on old code was to change the signature.
939+ ///
940+ /// The file is never a real film: the cancel is answered at the door,
941+ /// before [`Demuxer::open`] is ever reached, which is what "promptly"
942+ /// means here and what proves the header was never read.
943+ #[test]
944+ fn a_cancel_set_before_the_ask_stops_it_at_the_door() {
945+ let dir = crate::scratch::Scratch::dir("proxy-cancel-at-door");
946+ let file = dir.join("a.mp4");
947+ std::fs::write(&file, b"not a real film, and never read").expect("write");
948+ let cancel = Arc::new(AtomicBool::new(true));
949+
950+ let answer = generate_if_wanted(&file, &cancel);
951+ let err = answer.err().expect("a pre-cancelled ask must not hand back a job");
952+ assert!(
953+ crate::demux::is_cancelled(&err),
954+ "not the cancelled answer: {err}"
955+ );
956+ assert_eq!(cached(&file), None, "no proxy was produced");
957+
958+ // Settled the same way a cancelled `Making` is: the seat this claimed
959+ // is given back, so the very next ask -- uncancelled -- is free to take
960+ // it rather than being told one is "already being made".
961+ let out = path_for(&file).expect("a key");
962+ assert!(claim(&out).is_some(), "the door-cancel left the seat held");
963+ }
964+
894965 /// One encoder per film, in the engine and not only in the window that
895966 /// draws the switch: the seat is taken for as long as the job lives, and a
896967 /// second start for the same stand-in is refused rather than allowed to