FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Control: a connection's slot is freed even if its thread panics or can't start
Brandon Thomasdf3b4ecbde5d
diff --git a/apps/filmcraft/src/control_server.rs b/apps/filmcraft/src/control_server.rsindex d3ffc4121..d3b40f320 100644--- a/apps/filmcraft/src/control_server.rs+++ b/apps/filmcraft/src/control_server.rs@@ -46,11 +46,25 @@ fn accept_loop(listener: TcpListener, tx: Sender<ControlRequest>, ctx: egui::Con}let tx = tx.clone();let ctx = ctx.clone();- let open = Arc::clone(&open);- std::thread::spawn(move || {+ // The slot is given back when the guard drops: after `serve` returns, if it panics, or if+ // the thread can't be started (the closure, and with it the guard, is dropped).+ let slot = Slot(Arc::clone(&open));+ let spawned = std::thread::Builder::new().name("control-conn".into()).spawn(move || {+ let _slot = slot;serve(stream, &tx, &ctx);- open.fetch_sub(1, Ordering::SeqCst);});+ if let Err(e) = spawned {+ eprintln!("filmcraft: control server could not start a connection thread: {e}");+ }+ }+}++/// One counted connection; decrements the open-connection count when dropped.+struct Slot(Arc<AtomicUsize>);++impl Drop for Slot {+ fn drop(&mut self) {+ self.0.fetch_sub(1, Ordering::SeqCst);}}