DEBT #91 — proxy Asked phase now cancels its header read
Browse files at this commitba29145aefc509194074b58624a17d3c98eed973Parent: 9dc7454- fix
- DEBT #91 — proxy Asked phase now cancels its header read
3 changed files +97 -10
- crates/app/src/player/library.rs +9 -1
- crates/engine/src/demux.rs +8 -0
- crates/engine/src/proxy.rs +80 -9
crates/app/src/player/library.rsmodified+9 -1
| @@ -624,7 +624,15 @@ impl Player { | |||
| 624 | self.proxies.insert(path.clone(), Proxy::Asked); | 624 | self.proxies.insert(path.clone(), Proxy::Asked); |
| 625 | let started = cx.background_executor().spawn({ | 625 | let started = cx.background_executor().spawn({ |
| 626 | let path = path.clone(); | 626 | let path = path.clone(); |
| 627 | - async move { engine::proxy::generate_if_wanted(&path) } | 627 | + // deferred: DEBT #91 wires only the engine's header-read cancel |
| 628 | + // (crates/engine/src/proxy.rs); the app has no cancel to hand it | ||
| 629 | + // yet -- a stop asked during Asked still lands the way it always | ||
| 630 | + // has, by the `stopped` check below once the ask returns. Wiring | ||
| 631 | + // this flag to `toggle_proxy` is the next slice. | ||
| 632 | + async move { | ||
| 633 | + let uncancelled = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); | ||
| 634 | + engine::proxy::generate_if_wanted(&path, &uncancelled) | ||
| 635 | + } | ||
| 628 | }); | 636 | }); |
| 629 | cx.spawn(async move |this, cx| { | 637 | cx.spawn(async move |this, cx| { |
| 630 | let started = started.await; | 638 | let started = started.await; |
crates/engine/src/demux.rsmodified+8 -0
| @@ -77,6 +77,14 @@ pub fn is_cancelled(error: &crate::Error) -> bool { | |||
| 77 | error.to_string() == CANCELLED | 77 | error.to_string() == CANCELLED |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | +/// The same answer a walk gives up with on its own, for a caller that finds | ||
| 81 | +/// its cancel already set before there is any walk to abandon -- a proxy's | ||
| 82 | +/// header read finding its flag up the moment its seat is claimed, say. Kept | ||
| 83 | +/// to one string so [`is_cancelled`] answers both the same way. | ||
| 84 | +pub fn cancelled_read_error() -> crate::Error { | ||
| 85 | + CANCELLED.into() | ||
| 86 | +} | ||
| 87 | + | ||
| 80 | /// What the video track is coded with. Not a decoder choice by itself: only | 88 | /// What the video track is coded with. Not a decoder choice by itself: only |
| 81 | /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`] | 89 | /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`] |
| 82 | /// says for the others. | 90 | /// 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 not | 441 | /// 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 else | 450 | /// 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 here | 466 | /// 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 a | 467 | /// 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 the | 519 | // 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-in | 528 | // 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 another | 529 | // 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 that | 965 | /// 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 a | 966 | /// 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 to | 967 | /// second start for the same stand-in is refused rather than allowed to |