Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(cutout): a project the app closed in while removing a background opens again

Jareer6eb78add82b4
diff --git a/src/crates/concat-host/src/brush.rs b/src/crates/concat-host/src/brush.rs
index ee019aaf..75e59fbe 100644
--- a/src/crates/concat-host/src/brush.rs
+++ b/src/crates/concat-host/src/brush.rs
@@ -34,7 +34,7 @@ use concat_vision::{
};
use crate::cutout::Progress;
-use crate::jobs::SingleFlight;
+use crate::jobs::{SingleFlight, Tripwire};
/// One stroke to read.
#[derive(Clone, Debug)]
@@ -76,6 +76,12 @@ impl RegionRequest {
}
!missing(&store, &self.ranges).is_empty()
}
+
+ /// Whether the app died while reading this stroke last time, so it is
+ /// not to be read again by itself. See [`Tripwire`].
+ pub fn tripped(&self) -> bool {
+ Tripwire::tripped(&self.dir())
+ }
}
/// How many frames' embeddings stay in memory. Each is a few megabytes;
@@ -188,6 +194,7 @@ impl Brushes {
let Some(at) = request.stroke.at else {
return Err("the stroke has no instant".to_owned());
};
+ let _wire = Tripwire::arm(&request.dir());
let mut store = MaskStore::open(&request.dir());
let brush = self.brush(&cancel, progress)?;
progress(Progress::Analysing(0.0));
diff --git a/src/crates/concat-host/src/cutout.rs b/src/crates/concat-host/src/cutout.rs
index 8d6eb786..8bb0bae4 100644
--- a/src/crates/concat-host/src/cutout.rs
+++ b/src/crates/concat-host/src/cutout.rs
@@ -33,7 +33,7 @@ use concat_project::model::{MediaKind, Project, Subject};
use concat_vision::segment::Kind;
use concat_vision::{MASK_RATE, MaskStore, ModelId, Segmenter, mask_dir, models};
-use crate::jobs::SingleFlight;
+use crate::jobs::{SingleFlight, Tripwire};
/// What one analysis covers.
#[derive(Clone, Debug)]
@@ -198,6 +198,17 @@ impl Cutouts {
wanted
}
+ /// Where `request`'s masks are kept.
+ fn dir(request: &AnalyseRequest) -> PathBuf {
+ mask_dir(&request.project, &request.media_path, request.subject)
+ }
+
+ /// Whether the app died analysing `request` last time, so it is not to
+ /// be analysed again by itself. See [`Tripwire`].
+ pub fn tripped(request: &AnalyseRequest) -> bool {
+ Tripwire::tripped(&Self::dir(request))
+ }
+
/// How many instants `request` still needs, without doing anything.
/// A store made by a model the subject would not pick now needs all
/// of them again.
@@ -240,6 +251,7 @@ impl Cutouts {
if Self::outstanding(request) == usize::MAX {
store.clear();
}
+ let _wire = Tripwire::arm(&Self::dir(request));
// The model the subject names, or for automatic the one the
// store already records; the first frame decides otherwise.
diff --git a/src/crates/concat-host/src/jobs.rs b/src/crates/concat-host/src/jobs.rs
index 4ab26b9c..6c7b064a 100644
--- a/src/crates/concat-host/src/jobs.rs
+++ b/src/crates/concat-host/src/jobs.rs
@@ -110,10 +110,66 @@ impl Drop for Job {
}
}
+/// A file that says a job is running on the folder it names, removed
+/// when the job ends by any road the process survives: done, failed,
+/// cancelled. Left behind, it says the process died inside the job.
+///
+/// A job the window starts by itself whenever its work is outstanding -
+/// a smart stroke's regions - would otherwise start again on the next
+/// open and take the app down with it again: a project that closed the
+/// app every time it was opened, with the person's edits shut inside it.
+/// The window skips a tripped job and says so; the work is redone only
+/// when the person asks for it anew.
+pub struct Tripwire {
+ path: std::path::PathBuf,
+}
+
+impl Tripwire {
+ const FILE: &'static str = "running";
+
+ /// Marks `dir` as having a job running on it. A folder that cannot
+ /// be written to gets no mark, and the job runs unguarded.
+ pub fn arm(dir: &std::path::Path) -> Tripwire {
+ let path = dir.join(Self::FILE);
+ let _ = std::fs::create_dir_all(dir);
+ if let Err(error) = std::fs::write(&path, std::process::id().to_string()) {
+ log::warn!("could not mark {} as running: {error}", dir.display());
+ }
+ Tripwire { path }
+ }
+
+ /// Whether a job on `dir` was running when the process last died.
+ /// Only meaningful while no job on `dir` is running in this process.
+ pub fn tripped(dir: &std::path::Path) -> bool {
+ dir.join(Self::FILE).exists()
+ }
+}
+
+impl Drop for Tripwire {
+ fn drop(&mut self) {
+ let _ = std::fs::remove_file(&self.path);
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
+ #[test]
+ fn a_tripwire_is_left_only_by_a_job_that_never_ended() {
+ let dir = std::env::temp_dir().join(format!("concat-tripwire-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ {
+ let _wire = Tripwire::arm(&dir);
+ assert!(Tripwire::tripped(&dir), "marked while it runs");
+ }
+ assert!(!Tripwire::tripped(&dir), "cleared when it ends");
+ // A process that dies inside the job runs no destructor.
+ std::mem::forget(Tripwire::arm(&dir));
+ assert!(Tripwire::tripped(&dir), "left by a job that never ended");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
#[test]
fn refuses_a_second_concurrent_job() {
let flight = Arc::new(SingleFlight::new());
diff --git a/src/crates/concat/locales/en.json b/src/crates/concat/locales/en.json
index 0bd529f2..dc52d09b 100644
--- a/src/crates/concat/locales/en.json
+++ b/src/crates/concat/locales/en.json
@@ -554,6 +554,7 @@
"studio.reattachAudio": "Reattach audio",
"studio.redo": "Redo",
"studio.removeBackground": "Remove background: {0}",
+ "studio.removeBackgroundSkipped": "Concat closed while removing a background, so that clip's cutout is skipped and the project opens. Turn the cutout off and on again to retry.",
"studio.retiredEffects": "This project uses effects this version no longer has: {0}. They stay in its clips, marked not installed.",
"studio.reverse": "Reverse: {0}",
"studio.reverseAlreadyWorkOne": "Reverse is already at work; one clip at a time",
@@ -570,6 +571,7 @@
"studio.settings": "Settings…",
"studio.showingLookOverPicture": "Showing the look over the picture. Double-click the card, or its plus, to lay it on the timeline",
"studio.smartBrush": "Smart brush: {0}",
+ "studio.smartBrushSkipped": "Concat closed while the smart brush was reading a stroke, so that stroke is skipped and the project opens. Undo it and paint it again to retry.",
"studio.splitAtPlayhead": "Split at playhead",
"studio.stereo": "stereo",
"studio.textToSpeech": "Text to speech…",
diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rs
index 20006854..3b68ce14 100644
--- a/src/crates/concat/src/studio.rs
+++ b/src/crates/concat/src/studio.rs
@@ -958,6 +958,9 @@ pub struct Studio {
/// release until the model has read what was under it, so the paint
/// does not vanish before its answer arrives.
pending_stroke: Option<(String, f32, bool)>,
+ /// Cutouts and smart strokes the app died working on, by their masks'
+ /// folder: not started again by themselves, and said once a session.
+ tripped_strokes: HashSet<std::path::PathBuf>,
}
// ── conversions between the document and the window ─────────────────────
@@ -2165,6 +2168,7 @@ impl Studio {
merge_job: None,
region_job: None,
pending_stroke: None,
+ tripped_strokes: HashSet::new(),
host,
};
studio.handle(crate::panes::Msg::Settings(
@@ -5794,10 +5798,26 @@ impl Studio {
.into_iter()
.map(|(media_id, request)| (Self::analysis_key(&media_id, request.subject), request))
.collect();
- let Some((id, request)) = wanted
- .into_iter()
- .find(|(_, request)| Cutouts::outstanding(request) > 0)
- else {
+ // One the app died analysing last time is skipped, and said once:
+ // starting it again by itself closed the app on every open.
+ let mut next = None;
+ for (id, request) in wanted {
+ if Cutouts::outstanding(&request) == 0 {
+ continue;
+ }
+ if Cutouts::tripped(&request) {
+ let dir =
+ concat_vision::mask_dir(&request.project, &request.media_path, request.subject);
+ if self.tripped_strokes.insert(dir) {
+ log::warn!("cutout: media the app closed while analysing is skipped");
+ self.notify(&t("studio.removeBackgroundSkipped"), true);
+ }
+ continue;
+ }
+ next = Some((id, request));
+ break;
+ }
+ let Some((id, request)) = next else {
return;
};
self.cutout_jobs.insert(id.clone(), (false, 0.0));
@@ -9148,6 +9168,7 @@ impl Studio {
};
let project = std::path::PathBuf::from(session.path());
let mut next: Option<(String, RegionRequest)> = None;
+ let mut tripped = Vec::new();
'clips: for clip in &self.timeline().clips {
let Some(cutout) = clip.cutout.as_ref() else {
continue;
@@ -9173,10 +9194,24 @@ impl Studio {
ranges: vec![range],
stroke: stroke.clone(),
};
- if request.outstanding() {
- next = Some((Self::analysis_key(&media.id, cutout.subject), request));
- break 'clips;
+ if !request.outstanding() {
+ continue;
+ }
+ // The app died reading this one last time: reading it
+ // again by itself closed the app on every open, with the
+ // project shut inside. Painting the stroke again reads it.
+ if request.tripped() {
+ tripped.push(request.dir());
+ continue;
}
+ next = Some((Self::analysis_key(&media.id, cutout.subject), request));
+ break 'clips;
+ }
+ }
+ for dir in tripped {
+ if self.tripped_strokes.insert(dir) {
+ log::warn!("smart brush: a stroke the app closed while reading is skipped");
+ self.notify(&t("studio.smartBrushSkipped"), true);
}
}
let Some((key, request)) = next else {