Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): address PR review findings for stop reliability and diagnostics

Richie McIlroy7bd6ee3749bf
diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rs
index 98fd4aafb..c933ab89c 100644
--- a/apps/desktop/src-tauri/src/recording.rs
+++ b/apps/desktop/src-tauri/src/recording.rs
@@ -1889,11 +1889,14 @@ async fn handle_recording_finish(
let _ = screenshot_task.await;
- if let Ok(bytes) = compress_image(display_screenshot).await.map_err(|err| {
- error!(
- "Error compressing thumbnail for instant mode progressive upload: {err}"
- )
- }) {
+ if upload_succeeded
+ && let Ok(bytes) =
+ compress_image(display_screenshot).await.map_err(|err| {
+ error!(
+ "Error compressing thumbnail for instant mode progressive upload: {err}"
+ )
+ })
+ {
let res = crate::upload::singlepart_uploader(
app.clone(),
crate::api::PresignedS3PutRequest {
diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rs
index f0874fb18..e1b53d046 100644
--- a/crates/recording/src/instant_recording.rs
+++ b/crates/recording/src/instant_recording.rs
@@ -173,13 +173,19 @@ impl Message<Stop> for Actor {
let has_init = segments_dir.join("init.mp4").exists();
let has_segments = has_init
- && std::fs::read_dir(&segments_dir)
- .map(|entries| {
- entries
- .filter_map(Result::ok)
- .any(|e| e.path().extension().is_some_and(|ext| ext == "m4s"))
- })
- .unwrap_or(false);
+ && match std::fs::read_dir(&segments_dir) {
+ Ok(entries) => entries
+ .filter_map(Result::ok)
+ .any(|e| e.path().extension().is_some_and(|ext| ext == "m4s")),
+ Err(e) => {
+ warn!(
+ path = %segments_dir.display(),
+ error = %e,
+ "Failed to read segments directory, treating as no segments"
+ );
+ false
+ }
+ };
let health = if has_segments {
crate::RecordingHealth::Healthy
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rs
index a28acea4a..03170d033 100644
--- a/crates/recording/src/output_pipeline/core.rs
+++ b/crates/recording/src/output_pipeline/core.rs
@@ -1719,11 +1719,24 @@ impl OutputPipeline {
}
}
- let first_timestamp = tokio::time::timeout(Duration::from_secs(1), self.first_timestamp_rx)
- .await
- .ok()
- .and_then(|r| r.ok())
- .unwrap_or_else(|| Timestamp::Instant(Instant::now()));
+ let first_timestamp = match tokio::time::timeout(
+ Duration::from_secs(1),
+ self.first_timestamp_rx,
+ )
+ .await
+ {
+ Ok(Ok(ts)) => ts,
+ Ok(Err(_)) => {
+ warn!(
+ "first_timestamp channel was dropped without sending a value, defaulting to now"
+ );
+ Timestamp::Instant(Instant::now())
+ }
+ Err(_) => {
+ warn!("first_timestamp receive timed out after 1s, defaulting to now");
+ Timestamp::Instant(Instant::now())
+ }
+ };
Ok(FinishedOutputPipeline {
path: self.path,
diff --git a/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs b/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
index 02261335b..d0c2307b9 100644
--- a/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
+++ b/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
@@ -218,15 +218,21 @@ impl Muxer for MacOSFragmentedM4SMuxer {
fn stop(&mut self) {
if let Some(state) = &self.state {
- match state.video_tx.try_send(None) {
- Ok(()) => {}
- Err(std::sync::mpsc::TrySendError::Full(_)) => {
- warn!("M4S encoder channel full during stop, encoder thread may be slow");
- }
- Err(std::sync::mpsc::TrySendError::Disconnected(_)) => {
- trace!("M4S encoder channel already closed during stop");
+ if state.video_tx.try_send(None).is_ok() {
+ return;
+ }
+ for _ in 0..5 {
+ std::thread::sleep(Duration::from_millis(50));
+ match state.video_tx.try_send(None) {
+ Ok(()) => return,
+ Err(std::sync::mpsc::TrySendError::Disconnected(_)) => {
+ trace!("M4S encoder channel closed during stop retry");
+ return;
+ }
+ Err(std::sync::mpsc::TrySendError::Full(_)) => {}
}
}
+ warn!("M4S encoder channel still full after retries, finish() will deliver sentinel");
}
}
@@ -686,15 +692,23 @@ impl Muxer for MacOSFragmentedM4SCameraMuxer {
fn stop(&mut self) {
if let Some(state) = &self.state {
- match state.video_tx.try_send(None) {
- Ok(()) => {}
- Err(std::sync::mpsc::TrySendError::Full(_)) => {
- warn!("M4S camera encoder channel full during stop");
- }
- Err(std::sync::mpsc::TrySendError::Disconnected(_)) => {
- trace!("M4S camera encoder channel already closed during stop");
+ if state.video_tx.try_send(None).is_ok() {
+ return;
+ }
+ for _ in 0..5 {
+ std::thread::sleep(Duration::from_millis(50));
+ match state.video_tx.try_send(None) {
+ Ok(()) => return,
+ Err(std::sync::mpsc::TrySendError::Disconnected(_)) => {
+ trace!("M4S camera encoder channel closed during stop retry");
+ return;
+ }
+ Err(std::sync::mpsc::TrySendError::Full(_)) => {}
}
}
+ warn!(
+ "M4S camera encoder channel still full after retries, finish() will deliver sentinel"
+ );
}
}
diff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rs
index 099122877..6f0b8e56d 100644
--- a/crates/recording/src/sources/screen_capture/macos.rs
+++ b/crates/recording/src/sources/screen_capture/macos.rs
@@ -730,11 +730,11 @@ impl output_pipeline::VideoSource for VideoSource {
self.video_frame_counter.load(atomic::Ordering::Relaxed)
);
+ self.cancel_token.cancel();
+
let stop_result =
tokio::time::timeout(std::time::Duration::from_secs(5), self.capturer.stop()).await;
- self.cancel_token.cancel();
-
match stop_result {
Ok(result) => result,
Err(_) => {