The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
fix(server): quitting during an export cancels it, tells its caller, and leaves no temp file (#431)
chiarorosa4ffa32127ebc
diff --git a/src/crates/concat-server/src/json.rs b/src/crates/concat-server/src/json.rsindex 321efb49..0e1e163b 100644--- a/src/crates/concat-server/src/json.rs+++ b/src/crates/concat-server/src/json.rs@@ -289,9 +289,26 @@ fn connection<S: Socket>(mut stream: S, door: Door, id: u64, peer: Peer, pendinglet (Ok(writer), Ok(closer)) = (stream.duplicate(), stream.duplicate()) else {return;};+ if stream.read_timeout(None).is_err() || writer.write_timeout(Some(WRITE_TIMEOUT)).is_err() {+ return;+ }+ let Some(outbox) = Outbox::start(writer, id) else {+ return;+ };let _registered = match door.connections.lock() {Ok(mut connections) => {- connections.insert(id, Box::new(move || closer.hang_up()));+ // A stop that waited for the jobs lets their last events out+ // before it hangs up; see `Server::stop`.+ let queued = outbox.clone();+ connections.insert(+ id,+ Box::new(move |drain| {+ if drain {+ queued.close();+ }+ closer.hang_up();+ }),+ );Registered {connections: Arc::clone(&door.connections),id,@@ -299,12 +316,6 @@ fn connection<S: Socket>(mut stream: S, door: Door, id: u64, peer: Peer, pending}Err(_) => return,};- if stream.read_timeout(None).is_err() || writer.write_timeout(Some(WRITE_TIMEOUT)).is_err() {- return;- }- let Some(outbox) = Outbox::start(writer, id) else {- return;- };outbox.send(Message::Reply {id: call,response: Response::Result(Reply::Done(Done {})),@@ -781,4 +792,53 @@ mod tests {server.stop();assert!(client.closed());}++ #[test]+ fn a_stop_lets_out_what_a_caller_was_already_sent() {+ // The API's sink, kept from the API being made, stands in for a job+ // ending as the stop cancels it: what it says before the hang-up is+ // on the wire before the connection closes (#431).+ let scratch = tempfile::tempdir().expect("scratch");+ let dirs = concat_api::AppDirs {+ config: scratch.path().join("config"),+ data: scratch.path().join("data"),+ };+ let kept: Arc<Mutex<Option<concat_api::EventSink>>> = Arc::new(Mutex::new(None));+ let keep = Arc::clone(&kept);+ let config = crate::Config {+ json: Some("127.0.0.1:0".parse().expect("an address")),+ roots: vec![scratch.path().to_path_buf()],+ ..crate::Config::default()+ };+ let server = crate::Server::start(config, move |events| {+ *keep.lock().expect("kept") = Some(Arc::clone(&events));+ Ok(concat_api::Api::with_dirs(dirs, events))+ })+ .expect("starts");+ let mut client = Client::connect(&server);+ let sink = kept+ .lock()+ .expect("kept")+ .clone()+ .expect("the API was made");++ // Fewer than the outbox holds, so none is refused for want of room.+ let sent = 200;+ for frame in 0..sent {+ sink(concat_api::Event::ExportProgress {+ job: "j1".to_owned(),+ path: "project".to_owned(),+ frame,+ total: sent,+ stage: "video".to_owned(),+ });+ }+ server.stop();+ for frame in 0..sent {+ let event = client.read();+ assert_eq!(event["method"], "export.progress", "{event}");+ assert_eq!(event["params"]["frame"], frame, "{event}");+ }+ assert!(client.closed());+ }}diff --git a/src/crates/concat-server/src/lib.rs b/src/crates/concat-server/src/lib.rsindex 130fed1c..75dfd6ee 100644--- a/src/crates/concat-server/src/lib.rs+++ b/src/crates/concat-server/src/lib.rs@@ -178,8 +178,10 @@ pub struct Server {/// Every connection that is in, by number, with its way of being closed,/// so a stop reaches the threads blocked reading them. A connection takes-/// itself out when it ends.-pub(crate) type Connections = Arc<Mutex<BTreeMap<u64, Box<dyn Fn() + Send>>>>;+/// itself out when it ends. Called with `true`, the closing first writes+/// out what the connection has queued - the last event of a job the stop+/// ended - and waits for that, at most the write timeout.+pub(crate) type Connections = Arc<Mutex<BTreeMap<u64, Box<dyn Fn(bool) + Send>>>>;impl Server {/// Binds every address in `config` and starts serving. `make` builds@@ -355,9 +357,16 @@ impl Server {/// Stops the server and returns once everything it started has ended:/// the listeners and connections closed, the API's own jobs cancelled/// and their threads finished, the API's thread gone.+ ///+ /// The jobs end before the callers are hung up, so a caller whose+ /// export was cancelled hears `export.failed` for it rather than a+ /// connection that just ends after the last progress (#431).pub fn stop(mut self) {- self.close_doors();+ self.close_listeners();+ self.stopper.cancel_all();+ self.hub.close();self.finish();+ self.hang_up(true);}/// Stops the server without waiting for the API's thread: when this@@ -382,6 +391,18 @@ impl Server {/// the listeners joined, the connections hung up, the jobs cancelled/// and the queue closed.fn close_doors(&mut self) {+ self.close_listeners();+ self.hang_up(false);+ // Before the queue closes: the API's thread waits for its jobs as+ // it ends, and a job told to stop ends at its next frame instead+ // of its last.+ self.stopper.cancel_all();+ self.hub.close();+ }++ /// Takes the server off the list and stops every listener, so nobody+ /// new comes in; who is in already stays connected.+ fn close_listeners(&mut self) {// Unlisted first, so nobody new is sent to a door that is closing.if let Some(path) = self.listing.take() {discovery::remove(&path);@@ -404,20 +425,24 @@ impl Server {for thread in self.listeners.drain(..) {let _ = thread.join();}- if let Ok(mut connections) = self.connections.lock() {- for (_, close) in std::mem::take(&mut *connections) {- close();- }- }#[cfg(unix)]if let Some(path) = self.socket.take() {let _ = std::fs::remove_file(path);}- // Before the queue closes: the API's thread waits for its jobs as- // it ends, and a job told to stop ends at its next frame instead- // of its last.- self.stopper.cancel_all();- self.hub.close();+ }++ /// Closes every connection. With `drain`, each writes out what it has+ /// queued first; see [`Connections`].+ fn hang_up(&mut self, drain: bool) {+ // Taken out of the lock before any is closed: a connection that+ // ends takes itself out of the same map.+ let connections = match self.connections.lock() {+ Ok(mut connections) => std::mem::take(&mut *connections),+ Err(_) => return,+ };+ for (_, close) in connections {+ close(drain);+ }}fn finish(&mut self) {diff --git a/src/crates/concat/src/lib.rs b/src/crates/concat/src/lib.rsindex 3d8dfcd5..926185ee 100644--- a/src/crates/concat/src/lib.rs+++ b/src/crates/concat/src/lib.rs@@ -2427,10 +2427,56 @@ fn start(project: Option<std::path::PathBuf>, smoke: bool) -> Result<(), slint::std::mem::forget(startup_mark);leave(1);}+ stop_exports();drop(startup_mark);leave(0);}+/// How long a quit waits for an export it cancelled to stop.+const EXPORT_PATIENCE: std::time::Duration = std::time::Duration::from_secs(10);++/// Ends what is still rendering before the process goes, so a quit in the+/// middle of an export leaves no half-written `.{name}.concat-*` file+/// beside the output: the window's export is cancelled, and the API's+/// server is stopped - its jobs cancelled, the caller of each told+/// `export.failed`, the callers hung up (#431). A render stops at its next+/// frame and removes its files on the way out; one that does not stop+/// within [`EXPORT_PATIENCE`] does not keep the quit from quitting.+fn stop_exports() {+ let mut held = None;+ Shell::with(|shell, _| {+ let mut studio = shell.studio.borrow_mut();+ held = Some((studio.host.exporter.clone(), studio.host.server.take()));+ });+ let Some((exporter, server)) = held else {+ return;+ };+ if !exporter.is_busy() && server.is_none() {+ return;+ }+ let until = std::time::Instant::now() + EXPORT_PATIENCE;+ // The server first: it cancels its own jobs, so each ends as a cancel+ // its caller can tell apart from a failure.+ if let Some(server) = server {+ log::info!("close: stopping the API's server");+ let (stopped, wait) = std::sync::mpsc::channel();+ let _ = std::thread::Builder::new()+ .name("concat server stop".to_owned())+ .spawn(move || {+ server.stop();+ let _ = stopped.send(());+ });+ let _ = wait.recv_timeout(EXPORT_PATIENCE);+ }+ exporter.cancel();+ while exporter.is_busy() && std::time::Instant::now() < until {+ std::thread::sleep(std::time::Duration::from_millis(20));+ }+ if exporter.is_busy() {+ log::warn!("close: the export did not stop in time; leaving anyway");+ }+}+/// How long a launch runs before it counts as having started.const SAFE_AFTER: std::time::Duration = std::time::Duration::from_secs(15);