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 {
86 /// own state because it is what makes "how many are in flight" a number:86 /// own state because it is what makes "how many are in flight" a number:
87 /// the answer comes back on a worker, and until it does this file may still87 /// the answer comes back on a worker, and until it does this file may still
88 /// turn into an encode.88 /// 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>),
90 /// A file this machine cuts at speed as it is: no stand-in, and none96 /// A file this machine cuts at speed as it is: no stand-in, and none
91 /// wanted ([`engine::proxy::wanted`]).97 /// wanted ([`engine::proxy::wanted`]).
92 Native,98 Native,
crates/app/src/player/library.rsmodified+14 -5
@@ -621,10 +621,15 @@ impl Player {
621 /// film's stand-in has asked, and is told by the row how far it has got.621 /// film's stand-in has asked, and is told by the row how far it has got.
622 pub(crate) fn start_proxy_for(&mut self, path: &std::path::Path, cx: &mut Context<Self>) {622 pub(crate) fn start_proxy_for(&mut self, path: &std::path::Path, cx: &mut Context<Self>) {
623 let path = path.to_path_buf();623 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()));
625 let started = cx.background_executor().spawn({630 let started = cx.background_executor().spawn({
626 let path = path.clone();631 let path = path.clone();
627- async move { engine::proxy::generate_if_wanted(&path) }632+ async move { engine::proxy::generate_if_wanted(&path, &cancel) }
628 });633 });
629 cx.spawn(async move |this, cx| {634 cx.spawn(async move |this, cx| {
630 let started = started.await;635 let started = started.await;
@@ -636,7 +641,7 @@ impl Player {
636 // half of a click made during the ask is an encoder nothing in641 // half of a click made during the ask is an encoder nothing in
637 // the map can reach -- and it finishes minutes later and puts642 // the map can reach -- and it finishes minutes later and puts
638 // back the stand-in somebody switched off.643 // 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(_)));
640 let state = match started {645 let state = match started {
641 // Cancelled and *held*, exactly as a stop of a running one646 // Cancelled and *held*, exactly as a stop of a running one
642 // is: the worker's own answer is what settles it, so an647 // is: the worker's own answer is what settles it, so an
@@ -710,7 +715,11 @@ impl Player {
710 Some(Proxy::Making(_)) => self.cancel_proxy(path, cx),715 Some(Proxy::Making(_)) => self.cancel_proxy(path, cx),
711 // Already winding down: the second click on a stop is nothing.716 // Already winding down: the second click on a stop is nothing.
712 Some(Proxy::Cancelling(_)) => {}717 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);
714 engine::proxy::set_off(path, true);723 engine::proxy::set_off(path, true);
715 self.proxies.insert(path.to_path_buf(), Proxy::Cancelled);724 self.proxies.insert(path.to_path_buf(), Proxy::Cancelled);
716 let text = format!("STOPPING the stand-in for {}…", file_name(path));725 let text = format!("STOPPING the stand-in for {}…", file_name(path));
@@ -860,7 +869,7 @@ impl Player {
860 pub(crate) fn in_flight_proxies(&self) -> usize {869 pub(crate) fn in_flight_proxies(&self) -> usize {
861 self.proxies870 self.proxies
862 .values()871 .values()
863- .filter(|p| matches!(p, Proxy::Asked | Proxy::Making(_) | Proxy::Cancelling(_)))872+ .filter(|p| matches!(p, Proxy::Asked(_) | Proxy::Making(_) | Proxy::Cancelling(_)))
864 .count()873 .count()
865 }874 }
866 875
crates/app/src/tests/cards.rsmodified+25 -0
@@ -2084,3 +2084,28 @@ fn darkroom_source_rows_keep_proxy_add_and_media_audio_text_paths() {
2084 );2084 );
2085 }2085 }
2086 }2086 }
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(
293 true,293 true,
294 format!("Stopping the stand-in for {}…", row.name),294 format!("Stopping the stand-in for {}…", row.name),
295 ),295 ),
296- Some(Proxy::Asked) => (296+ Some(Proxy::Asked(_)) => (
297 "○",297 "○",
298 false,298 false,
299 None,299 None,
crates/engine/src/demux.rsmodified+8 -0
@@ -102,6 +102,14 @@ impl<R: Seek> Seek for CancelableReader<R> {
102 }102 }
103 }103 }
104 104
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+
105 /// What the video track is coded with. Not a decoder choice by itself: only113 /// What the video track is coded with. Not a decoder choice by itself: only
106 /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`]114 /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`]
107 /// says for the others.115 /// says for the others.
crates/engine/src/proxy.rsmodified+80 -9
@@ -32,6 +32,8 @@
32 //! cancel a file export has ([`Job`]).32 //! cancel a file export has ([`Job`]).
33 33
34 use std::path::{Path, PathBuf};34 use std::path::{Path, PathBuf};
35+use std::sync::Arc;
36+use std::sync::atomic::AtomicBool;
35 use std::time::UNIX_EPOCH;37 use std::time::UNIX_EPOCH;
36 38
37 use crate::demux::{Codec, Demuxer, NoVideoTrack, VideoMeta};39 use crate::demux::{Codec, Demuxer, NoVideoTrack, VideoMeta};
@@ -439,7 +441,10 @@ impl Job {
439 /// back through [`Job::outcome`], so a caller has two places to look and not441 /// back through [`Job::outcome`], so a caller has two places to look and not
440 /// twenty.442 /// twenty.
441 pub fn generate(source: &Path) -> crate::Result<Job> {443 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"))
443 }448 }
444 449
445 /// The same, for a file that is worth standing in for and nobody else450 /// 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> {
461 /// header says so -- so the demuxer's own [`NoVideoTrack`] answer is taken here466 /// header says so -- so the demuxer's own [`NoVideoTrack`] answer is taken here
462 /// as the same quiet `None`. A film that *has* a picture and will not open is a467 /// as the same quiet `None`. A film that *has* a picture and will not open is a
463 /// different thing and stays loud.468 /// 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>> {
465 if crate::is_audio(source) || crate::is_image(source) {480 if crate::is_audio(source) || crate::is_image(source) {
466 return Ok(None);481 return Ok(None);
467 }482 }
@@ -472,7 +487,7 @@ pub fn generate_if_wanted(source: &Path) -> crate::Result<Option<Job>> {
472 if is_off(source) {487 if is_off(source) {
473 return Ok(None);488 return Ok(None);
474 }489 }
475- match started(source, wanted) {490+ match started(source, wanted, cancel) {
476 Err(e) if NoVideoTrack::is_it(&e) => Ok(None),491 Err(e) if NoVideoTrack::is_it(&e) => Ok(None),
477 other => other,492 other => other,
478 }493 }
@@ -480,7 +495,18 @@ pub fn generate_if_wanted(source: &Path) -> crate::Result<Option<Job>> {
480 495
481 /// Both doors: the cache is looked at, the header is read once, and `only_if`496 /// Both doors: the cache is looked at, the header is read once, and `only_if`
482 /// decides off that header whether there is anything to do.497 /// 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>> {
484 let out = path_for(source).ok_or("no cache directory to keep a proxy in")?;510 let out = path_for(source).ok_or("no cache directory to keep a proxy in")?;
485 if out.is_file() {511 if out.is_file() {
486 return Ok(Some(Job {512 return Ok(Some(Job {
@@ -493,10 +519,18 @@ fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result<Opti
493 // the job is dropped ([`IN_FLIGHT`]): the second start would unlink the519 // the job is dropped ([`IN_FLIGHT`]): the second start would unlink the
494 // first's `.part` below and orphan an encode nothing can stop.520 // first's `.part` below and orphan an encode nothing can stop.
495 let claim = claim(&out).ok_or("a stand-in for this film is already being made")?;521 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+ }
496 // The source's own header: its rate and length are the proxy's (a stand-in528 // The source's own header: its rate and length are the proxy's (a stand-in
497 // that ran at another rate or stopped early would put every cut on another529 // that ran at another rate or stopped early would put every cut on another
498- // frame), and only its picture size changes.530+ // frame), and only its picture size changes. Wrapped in the same cancel an
499- let (meta, _) = Demuxer::open(source)?;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))?;
500 if !only_if(&meta) {534 if !only_if(&meta) {
501 return Ok(None);535 return Ok(None);
502 }536 }
@@ -580,6 +614,12 @@ mod tests {
580 use super::*;614 use super::*;
581 use crate::colorspace::ColorDescription;615 use crate::colorspace::ColorDescription;
582 616
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+
583 fn meta(width: u32, height: u32, codec: Codec) -> VideoMeta {623 fn meta(width: u32, height: u32, codec: Codec) -> VideoMeta {
584 VideoMeta {624 VideoMeta {
585 width,625 width,
@@ -738,7 +778,7 @@ mod tests {
738 for name in ["tone.wav", "song.mp3", "track.mka", "still.png", "shot.jpg"] {778 for name in ["tone.wav", "song.mp3", "track.mka", "still.png", "shot.jpg"] {
739 let file = dir.join(name);779 let file = dir.join(name);
740 std::fs::write(&file, b"not a film, and never read").expect("write");780 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());
742 assert!(782 assert!(
743 matches!(answer, Ok(None)),783 matches!(answer, Ok(None)),
744 "{name} is not a film to stand in for: {:?}",784 "{name} is not a film to stand in for: {:?}",
@@ -758,7 +798,7 @@ mod tests {
758 crate::demux::NoVideoTrack::is_it(&refusal),798 crate::demux::NoVideoTrack::is_it(&refusal),
759 "not the no-picture answer: {refusal}"799 "not the no-picture answer: {refusal}"
760 );800 );
761- let answer = generate_if_wanted(&silent_film);801+ let answer = generate_if_wanted(&silent_film, &uncancelled());
762 assert!(802 assert!(
763 matches!(answer, Ok(None)),803 matches!(answer, Ok(None)),
764 "an audio-only mp4 is not a film to stand in for: {:?}",804 "an audio-only mp4 is not a film to stand in for: {:?}",
@@ -873,7 +913,7 @@ mod tests {
873 assert!(is_off(&file), "the mark is beside the cache entry");913 assert!(is_off(&file), "the mark is beside the cache entry");
874 // Which is what the sweep asks -- and it does not even read the header.914 // Which is what the sweep asks -- and it does not even read the header.
875 assert!(915 assert!(
876- matches!(generate_if_wanted(&file), Ok(None)),916+ matches!(generate_if_wanted(&file, &uncancelled()), Ok(None)),
877 "a switched-off film was started again"917 "a switched-off film was started again"
878 );918 );
879 set_off(&file, false);919 set_off(&file, false);
@@ -891,6 +931,37 @@ mod tests {
891 std::fs::remove_file(stale).expect("the old mark");931 std::fs::remove_file(stale).expect("the old mark");
892 }932 }
893 933
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+
894 /// One encoder per film, in the engine and not only in the window that965 /// One encoder per film, in the engine and not only in the window that
895 /// draws the switch: the seat is taken for as long as the job lives, and a966 /// draws the switch: the seat is taken for as long as the job lives, and a
896 /// second start for the same stand-in is refused rather than allowed to967 /// second start for the same stand-in is refused rather than allowed to