Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: guard active exports from app exits

Richie McIlroycc2a871b72b1
diff --git a/apps/desktop/src-tauri/src/export.rs b/apps/desktop/src-tauri/src/export.rs
index 4edbacee6..3730060a6 100644
--- a/apps/desktop/src-tauri/src/export.rs
+++ b/apps/desktop/src-tauri/src/export.rs
@@ -17,7 +17,7 @@ use std::{
process::Stdio,
sync::{
Arc,
- atomic::{AtomicBool, Ordering},
+ atomic::{AtomicBool, AtomicUsize, Ordering},
},
};
use tokio::io::AsyncBufReadExt;
@@ -62,6 +62,7 @@ async fn run_protected_export(
const EXPORTER_ENV_BIN_PATH: &str = "CAP_EXPORTER_BIN";
const EXPORTER_STDERR_TAIL_LIMIT: usize = 80;
+static ACTIVE_EXPORT_SESSIONS: AtomicUsize = AtomicUsize::new(0);
#[cfg(windows)]
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
@@ -97,6 +98,68 @@ impl ExportProgress {
}
}
+fn retain_export_session() {
+ let active_exports = ACTIVE_EXPORT_SESSIONS.fetch_add(1, Ordering::AcqRel) + 1;
+ info!(active_exports, "Export session guard started");
+}
+
+fn release_export_session() {
+ let mut current = ACTIVE_EXPORT_SESSIONS.load(Ordering::Acquire);
+ loop {
+ if current == 0 {
+ tracing::warn!("Export session guard release requested with no active exports");
+ return;
+ }
+
+ match ACTIVE_EXPORT_SESSIONS.compare_exchange(
+ current,
+ current - 1,
+ Ordering::AcqRel,
+ Ordering::Acquire,
+ ) {
+ Ok(_) => {
+ info!(
+ active_exports = current - 1,
+ "Export session guard released"
+ );
+ return;
+ }
+ Err(next) => current = next,
+ }
+ }
+}
+
+pub fn export_session_active() -> bool {
+ ACTIVE_EXPORT_SESSIONS.load(Ordering::Acquire) > 0
+}
+
+#[tauri::command]
+#[specta::specta]
+pub fn begin_export_session() {
+ retain_export_session();
+}
+
+#[tauri::command]
+#[specta::specta]
+pub fn end_export_session() {
+ release_export_session();
+}
+
+struct ExportSessionGuard;
+
+impl ExportSessionGuard {
+ fn new() -> Self {
+ retain_export_session();
+ Self
+ }
+}
+
+impl Drop for ExportSessionGuard {
+ fn drop(&mut self) {
+ release_export_session();
+ }
+}
+
#[cfg(windows)]
fn configure_exporter_command(command: &mut tokio::process::Command) {
command.creation_flags(CREATE_NO_WINDOW);
@@ -524,6 +587,7 @@ async fn export_video_inner(
editor: OptionalWindowEditorInstance,
progress: ExportProgress,
) -> Result<PathBuf, String> {
+ let _session_guard = ExportSessionGuard::new();
let force_ffmpeg = should_force_ffmpeg_export(&project_path, &settings);
info!(
project_path = %project_path.display(),
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index c6e0fbae7..4b6cc2bf3 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -1674,6 +1674,11 @@ fn finalize_app_exit(app: &AppHandle, exit_code: i32) {
}
pub async fn request_app_exit(app: AppHandle) {
+ if export::export_session_active() {
+ warn!("Ignoring app exit request during active export");
+ return;
+ }
+
let Some(exit_state) = app.try_state::<AppExitState>() else {
warn!("Exit state unavailable while requesting app exit");
finalize_app_exit(&app, 0);
@@ -3917,6 +3922,8 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) {
fake_window::remove_fake_window,
focus_captures_panel,
get_current_recording,
+ export::begin_export_session,
+ export::end_export_session,
export::export_video,
export::export_video_no_progress,
export::get_export_estimates,
@@ -4446,6 +4453,15 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) {
match event {
WindowEvent::CloseRequested { api, .. } => {
+ if export::export_session_active() {
+ api.prevent_close();
+ warn!(
+ window = label,
+ "Preventing window close request during active export"
+ );
+ return;
+ }
+
if let Ok(window_id) = CapWindowId::from_str(label) {
match window_id {
CapWindowId::Camera => {
@@ -4891,6 +4907,11 @@ fn handle_run_event(_handle: &AppHandle, event: tauri::RunEvent) {
api.prevent_exit();
+ if export::export_session_active() {
+ warn!("Preventing app exit request during active export");
+ return;
+ }
+
let _ = code;
let handle = _handle.clone();
spawn_on_runtime(async move {
diff --git a/apps/desktop/src/utils/export.ts b/apps/desktop/src/utils/export.ts
index dee485d2d..389921b38 100644
--- a/apps/desktop/src/utils/export.ts
+++ b/apps/desktop/src/utils/export.ts
@@ -7,13 +7,33 @@ export function createExportTask(
onProgress: (progress: FramesRendered) => void,
) {
if (!import.meta.env.DEV) {
- onProgress({ renderedCount: 0, totalFrames: 1, type: "FramesRendered" });
+ const promise = (async () => {
+ await invoke("begin_export_session");
+ try {
+ const exportPromise = invoke<string>("export_video_no_progress", {
+ projectPath,
+ settings,
+ });
+ try {
+ onProgress({
+ renderedCount: 0,
+ totalFrames: 1,
+ type: "FramesRendered",
+ });
+ } catch (error) {
+ console.error("Failed to handle initial export progress", error);
+ }
+ return await exportPromise;
+ } finally {
+ await invoke("end_export_session").catch((error) => {
+ console.error("Failed to release export session guard", error);
+ });
+ }
+ })();
+
return {
cancel: () => {},
- promise: invoke<string>("export_video_no_progress", {
- projectPath,
- settings,
- }),
+ promise,
};
}