From 4d50124604a8e9cecfa358a26151f73a76053c7c Mon Sep 17 00:00:00 2001 From: yeagoo Date: Sun, 25 Oct 2026 02:22:20 +0800 Subject: [PATCH 1/3] fix(gpui): journal output writes and recover interrupted ones at startup A forced quit, crash or shutdown timeout could leave hidden temporary outputs behind and, on Windows, the previous output only under its backup name. Each write now records its temporary and backup paths in a synced journal in the application data directory before creating them; at startup, while holding the instance marker, the shell restores a staged backup whose new output never arrived, removes leftover temporaries that match the generated names, and carries anything it cannot resolve forward. Co-Authored-By: Claude Opus 5.5 --- apps/gpui/crates/shell/src/main.rs | 34 +- crates/imgconvert-backend/src/convert.rs | 24 +- crates/imgconvert-backend/src/lib.rs | 1 + .../imgconvert-backend/src/write_journal.rs | 602 ++++++++++++++++++ 4 files changed, 656 insertions(+), 5 deletions(-) create mode 100644 crates/imgconvert-backend/src/write_journal.rs diff --git a/apps/gpui/crates/shell/src/main.rs b/apps/gpui/crates/shell/src/main.rs index a62da0a..5f05d22 100644 --- a/apps/gpui/crates/shell/src/main.rs +++ b/apps/gpui/crates/shell/src/main.rs @@ -3,8 +3,8 @@ use gpui_kit::Global; use imgconvert_backend::instance_lock::{self, AcquireOutcome, InstanceInfo, InstanceLock}; -use imgconvert_backend::paths; use imgconvert_backend::settings::Settings; +use imgconvert_backend::{paths, write_journal}; use imgconvert_gpui::{init_app, open_blocked_window, open_main_window, probe_driver}; use imgconvert_gpui_shared::i18n::Locale; use imgconvert_gpui_shared::{SettingsModel, perf_probe, services}; @@ -56,6 +56,11 @@ fn main() { return; } }; + // Before any conversion and while holding the marker, so every + // journal found belongs to a process that has ended. + if instance_lock.is_some() { + recover_interrupted_writes(); + } cx.set_global(HeldInstanceLock(instance_lock)); init_app(cx); match open_main_window(cx) { @@ -71,6 +76,33 @@ fn main() { }); } +/// Resolves output writes a previous run left unfinished (a forced quit, a +/// crash) and starts this run's write journal. +fn recover_interrupted_writes() { + let Some(dir) = paths::app_data_dir().map(|dir| dir.join(write_journal::JOURNAL_DIR_NAME)) + else { + return; + }; + match write_journal::recover_and_install(&dir) { + Ok(report) => { + for path in &report.restored { + eprintln!( + "imgconvert: restored the previous output {}", + path.display() + ); + } + if !report.removed.is_empty() || report.retained > 0 { + eprintln!( + "imgconvert: removed {} leftover file(s) of interrupted writes; {} kept for later", + report.removed.len(), + report.retained + ); + } + } + Err(error) => eprintln!("imgconvert: cannot start the write journal: {error}"), + } +} + /// Takes the single-instance marker. Without a data directory or on an I/O /// failure other than contention the shell still starts (and settings then /// cannot be saved either, which the panel reports). diff --git a/crates/imgconvert-backend/src/convert.rs b/crates/imgconvert-backend/src/convert.rs index 0eef9ef..8419412 100644 --- a/crates/imgconvert-backend/src/convert.rs +++ b/crates/imgconvert-backend/src/convert.rs @@ -31,6 +31,7 @@ use crate::pdf; pub use crate::pdf::PdfConvertOptions; use crate::progress::{BatchId, ProgressSink}; use crate::result_cache; +use crate::write_journal; use imgconvert_platform::{atomic_output, external_codecs}; static TMP_COUNTER: AtomicU64 = AtomicU64::new(0); @@ -1229,6 +1230,7 @@ fn temp_file(out: &Path) -> Result<(PathBuf, File), ConversionError> { .map(|duration| duration.as_nanos()) .unwrap_or(0); let tmp = parent.join(format!(".imgconvert-{pid}-{nanos}-{counter}.tmp")); + write_journal::note_tmp(&tmp); match OpenOptions::new().write(true).create_new(true).open(&tmp) { Ok(file) => return Ok((tmp, file)), Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, @@ -1248,10 +1250,15 @@ fn write_output( fs::create_dir_all(parent).map_err(|e| output_io_error("无法创建输出目录", out, &e))?; } - match mode { + let _op = write_journal::Op::start(); + let result = match mode { WriteMode::Replace => replace_output(out, bytes), WriteMode::CreateNew => write_new_output(out, bytes), + }; + if let Some(parent) = out.parent() { + write_journal::sync_dir(parent); } + result } fn write_temp_output(out: &Path, bytes: &[u8]) -> Result { @@ -1310,6 +1317,7 @@ where let cleanup = cleanup_partial_note(tmp); error.append(&cleanup) })?; + write_journal::note_backup(out, &backup); if let Err(error) = rename(out, &backup) { if error.kind() == std::io::ErrorKind::NotFound { // The destination disappeared after the initial replacement attempt. It is now safe @@ -1336,7 +1344,11 @@ where let detail = output_io_error("无法写入输出文件", out, &rename_error); match rename(&backup, out) { Ok(()) => Err(detail.append(&format!("; 原文件已恢复{cleanup}"))), - Err(restore_error) => Err(detail.append(&format!( + Err(restore_error) => Err({ + write_journal::retain_current(); + detail + } + .append(&format!( "; 无法恢复原文件,备份位于 {}: {restore_error}{cleanup}", backup.display() ))), @@ -1441,8 +1453,12 @@ fn write_new_output(out: &Path, bytes: &[u8]) -> Result, Ok(atomic_output::Committed { leftover_temp: Some(_), .. - }) => Ok(Some(ConvertWarning::TemporaryFileRetained { - path: tmp.to_string_lossy().to_string(), + }) => Ok(Some({ + // The next start removes the leftover link. + write_journal::retain_current(); + ConvertWarning::TemporaryFileRetained { + path: tmp.to_string_lossy().to_string(), + } })), Err(error) => { let detail = conversion_error_for_commit(out, &error); diff --git a/crates/imgconvert-backend/src/lib.rs b/crates/imgconvert-backend/src/lib.rs index 0cf394b..18ebae0 100644 --- a/crates/imgconvert-backend/src/lib.rs +++ b/crates/imgconvert-backend/src/lib.rs @@ -25,6 +25,7 @@ pub mod progress; pub mod result_cache; pub mod service; pub mod thumbnail; +pub mod write_journal; /// Settings persistence, per-channel paths and the single-instance marker /// live in the platform crate; shells reach them through this facade. diff --git a/crates/imgconvert-backend/src/write_journal.rs b/crates/imgconvert-backend/src/write_journal.rs new file mode 100644 index 0000000..b74416f --- /dev/null +++ b/crates/imgconvert-backend/src/write_journal.rs @@ -0,0 +1,602 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (C) 2026 ImgConvert contributors + +//! Write-ahead journal for output writes (docs/GPUI_MACOS_LIFECYCLE_PLAN.md +//! §2.3, L5). +//! +//! An output write creates a hidden temporary file next to the output and, +//! when an existing output has to be staged aside (Windows), an +//! `imgconvert-backup-*` file. A forced quit, crash or shutdown timeout +//! between those steps leaves them behind, and in the staging case the +//! user's previous output only exists under the backup name. +//! +//! Before each of those files is created the path is appended to this +//! process's journal and synced; when the write is over a terminal record +//! follows. The journal lives in the application data directory, never in +//! the user's folders, and the user's folders are never scanned: recovery +//! only touches paths the journal names. +//! +//! The shell calls [`recover_and_install`] once at startup, after it holds +//! the single-instance marker and before any conversion: every journal it +//! finds then belongs to a process that has ended. Without an installed +//! journal (tests, the Tauri shell) recording does nothing. + +use std::cell::Cell; +use std::collections::BTreeMap; +use std::fs::{self, File, OpenOptions}; +use std::io::{self, BufRead, BufReader, Write}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Mutex, OnceLock}; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde::{Deserialize, Serialize}; + +/// The journal directory's name under the application data directory. +pub const JOURNAL_DIR_NAME: &str = "pending-writes"; + +static JOURNAL: OnceLock = OnceLock::new(); +static NEXT_OP: AtomicU64 = AtomicU64::new(1); + +thread_local! { + /// The write in progress on this thread (writes are synchronous). + static CURRENT: Cell> = const { Cell::new(None) }; + /// Set when the current write must stay unresolved. + static RETAINED: Cell = const { Cell::new(false) }; +} + +struct Journal { + file: Mutex, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "kind", rename_all = "camelCase")] +enum Record { + /// A temporary output is about to be created. + Tmp { op: u64, path: PathBuf }, + /// `out` is about to be moved to `backup`. + Backup { + op: u64, + out: PathBuf, + path: PathBuf, + }, + /// The write is over; nothing of it needs recovery. + End { op: u64 }, +} + +impl Record { + fn op(&self) -> u64 { + match self { + Record::Tmp { op, .. } | Record::Backup { op, .. } | Record::End { op } => *op, + } + } +} + +fn append(record: &Record, sync: bool) { + let Some(journal) = JOURNAL.get() else { + return; + }; + let Ok(mut line) = serde_json::to_vec(record) else { + return; + }; + line.push(b'\n'); + let Ok(mut file) = journal.file.lock() else { + return; + }; + // Best effort: a write that cannot be journaled still proceeds, as it + // did before the journal existed. + if file.write_all(&line).is_ok() && sync { + let _ = file.sync_data(); + } +} + +/// One output write. Dropping it records the end of the write unless +/// [`retain_current`] was called during it. +pub(crate) struct Op { + id: Option, +} + +impl Op { + /// Starts a write on this thread. + pub(crate) fn start() -> Self { + if JOURNAL.get().is_none() { + return Self { id: None }; + } + let id = NEXT_OP.fetch_add(1, Ordering::Relaxed); + CURRENT.with(|current| current.set(Some(id))); + RETAINED.with(|retained| retained.set(false)); + Self { id: Some(id) } + } +} + +/// Leaves the current write unresolved: the previous output could not be +/// put back, and the next start retries. +pub(crate) fn retain_current() { + RETAINED.with(|retained| retained.set(true)); +} + +impl Drop for Op { + fn drop(&mut self) { + let Some(op) = self.id else { + return; + }; + CURRENT.with(|current| current.set(None)); + if !RETAINED.with(Cell::take) { + // Not synced: a lost end record only means recovery finds the + // files already gone. + append(&Record::End { op }, false); + } + } +} + +fn current() -> Option { + CURRENT.with(Cell::get) +} + +/// Records a temporary output before it is created. +pub(crate) fn note_tmp(path: &Path) { + if let Some(op) = current() { + append( + &Record::Tmp { + op, + path: path.to_path_buf(), + }, + true, + ); + } +} + +/// Records that `out` is about to be staged aside as `backup`. +pub(crate) fn note_backup(out: &Path, backup: &Path) { + if let Some(op) = current() { + append( + &Record::Backup { + op, + out: out.to_path_buf(), + path: backup.to_path_buf(), + }, + true, + ); + } +} + +/// Syncs a directory after a rename inside it (best effort; Unix only). +pub(crate) fn sync_dir(dir: &Path) { + #[cfg(unix)] + if let Ok(handle) = File::open(dir) { + let _ = handle.sync_all(); + } + #[cfg(not(unix))] + let _ = dir; +} + +/// What recovery did, for the diagnostic log. +#[derive(Debug, Default, Clone, PartialEq, Eq)] +pub struct RecoveryReport { + /// Previous outputs moved back from their backup name. + pub restored: Vec, + /// Temporary outputs and stale backups removed. + pub removed: Vec, + /// Entries left for a later start (inaccessible, or unexpected state). + pub retained: usize, +} + +/// Recovers every journal in `dir`, then starts this process's journal there. +/// Call once, while holding the single-instance marker, before any write. +pub fn recover_and_install(dir: &Path) -> io::Result { + fs::create_dir_all(dir)?; + let report = recover(dir); + let name = format!( + "{}-{}.jsonl", + std::process::id(), + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |elapsed| elapsed.as_nanos()) + ); + let file = OpenOptions::new() + .create_new(true) + .append(true) + .open(dir.join(name))?; + let _ = JOURNAL.set(Journal { + file: Mutex::new(file), + }); + Ok(report) +} + +fn recover(dir: &Path) -> RecoveryReport { + let mut report = RecoveryReport::default(); + let Ok(entries) = fs::read_dir(dir) else { + return report; + }; + let mut journals: Vec = entries + .filter_map(Result::ok) + .map(|entry| entry.path()) + .filter(|path| path.extension().is_some_and(|ext| ext == "jsonl")) + .collect(); + journals.sort(); + let mut retained = Vec::new(); + for journal in &journals { + for (_, records) in unresolved_ops(journal) { + let mut keep = recover_op(&records, &mut report); + retained.append(&mut keep); + } + } + report.retained = retained + .iter() + .map(Record::op) + .collect::>() + .len(); + // Carry unresolved entries forward before removing the old journals, so + // a crash here loses nothing. + if !retained.is_empty() { + let carried = dir.join(format!( + "retained-{}.jsonl", + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |elapsed| elapsed.as_nanos()) + )); + let written = File::create(&carried).and_then(|mut file| { + for record in &retained { + let mut line = serde_json::to_vec(record).map_err(io::Error::other)?; + line.push(b'\n'); + file.write_all(&line)?; + } + file.sync_all() + }); + if written.is_err() { + // Keep the originals rather than risk losing the entries. + let _ = fs::remove_file(&carried); + return report; + } + } + for journal in journals { + let _ = fs::remove_file(journal); + } + sync_dir(dir); + report +} + +/// The records of every operation in `journal` that has no end record. Op +/// ids are only unique within one journal. +fn unresolved_ops(journal: &Path) -> BTreeMap> { + let mut ops: BTreeMap> = BTreeMap::new(); + let Ok(file) = File::open(journal) else { + return ops; + }; + for line in BufReader::new(file).lines() { + let Ok(line) = line else { + break; + }; + // A torn last line from a crash is skipped. + let Ok(record) = serde_json::from_str::(&line) else { + continue; + }; + match record { + Record::End { op } => { + ops.remove(&op); + } + record => ops.entry(record.op()).or_default().push(record), + } + } + ops +} + +/// Resolves one interrupted write. Returns the records to keep for later. +fn recover_op(records: &[Record], report: &mut RecoveryReport) -> Vec { + let mut keep = Vec::new(); + let tmp = records.iter().find_map(|record| match record { + Record::Tmp { path, .. } => Some(path.as_path()), + _ => None, + }); + // Backups first: whether the new output was placed is read from the + // temporary file, which the second pass removes. + let mut backup_kept = false; + for record in records { + if let Record::Backup { out, path, .. } = record { + match restore_backup(out, path, tmp) { + Ok(Some(Outcome::Restored)) => report.restored.push(out.clone()), + Ok(Some(Outcome::Removed)) => report.removed.push(path.clone()), + Ok(None) => {} + Err(()) => { + backup_kept = true; + keep.push(record.clone()); + } + } + } + } + for record in records { + if let Record::Tmp { path, .. } = record { + // While a backup is unresolved its temporary file is the + // evidence that the new output was never placed; keep both. + if backup_kept { + keep.push(record.clone()); + continue; + } + match remove_temporary(path) { + Ok(true) => report.removed.push(path.clone()), + Ok(false) => {} + Err(()) => keep.push(record.clone()), + } + } + } + keep +} + +enum Outcome { + Restored, + Removed, +} + +/// The backup holds the previous output. Put it back when the new output +/// never arrived; drop it when the new output is in place. Anything else is +/// left alone and kept. +fn restore_backup(out: &Path, backup: &Path, tmp: Option<&Path>) -> Result, ()> { + if !is_named(backup, is_backup_name) { + return Ok(None); + } + match fs::symlink_metadata(backup) { + Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None), + Err(_) => return Err(()), + Ok(_) => {} + } + match fs::symlink_metadata(out) { + Err(error) if error.kind() == io::ErrorKind::NotFound => { + fs::rename(backup, out).map_err(|_| ())?; + if let Some(parent) = out.parent() { + sync_dir(parent); + } + Ok(Some(Outcome::Restored)) + } + Err(_) => Err(()), + Ok(_) => { + // The new output was placed only if its temporary file is gone. + let placed = tmp.is_none_or(|tmp| { + matches!(fs::symlink_metadata(tmp), Err(error) if error.kind() == io::ErrorKind::NotFound) + }); + if placed { + fs::remove_file(backup).map_err(|_| ())?; + Ok(Some(Outcome::Removed)) + } else { + Err(()) + } + } + } +} + +/// Removes a leftover temporary output. Only a regular file with the +/// temporary-name pattern is removed. +fn remove_temporary(path: &Path) -> Result { + if !is_named(path, is_temporary_name) { + return Ok(false); + } + match fs::symlink_metadata(path) { + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false), + Err(_) => Err(()), + Ok(metadata) if metadata.file_type().is_file() => { + fs::remove_file(path).map_err(|_| ())?; + Ok(true) + } + // Not ours to touch. + Ok(_) => Ok(false), + } +} + +fn is_named(path: &Path, rule: fn(&str) -> bool) -> bool { + path.file_name() + .and_then(|name| name.to_str()) + .is_some_and(rule) +} + +/// `.imgconvert---.tmp` +fn is_temporary_name(name: &str) -> bool { + name.strip_prefix(".imgconvert-") + .and_then(|rest| rest.strip_suffix(".tmp")) + .is_some_and(three_numbers) +} + +/// `imgconvert-backup---` +fn is_backup_name(name: &str) -> bool { + name.strip_prefix("imgconvert-backup-") + .is_some_and(three_numbers) +} + +fn three_numbers(text: &str) -> bool { + let parts: Vec<_> = text.split('-').collect(); + parts.len() == 3 + && parts + .iter() + .all(|part| !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit())) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn journal_with(dir: &Path, records: &[Record]) { + let mut file = File::create(dir.join("1-1.jsonl")).unwrap(); + for record in records { + writeln!(file, "{}", serde_json::to_string(record).unwrap()).unwrap(); + } + } + + fn scratch(name: &str) -> (PathBuf, PathBuf) { + let root = + std::env::temp_dir().join(format!("imgconvert-journal-{name}-{}", std::process::id())); + let _ = fs::remove_dir_all(&root); + let journal = root.join("journal"); + let out = root.join("out"); + fs::create_dir_all(&journal).unwrap(); + fs::create_dir_all(&out).unwrap(); + (journal, out) + } + + #[test] + fn names_match_only_the_generated_patterns() { + assert!(is_temporary_name(".imgconvert-12-345-6.tmp")); + assert!(!is_temporary_name(".imgconvert-12-345.tmp")); + assert!(!is_temporary_name(".imgconvert-a-345-6.tmp")); + assert!(!is_temporary_name("photo.tmp")); + assert!(is_backup_name("imgconvert-backup-1-2-3")); + assert!(!is_backup_name("imgconvert-backup-1-2-3.png")); + assert!(!is_backup_name("photo.png")); + } + + #[test] + fn a_leftover_temporary_is_removed_and_a_finished_write_is_left_alone() { + let (journal, out) = scratch("tmp"); + let leftover = out.join(".imgconvert-1-1-1.tmp"); + let finished = out.join(".imgconvert-1-1-2.tmp"); + let unrelated = out.join(".imgconvert-1-1-3.tmp"); + for path in [&leftover, &finished, &unrelated] { + fs::write(path, b"x").unwrap(); + } + journal_with( + &journal, + &[ + Record::Tmp { + op: 1, + path: leftover.clone(), + }, + Record::Tmp { + op: 2, + path: finished.clone(), + }, + Record::End { op: 2 }, + Record::Tmp { + op: 3, + path: out.join("photo.png"), + }, + ], + ); + fs::write(out.join("photo.png"), b"user").unwrap(); + let report = recover(&journal); + assert_eq!(report.removed, vec![leftover.clone()]); + assert!(!leftover.exists()); + assert!(finished.exists(), "ended writes are not touched"); + assert!(unrelated.exists(), "unjournaled files are not touched"); + assert!( + out.join("photo.png").exists(), + "names outside the pattern are not touched" + ); + assert_eq!(fs::read_dir(&journal).unwrap().count(), 0); + } + + #[test] + fn an_interrupted_replacement_puts_the_previous_output_back() { + let (journal, out) = scratch("restore"); + let target = out.join("photo.avif"); + let backup = out.join("imgconvert-backup-1-1-1"); + let tmp = out.join(".imgconvert-1-1-1.tmp"); + fs::write(&backup, b"previous").unwrap(); + fs::write(&tmp, b"new").unwrap(); + journal_with( + &journal, + &[ + Record::Tmp { + op: 1, + path: tmp.clone(), + }, + Record::Backup { + op: 1, + out: target.clone(), + path: backup.clone(), + }, + ], + ); + let report = recover(&journal); + assert_eq!(report.restored, vec![target.clone()]); + assert_eq!(fs::read(&target).unwrap(), b"previous"); + assert!(!backup.exists()); + assert!(!tmp.exists()); + } + + #[test] + fn a_placed_replacement_drops_the_stale_backup() { + let (journal, out) = scratch("placed"); + let target = out.join("photo.avif"); + let backup = out.join("imgconvert-backup-1-1-1"); + fs::write(&target, b"new").unwrap(); + fs::write(&backup, b"previous").unwrap(); + journal_with( + &journal, + &[ + Record::Tmp { + op: 1, + path: out.join(".imgconvert-1-1-1.tmp"), + }, + Record::Backup { + op: 1, + out: target.clone(), + path: backup.clone(), + }, + ], + ); + let report = recover(&journal); + assert_eq!(fs::read(&target).unwrap(), b"new"); + assert!(!backup.exists()); + assert_eq!(report.removed, vec![backup]); + } + + #[test] + fn an_ambiguous_replacement_is_kept_for_later() { + let (journal, out) = scratch("ambiguous"); + let target = out.join("photo.avif"); + let backup = out.join("imgconvert-backup-1-1-1"); + let tmp = out.join(".imgconvert-1-1-1.tmp"); + // An output exists again, yet the new one was never placed. + fs::write(&target, b"other").unwrap(); + fs::write(&backup, b"previous").unwrap(); + fs::write(&tmp, b"new").unwrap(); + journal_with( + &journal, + &[ + Record::Tmp { + op: 1, + path: tmp.clone(), + }, + Record::Backup { + op: 1, + out: target.clone(), + path: backup.clone(), + }, + ], + ); + let report = recover(&journal); + assert_eq!( + fs::read(&backup).unwrap(), + b"previous", + "never deleted while ambiguous" + ); + assert!(tmp.exists()); + assert_eq!(report.retained, 1); + let carried: Vec<_> = fs::read_dir(&journal) + .unwrap() + .filter_map(Result::ok) + .collect(); + assert_eq!(carried.len(), 1, "unresolved entries are carried forward"); + // The next start sees the same state and keeps it again. + assert_eq!(recover(&journal).retained, 1); + assert!(backup.exists()); + } + + #[test] + fn a_torn_last_line_is_ignored() { + let (journal, out) = scratch("torn"); + let leftover = out.join(".imgconvert-1-1-1.tmp"); + fs::write(&leftover, b"x").unwrap(); + journal_with( + &journal, + &[Record::Tmp { + op: 1, + path: leftover.clone(), + }], + ); + let mut file = OpenOptions::new() + .append(true) + .open(journal.join("1-1.jsonl")) + .unwrap(); + write!(file, "{{\"kind\":\"end\",\"op\"").unwrap(); + recover(&journal); + assert!(!leftover.exists()); + } +} From 5da7758e02fecdbc61a4ae21bb2d5b5450b6ea6b Mon Sep 17 00:00:00 2001 From: yeagoo Date: Sun, 25 Oct 2026 02:33:05 +0800 Subject: [PATCH 2/3] fix(gpui): make the write journal fail closed and keep unresolved entries Op ids are unique across journals so carried records never merge; a backup is never staged when its journal record cannot be written; entries whose folder cannot be inspected are kept instead of treated as done; a journal damaged before its last line is kept as *.unreadable; the end of a staged write is synced; recovery is retried after an output folder is granted. Co-Authored-By: Claude Opus 5.5 --- .../crates/workflow/src/workflow_panel.rs | 12 + crates/imgconvert-backend/src/convert.rs | 7 +- .../imgconvert-backend/src/write_journal.rs | 530 +++++++++++++----- docs/GPUI_MACOS_LIFECYCLE_PLAN.md | 4 + 4 files changed, 410 insertions(+), 143 deletions(-) diff --git a/apps/gpui/crates/workflow/src/workflow_panel.rs b/apps/gpui/crates/workflow/src/workflow_panel.rs index 12d3ed3..3b0e30f 100644 --- a/apps/gpui/crates/workflow/src/workflow_panel.rs +++ b/apps/gpui/crates/workflow/src/workflow_panel.rs @@ -192,6 +192,7 @@ impl WorkflowPanel { self.notice = None; } self.output_session_grant = Some(dir.clone()); + retry_interrupted_writes(cx); let dir = dir.to_string_lossy().into_owned(); self.settings.update(cx, |settings, cx| { settings.update(cx, |settings| settings.out_dir = Some(dir)); @@ -677,6 +678,16 @@ pub fn build_options( ) } +/// A folder grant may make outputs of an earlier run reachable again (the Mac +/// App Store sandbox keeps no access across launches): retry what startup +/// recovery had to leave unresolved. +fn retry_interrupted_writes(cx: &App) { + tasks::run_blocking("imgconvert-recover", cx, || { + imgconvert_backend::write_journal::retry_retained() + }) + .detach(); +} + #[cfg(test)] mod tests { #[test] @@ -834,3 +845,4 @@ mod tests { assert!(!formats.iter().any(|format| format.as_ref() == "pdf")); } } + diff --git a/crates/imgconvert-backend/src/convert.rs b/crates/imgconvert-backend/src/convert.rs index 8419412..d398000 100644 --- a/crates/imgconvert-backend/src/convert.rs +++ b/crates/imgconvert-backend/src/convert.rs @@ -1317,7 +1317,12 @@ where let cleanup = cleanup_partial_note(tmp); error.append(&cleanup) })?; - write_journal::note_backup(out, &backup); + if let Err(error) = write_journal::note_backup(out, &backup) { + // Never stage the previous output where nothing records it. + let cleanup = cleanup_partial_note(tmp); + let detail = output_io_error("无法记录写入日志", out, &error); + return Err(detail.append(&cleanup)); + } if let Err(error) = rename(out, &backup) { if error.kind() == std::io::ErrorKind::NotFound { // The destination disappeared after the initial replacement attempt. It is now safe diff --git a/crates/imgconvert-backend/src/write_journal.rs b/crates/imgconvert-backend/src/write_journal.rs index b74416f..1ce8c9e 100644 --- a/crates/imgconvert-backend/src/write_journal.rs +++ b/crates/imgconvert-backend/src/write_journal.rs @@ -22,7 +22,7 @@ //! journal (tests, the Tauri shell) recording does nothing. use std::cell::Cell; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::fs::{self, File, OpenOptions}; use std::io::{self, BufRead, BufReader, Write}; use std::path::{Path, PathBuf}; @@ -40,103 +40,124 @@ static NEXT_OP: AtomicU64 = AtomicU64::new(1); thread_local! { /// The write in progress on this thread (writes are synchronous). - static CURRENT: Cell> = const { Cell::new(None) }; + static CURRENT: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; /// Set when the current write must stay unresolved. static RETAINED: Cell = const { Cell::new(false) }; + /// Set when the current write staged a backup; its end is then synced. + static STAGED: Cell = const { Cell::new(false) }; } struct Journal { + dir: PathBuf, + path: PathBuf, + /// Prefix that makes op ids unique across processes and carried files. + id: String, file: Mutex, } +/// Op ids are `:`, unique across every journal, so records +/// carried forward from several journals never merge. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(tag = "kind", rename_all = "camelCase")] enum Record { /// A temporary output is about to be created. - Tmp { op: u64, path: PathBuf }, + Tmp { op: String, path: PathBuf }, /// `out` is about to be moved to `backup`. Backup { - op: u64, + op: String, out: PathBuf, path: PathBuf, }, /// The write is over; nothing of it needs recovery. - End { op: u64 }, + End { op: String }, } impl Record { - fn op(&self) -> u64 { + fn op(&self) -> &str { match self { - Record::Tmp { op, .. } | Record::Backup { op, .. } | Record::End { op } => *op, + Record::Tmp { op, .. } | Record::Backup { op, .. } | Record::End { op } => op, } } } -fn append(record: &Record, sync: bool) { +fn unique_suffix() -> String { + format!( + "{}-{}", + std::process::id(), + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |elapsed| elapsed.as_nanos()) + ) +} + +fn append(record: &Record, sync: bool) -> io::Result<()> { let Some(journal) = JOURNAL.get() else { - return; - }; - let Ok(mut line) = serde_json::to_vec(record) else { - return; + return Ok(()); }; + let mut line = serde_json::to_vec(record).map_err(io::Error::other)?; line.push(b'\n'); - let Ok(mut file) = journal.file.lock() else { - return; - }; - // Best effort: a write that cannot be journaled still proceeds, as it - // did before the journal existed. - if file.write_all(&line).is_ok() && sync { - let _ = file.sync_data(); + let mut file = journal + .file + .lock() + .map_err(|_| io::Error::other("write journal lock poisoned"))?; + file.write_all(&line)?; + if sync { + file.sync_data()?; } + Ok(()) } /// One output write. Dropping it records the end of the write unless /// [`retain_current`] was called during it. pub(crate) struct Op { - id: Option, + id: Option, } impl Op { /// Starts a write on this thread. pub(crate) fn start() -> Self { - if JOURNAL.get().is_none() { + let Some(journal) = JOURNAL.get() else { return Self { id: None }; - } - let id = NEXT_OP.fetch_add(1, Ordering::Relaxed); - CURRENT.with(|current| current.set(Some(id))); + }; + let id = format!("{}:{}", journal.id, NEXT_OP.fetch_add(1, Ordering::Relaxed)); + CURRENT.with(|current| *current.borrow_mut() = Some(id.clone())); RETAINED.with(|retained| retained.set(false)); + STAGED.with(|staged| staged.set(false)); Self { id: Some(id) } } } -/// Leaves the current write unresolved: the previous output could not be -/// put back, and the next start retries. +/// Leaves the current write unresolved so the next start retries it. pub(crate) fn retain_current() { RETAINED.with(|retained| retained.set(true)); } impl Drop for Op { fn drop(&mut self) { - let Some(op) = self.id else { + let Some(op) = self.id.take() else { return; }; - CURRENT.with(|current| current.set(None)); + CURRENT.with(|current| *current.borrow_mut() = None); + let staged = STAGED.with(Cell::take); if !RETAINED.with(Cell::take) { - // Not synced: a lost end record only means recovery finds the - // files already gone. - append(&Record::End { op }, false); + // Unsynced unless a backup was staged: a lost end record then + // only means recovery finds the files already gone. With a + // backup the end is the commit marker and must not outlive a + // rename that did not reach the disk. + let _ = append(&Record::End { op }, staged); } } } -fn current() -> Option { - CURRENT.with(Cell::get) +fn current() -> Option { + CURRENT.with(|current| current.borrow().clone()) } -/// Records a temporary output before it is created. +/// Records a temporary output before it is created. Best effort: an +/// unrecorded temporary can only be left behind, never lose data. pub(crate) fn note_tmp(path: &Path) { if let Some(op) = current() { - append( + let _ = append( &Record::Tmp { op, path: path.to_path_buf(), @@ -146,21 +167,27 @@ pub(crate) fn note_tmp(path: &Path) { } } -/// Records that `out` is about to be staged aside as `backup`. -pub(crate) fn note_backup(out: &Path, backup: &Path) { - if let Some(op) = current() { - append( - &Record::Backup { - op, - out: out.to_path_buf(), - path: backup.to_path_buf(), - }, - true, - ); - } +/// Records that `out` is about to be staged aside as `backup`. The caller +/// must not stage when this fails: the previous output would otherwise only +/// exist under a name nothing points to. +pub(crate) fn note_backup(out: &Path, backup: &Path) -> io::Result<()> { + let Some(op) = current() else { + return Ok(()); + }; + append( + &Record::Backup { + op, + out: out.to_path_buf(), + path: backup.to_path_buf(), + }, + true, + )?; + STAGED.with(|staged| staged.set(true)); + Ok(()) } -/// Syncs a directory after a rename inside it (best effort; Unix only). +/// Syncs a directory after a rename inside it (best effort; Unix only, as +/// Windows offers no directory handle to flush). pub(crate) fn sync_dir(dir: &Path) { #[cfg(unix)] if let Ok(handle) = File::open(dir) { @@ -177,33 +204,45 @@ pub struct RecoveryReport { pub restored: Vec, /// Temporary outputs and stale backups removed. pub removed: Vec, - /// Entries left for a later start (inaccessible, or unexpected state). + /// Operations left for a later attempt (inaccessible, unmounted, or in + /// an unexpected state). pub retained: usize, + /// Journals that could not be read completely; renamed `*.unreadable` + /// and kept. + pub unreadable: usize, } /// Recovers every journal in `dir`, then starts this process's journal there. /// Call once, while holding the single-instance marker, before any write. pub fn recover_and_install(dir: &Path) -> io::Result { fs::create_dir_all(dir)?; - let report = recover(dir); - let name = format!( - "{}-{}.jsonl", - std::process::id(), - SystemTime::now() - .duration_since(UNIX_EPOCH) - .map_or(0, |elapsed| elapsed.as_nanos()) - ); + let report = recover(dir, None); + let id = unique_suffix(); + let path = dir.join(format!("{id}.jsonl")); let file = OpenOptions::new() .create_new(true) .append(true) - .open(dir.join(name))?; + .open(&path)?; let _ = JOURNAL.set(Journal { + dir: dir.to_path_buf(), + path, + id, file: Mutex::new(file), }); Ok(report) } -fn recover(dir: &Path) -> RecoveryReport { +/// Retries what earlier runs left unresolved, for example after the user +/// grants access to an output folder again (the Mac App Store sandbox keeps +/// no access across launches). This run's own journal is never touched. +pub fn retry_retained() -> RecoveryReport { + match JOURNAL.get() { + Some(journal) => recover(&journal.dir, Some(&journal.path)), + None => RecoveryReport::default(), + } +} + +fn recover(dir: &Path, own: Option<&Path>) -> RecoveryReport { let mut report = RecoveryReport::default(); let Ok(entries) = fs::read_dir(dir) else { return report; @@ -212,29 +251,55 @@ fn recover(dir: &Path) -> RecoveryReport { .filter_map(Result::ok) .map(|entry| entry.path()) .filter(|path| path.extension().is_some_and(|ext| ext == "jsonl")) + .filter(|path| Some(path.as_path()) != own) .collect(); journals.sort(); - let mut retained = Vec::new(); + let mut ops: BTreeMap> = BTreeMap::new(); + let mut ended = BTreeSet::new(); + let mut consumed = Vec::new(); for journal in &journals { - for (_, records) in unresolved_ops(journal) { - let mut keep = recover_op(&records, &mut report); + match read_journal(journal) { + Ok(records) => { + for record in records { + match record { + Record::End { op } => { + ended.insert(op); + } + record => ops.entry(record.op().to_string()).or_default().push(record), + } + } + consumed.push(journal.clone()); + } + Err(Unreadable::Missing) => {} + Err(Unreadable::Partly(records)) => { + // What could be read is still recovered; the file is kept + // under another name for inspection instead of retried. + for record in records { + if !matches!(record, Record::End { .. }) { + ops.entry(record.op().to_string()).or_default().push(record); + } + } + report.unreadable += 1; + let _ = fs::rename(journal, journal.with_extension("unreadable")); + } + } + } + let mut retained = Vec::new(); + for (op, records) in &ops { + if ended.contains(op) { + continue; + } + let mut keep = recover_op(records, &mut report); + if !keep.is_empty() { + report.retained += 1; retained.append(&mut keep); } } - report.retained = retained - .iter() - .map(Record::op) - .collect::>() - .len(); - // Carry unresolved entries forward before removing the old journals, so - // a crash here loses nothing. + // Carry unresolved entries forward before removing the sources, so a + // crash here loses nothing; a source that cannot be removed only + // repeats idempotent work next time. if !retained.is_empty() { - let carried = dir.join(format!( - "retained-{}.jsonl", - SystemTime::now() - .duration_since(UNIX_EPOCH) - .map_or(0, |elapsed| elapsed.as_nanos()) - )); + let carried = dir.join(format!("retained-{}.jsonl", unique_suffix())); let written = File::create(&carried).and_then(|mut file| { for record in &retained { let mut line = serde_json::to_vec(record).map_err(io::Error::other)?; @@ -244,41 +309,51 @@ fn recover(dir: &Path) -> RecoveryReport { file.sync_all() }); if written.is_err() { - // Keep the originals rather than risk losing the entries. let _ = fs::remove_file(&carried); return report; } } - for journal in journals { + for journal in consumed { let _ = fs::remove_file(journal); } sync_dir(dir); report } -/// The records of every operation in `journal` that has no end record. Op -/// ids are only unique within one journal. -fn unresolved_ops(journal: &Path) -> BTreeMap> { - let mut ops: BTreeMap> = BTreeMap::new(); - let Ok(file) = File::open(journal) else { - return ops; +enum Unreadable { + /// Gone between listing and reading. + Missing, + /// Not fully readable; the records before the damage. + Partly(Vec), +} + +/// Reads a journal. Only a torn final line (a crash mid-append) is +/// tolerated; damage anywhere else marks the journal unreadable. +fn read_journal(journal: &Path) -> Result, Unreadable> { + let file = match File::open(journal) { + Ok(file) => file, + Err(error) if error.kind() == io::ErrorKind::NotFound => return Err(Unreadable::Missing), + Err(_) => return Err(Unreadable::Partly(Vec::new())), }; + let mut records = Vec::new(); + let mut damaged = false; for line in BufReader::new(file).lines() { let Ok(line) = line else { - break; + return Err(Unreadable::Partly(records)); }; - // A torn last line from a crash is skipped. - let Ok(record) = serde_json::from_str::(&line) else { + if line.trim().is_empty() { continue; - }; - match record { - Record::End { op } => { - ops.remove(&op); - } - record => ops.entry(record.op()).or_default().push(record), + } + if damaged { + // A damaged line followed by more lines is not a torn tail. + return Err(Unreadable::Partly(records)); + } + match serde_json::from_str::(&line) { + Ok(record) => records.push(record), + Err(_) => damaged = true, } } - ops + Ok(records) } /// Resolves one interrupted write. Returns the records to keep for later. @@ -327,6 +402,23 @@ enum Outcome { Removed, } +/// Whether `path` is absent from a folder that can be inspected. An +/// unmounted volume or a folder without access is not "absent": the entry +/// is then kept. +fn absent(path: &Path) -> Result { + match fs::symlink_metadata(path) { + Ok(_) => Ok(false), + Err(error) if error.kind() == io::ErrorKind::NotFound => { + let parent = path.parent().ok_or(())?; + match fs::metadata(parent) { + Ok(metadata) if metadata.is_dir() => Ok(true), + _ => Err(()), + } + } + Err(_) => Err(()), + } +} + /// The backup holds the previous output. Put it back when the new output /// never arrived; drop it when the new output is in place. Anything else is /// left alone and kept. @@ -334,33 +426,30 @@ fn restore_backup(out: &Path, backup: &Path, tmp: Option<&Path>) -> Result return Ok(None), - Err(_) => return Err(()), - Ok(_) => {} + if absent(backup)? { + return Ok(None); } - match fs::symlink_metadata(out) { - Err(error) if error.kind() == io::ErrorKind::NotFound => { - fs::rename(backup, out).map_err(|_| ())?; - if let Some(parent) = out.parent() { - sync_dir(parent); - } - Ok(Some(Outcome::Restored)) - } - Err(_) => Err(()), - Ok(_) => { - // The new output was placed only if its temporary file is gone. - let placed = tmp.is_none_or(|tmp| { - matches!(fs::symlink_metadata(tmp), Err(error) if error.kind() == io::ErrorKind::NotFound) - }); - if placed { - fs::remove_file(backup).map_err(|_| ())?; - Ok(Some(Outcome::Removed)) - } else { - Err(()) - } + if absent(out)? { + fs::rename(backup, out).map_err(|_| ())?; + if let Some(parent) = out.parent() { + sync_dir(parent); } + return Ok(Some(Outcome::Restored)); } + // The new output was placed only if its temporary file is gone. + let placed = match tmp { + None => true, + Some(tmp) => absent(tmp)?, + }; + if !placed { + return Err(()); + } + // A staged directory link (Windows) is removed as a directory entry, + // as the regular cleanup does; nothing is removed recursively. + fs::remove_file(backup) + .or_else(|_| fs::remove_dir(backup)) + .map_err(|_| ())?; + Ok(Some(Outcome::Removed)) } /// Removes a leftover temporary output. Only a regular file with the @@ -369,16 +458,16 @@ fn remove_temporary(path: &Path) -> Result { if !is_named(path, is_temporary_name) { return Ok(false); } - match fs::symlink_metadata(path) { - Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false), - Err(_) => Err(()), - Ok(metadata) if metadata.file_type().is_file() => { - fs::remove_file(path).map_err(|_| ())?; - Ok(true) - } + if absent(path)? { + return Ok(false); + } + let metadata = fs::symlink_metadata(path).map_err(|_| ())?; + if !metadata.file_type().is_file() { // Not ours to touch. - Ok(_) => Ok(false), + return Ok(false); } + fs::remove_file(path).map_err(|_| ())?; + Ok(true) } fn is_named(path: &Path, rule: fn(&str) -> bool) -> bool { @@ -454,22 +543,22 @@ mod tests { &journal, &[ Record::Tmp { - op: 1, + op: "j:1".into(), path: leftover.clone(), }, Record::Tmp { - op: 2, + op: "j:2".into(), path: finished.clone(), }, - Record::End { op: 2 }, + Record::End { op: "j:2".into() }, Record::Tmp { - op: 3, + op: "j:3".into(), path: out.join("photo.png"), }, ], ); fs::write(out.join("photo.png"), b"user").unwrap(); - let report = recover(&journal); + let report = recover(&journal, None); assert_eq!(report.removed, vec![leftover.clone()]); assert!(!leftover.exists()); assert!(finished.exists(), "ended writes are not touched"); @@ -493,17 +582,17 @@ mod tests { &journal, &[ Record::Tmp { - op: 1, + op: "j:1".into(), path: tmp.clone(), }, Record::Backup { - op: 1, + op: "j:1".into(), out: target.clone(), path: backup.clone(), }, ], ); - let report = recover(&journal); + let report = recover(&journal, None); assert_eq!(report.restored, vec![target.clone()]); assert_eq!(fs::read(&target).unwrap(), b"previous"); assert!(!backup.exists()); @@ -521,17 +610,17 @@ mod tests { &journal, &[ Record::Tmp { - op: 1, + op: "j:1".into(), path: out.join(".imgconvert-1-1-1.tmp"), }, Record::Backup { - op: 1, + op: "j:1".into(), out: target.clone(), path: backup.clone(), }, ], ); - let report = recover(&journal); + let report = recover(&journal, None); assert_eq!(fs::read(&target).unwrap(), b"new"); assert!(!backup.exists()); assert_eq!(report.removed, vec![backup]); @@ -551,17 +640,17 @@ mod tests { &journal, &[ Record::Tmp { - op: 1, + op: "j:1".into(), path: tmp.clone(), }, Record::Backup { - op: 1, + op: "j:1".into(), out: target.clone(), path: backup.clone(), }, ], ); - let report = recover(&journal); + let report = recover(&journal, None); assert_eq!( fs::read(&backup).unwrap(), b"previous", @@ -575,7 +664,7 @@ mod tests { .collect(); assert_eq!(carried.len(), 1, "unresolved entries are carried forward"); // The next start sees the same state and keeps it again. - assert_eq!(recover(&journal).retained, 1); + assert_eq!(recover(&journal, None).retained, 1); assert!(backup.exists()); } @@ -587,7 +676,7 @@ mod tests { journal_with( &journal, &[Record::Tmp { - op: 1, + op: "j:1".into(), path: leftover.clone(), }], ); @@ -596,7 +685,164 @@ mod tests { .open(journal.join("1-1.jsonl")) .unwrap(); write!(file, "{{\"kind\":\"end\",\"op\"").unwrap(); - recover(&journal); + recover(&journal, None); assert!(!leftover.exists()); } + + #[test] + fn an_unavailable_output_folder_keeps_the_entries() { + let (journal, out) = scratch("unmounted"); + let gone = out.join("unmounted-volume"); + journal_with( + &journal, + &[ + Record::Tmp { + op: "j:1".into(), + path: gone.join(".imgconvert-1-1-1.tmp"), + }, + Record::Backup { + op: "j:1".into(), + out: gone.join("photo.avif"), + path: gone.join("imgconvert-backup-1-1-1"), + }, + ], + ); + let report = recover(&journal, None); + assert_eq!( + report.retained, 1, + "a missing folder is not proof of completion" + ); + assert!(report.removed.is_empty() && report.restored.is_empty()); + assert_eq!(fs::read_dir(&journal).unwrap().count(), 1); + } + + #[test] + fn damage_before_the_last_line_keeps_the_journal() { + let (journal, out) = scratch("damaged"); + let leftover = out.join(".imgconvert-1-1-1.tmp"); + fs::write(&leftover, b"x").unwrap(); + let mut file = File::create(journal.join("1-1.jsonl")).unwrap(); + writeln!(file, "not json").unwrap(); + writeln!( + file, + "{}", + serde_json::to_string(&Record::Tmp { + op: "j:1".into(), + path: leftover.clone(), + }) + .unwrap() + ) + .unwrap(); + drop(file); + let report = recover(&journal, None); + assert_eq!(report.unreadable, 1); + assert!( + journal.join("1-1.unreadable").exists(), + "kept for inspection" + ); + assert!(!journal.join("1-1.jsonl").exists(), "not retried forever"); + } + + #[test] + fn operations_of_different_journals_never_merge() { + let (journal, out) = scratch("ids"); + let target = out.join("photo.avif"); + let backup = out.join("imgconvert-backup-1-1-1"); + let other_tmp = out.join(".imgconvert-2-2-2.tmp"); + fs::write(&target, b"other").unwrap(); + fs::write(&backup, b"previous").unwrap(); + // The backup's own write left its temporary behind (never placed); + // an unrelated journal's finished write must not count as placement. + fs::write(out.join(".imgconvert-1-1-1.tmp"), b"new").unwrap(); + let mut a = File::create(journal.join("a.jsonl")).unwrap(); + for record in [ + Record::Tmp { + op: "a:1".into(), + path: out.join(".imgconvert-1-1-1.tmp"), + }, + Record::Backup { + op: "a:1".into(), + out: target.clone(), + path: backup.clone(), + }, + ] { + writeln!(a, "{}", serde_json::to_string(&record).unwrap()).unwrap(); + } + let mut b = File::create(journal.join("b.jsonl")).unwrap(); + writeln!( + b, + "{}", + serde_json::to_string(&Record::Tmp { + op: "b:1".into(), + path: other_tmp, + }) + .unwrap() + ) + .unwrap(); + drop((a, b)); + recover(&journal, None); + assert_eq!(fs::read(&backup).unwrap(), b"previous"); + } + + #[test] + fn retrying_skips_nothing_but_this_runs_journal() { + let (journal, out) = scratch("retry"); + let leftover = out.join(".imgconvert-1-1-1.tmp"); + fs::write(&leftover, b"x").unwrap(); + journal_with( + &journal, + &[Record::Tmp { + op: "j:1".into(), + path: leftover.clone(), + }], + ); + let own = journal.join("1-1.jsonl"); + assert_eq!(recover(&journal, Some(&own)), RecoveryReport::default()); + assert!(leftover.exists()); + assert!(!recover(&journal, None).removed.is_empty()); + } + + /// The real write path journals every operation and ends each one. The + /// journal is process-wide; other tests in this binary then journal into + /// the same scratch folder, which changes nothing they observe. + #[test] + fn real_writes_are_journaled_and_ended() { + let (journal, out) = scratch("e2e"); + recover_and_install(&journal).unwrap(); + let own = JOURNAL.get().unwrap().path.clone(); + let input = out.join("in.png"); + fs::write(&input, crate::convert::tests::one_by_one_png()).unwrap(); + let mut options = crate::convert::default_conversion_options( + input.to_string_lossy().into_owned(), + Some(out.join("converted").to_string_lossy().into_owned()), + "png".into(), + ); + options.overwrite = true; + options.overwrite_mode = Some("overwrite".into()); + crate::convert::convert(&options).unwrap(); + crate::convert::convert(&options).unwrap(); + let records = read_journal(&own).ok().unwrap(); + let mine: Vec<_> = records + .iter() + .filter(|record| match record { + Record::Tmp { path, .. } => path.starts_with(out.join("converted")), + _ => false, + }) + .map(|record| record.op().to_string()) + .collect(); + assert_eq!(mine.len(), 2, "one temporary per write: {records:?}"); + for op in &mine { + assert!( + records + .iter() + .any(|record| matches!(record, Record::End { op: ended } if ended == op)), + "{op} has an end record" + ); + } + assert_eq!( + retry_retained(), + RecoveryReport::default(), + "own journal untouched" + ); + } } diff --git a/docs/GPUI_MACOS_LIFECYCLE_PLAN.md b/docs/GPUI_MACOS_LIFECYCLE_PLAN.md index 184c944..2e79b2d 100644 --- a/docs/GPUI_MACOS_LIFECYCLE_PLAN.md +++ b/docs/GPUI_MACOS_LIFECYCLE_PLAN.md @@ -145,3 +145,7 @@ - PR-A:活动判定只看批次(`progress_bridge::is_busy`、`BatchService::is_active`)。规划中或输出安全确认中退出不再询问:此时尚未写任何文件,退出闩会拒绝之后的开始。 - PR-A:确认前不关闭已打开的对话框:批次运行时不能再开始新批次,已打开的对话框不会导致写入。 - PR-A:草稿只在最终退出时提交(“继续转换”不改动草稿),提交绕过表单的忙碌闸门。 +- PR-B:恢复在持有单实例标记后于启动时同步执行,不另设执行闸门:此时尚无批次,且所有已有日志都属于已结束的进程,因此不需要 pid 判断。操作 id 为“日志 id:序号”,跨日志唯一。 +- PR-B:只有暂存备份的写入在结束时同步落盘结束记录;Windows 无法刷新目录,结束记录的持久性依赖卷缓存。 +- PR-B:输出所在文件夹无法检查(未挂载、无权限)时条目保留,不视为已完成;每次选择输出文件夹后重试保留条目(覆盖 Mac App Store 沙盒重新授权的情形)。读到中间损坏的日志改名为 `*.unreadable` 保留,不再重试。 +- PR-B:写入日志记录失败时不暂存原输出(报错“无法记录写入日志”);日志无法启动时写入照常进行,与引入日志前相同。 From ee5f09338aa48731cf4596c2cc9e2413319d95f0 Mon Sep 17 00:00:00 2001 From: yeagoo Date: Sun, 25 Oct 2026 02:55:03 +0800 Subject: [PATCH 3/3] fix(gpui): serialize journal recovery and keep ambiguous backups Co-Authored-By: Claude Opus 5.5 --- .../crates/workflow/src/workflow_panel.rs | 17 +- .../imgconvert-backend/src/write_journal.rs | 197 ++++++++++++++---- docs/GPUI_MACOS_LIFECYCLE_PLAN.md | 1 + 3 files changed, 171 insertions(+), 44 deletions(-) diff --git a/apps/gpui/crates/workflow/src/workflow_panel.rs b/apps/gpui/crates/workflow/src/workflow_panel.rs index 3b0e30f..bacc5a4 100644 --- a/apps/gpui/crates/workflow/src/workflow_panel.rs +++ b/apps/gpui/crates/workflow/src/workflow_panel.rs @@ -256,6 +256,7 @@ impl WorkflowPanel { if self.planning { return; } + retry_interrupted_writes(cx); let access = imgconvert_backend::access::runtime_file_access(); if let Some(dir) = self.output_dir(cx).filter(|dir| { output_grant_valid( @@ -678,14 +679,15 @@ pub fn build_options( ) } -/// A folder grant may make outputs of an earlier run reachable again (the Mac -/// App Store sandbox keeps no access across launches): retry what startup -/// recovery had to leave unresolved. +/// Retries what startup recovery had to leave unresolved: a folder grant +/// (the Mac App Store sandbox keeps no access across launches) or a volume +/// mounted again may make it reachable. Runs only while no batch can write, +/// on this thread, so it never races an output write. It reads the journal +/// folder and, almost always, finds nothing to do. fn retry_interrupted_writes(cx: &App) { - tasks::run_blocking("imgconvert-recover", cx, || { - imgconvert_backend::write_journal::retry_retained() - }) - .detach(); + if !progress_bridge::is_busy(cx) { + imgconvert_backend::write_journal::retry_retained(); + } } #[cfg(test)] @@ -845,4 +847,3 @@ mod tests { assert!(!formats.iter().any(|format| format.as_ref() == "pdf")); } } - diff --git a/crates/imgconvert-backend/src/write_journal.rs b/crates/imgconvert-backend/src/write_journal.rs index 1ce8c9e..8cc96f2 100644 --- a/crates/imgconvert-backend/src/write_journal.rs +++ b/crates/imgconvert-backend/src/write_journal.rs @@ -36,6 +36,9 @@ use serde::{Deserialize, Serialize}; pub const JOURNAL_DIR_NAME: &str = "pending-writes"; static JOURNAL: OnceLock = OnceLock::new(); +/// One recovery at a time: two would read and delete each other's carried +/// files. +static RECOVERY: Mutex<()> = Mutex::new(()); static NEXT_OP: AtomicU64 = AtomicU64::new(1); thread_local! { @@ -243,6 +246,9 @@ pub fn retry_retained() -> RecoveryReport { } fn recover(dir: &Path, own: Option<&Path>) -> RecoveryReport { + let _serialized = RECOVERY + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); let mut report = RecoveryReport::default(); let Ok(entries) = fs::read_dir(dir) else { return report; @@ -257,30 +263,27 @@ fn recover(dir: &Path, own: Option<&Path>) -> RecoveryReport { let mut ops: BTreeMap> = BTreeMap::new(); let mut ended = BTreeSet::new(); let mut consumed = Vec::new(); + let mut damaged = Vec::new(); for journal in &journals { - match read_journal(journal) { + let records = match read_journal(journal) { Ok(records) => { - for record in records { - match record { - Record::End { op } => { - ended.insert(op); - } - record => ops.entry(record.op().to_string()).or_default().push(record), - } - } consumed.push(journal.clone()); + records } - Err(Unreadable::Missing) => {} + Err(Unreadable::Missing) => continue, + // Every readable record is still recovered; the file is kept + // under another name for inspection instead of retried. Err(Unreadable::Partly(records)) => { - // What could be read is still recovered; the file is kept - // under another name for inspection instead of retried. - for record in records { - if !matches!(record, Record::End { .. }) { - ops.entry(record.op().to_string()).or_default().push(record); - } + damaged.push(journal.clone()); + records + } + }; + for record in records { + match record { + Record::End { op } => { + ended.insert(op); } - report.unreadable += 1; - let _ = fs::rename(journal, journal.with_extension("unreadable")); + record => ops.entry(record.op().to_string()).or_default().push(record), } } } @@ -299,8 +302,10 @@ fn recover(dir: &Path, own: Option<&Path>) -> RecoveryReport { // crash here loses nothing; a source that cannot be removed only // repeats idempotent work next time. if !retained.is_empty() { - let carried = dir.join(format!("retained-{}.jsonl", unique_suffix())); - let written = File::create(&carried).and_then(|mut file| { + // Written under a name recovery ignores, then renamed into place. + let name = format!("retained-{}", unique_suffix()); + let staging = dir.join(format!("{name}.partial")); + let written = File::create(&staging).and_then(|mut file| { for record in &retained { let mut line = serde_json::to_vec(record).map_err(io::Error::other)?; line.push(b'\n'); @@ -308,14 +313,21 @@ fn recover(dir: &Path, own: Option<&Path>) -> RecoveryReport { } file.sync_all() }); - if written.is_err() { - let _ = fs::remove_file(&carried); + if written + .and_then(|()| fs::rename(&staging, dir.join(format!("{name}.jsonl")))) + .is_err() + { + let _ = fs::remove_file(&staging); return report; } } for journal in consumed { let _ = fs::remove_file(journal); } + for journal in damaged { + report.unreadable += 1; + let _ = fs::rename(&journal, journal.with_extension("unreadable")); + } sync_dir(dir); report } @@ -327,8 +339,9 @@ enum Unreadable { Partly(Vec), } -/// Reads a journal. Only a torn final line (a crash mid-append) is -/// tolerated; damage anywhere else marks the journal unreadable. +/// Reads a journal. A torn final line (a crash mid-append) is normal; any +/// other damage marks the journal unreadable, with every parseable record +/// still returned. fn read_journal(journal: &Path) -> Result, Unreadable> { let file = match File::open(journal) { Ok(file) => file, @@ -336,24 +349,31 @@ fn read_journal(journal: &Path) -> Result, Unreadable> { Err(_) => return Err(Unreadable::Partly(Vec::new())), }; let mut records = Vec::new(); + let mut pending_damage = false; let mut damaged = false; - for line in BufReader::new(file).lines() { + for line in BufReader::new(file).split(b'\n') { let Ok(line) = line else { - return Err(Unreadable::Partly(records)); + damaged = true; + break; }; - if line.trim().is_empty() { + if line.iter().all(u8::is_ascii_whitespace) { continue; } - if damaged { - // A damaged line followed by more lines is not a torn tail. - return Err(Unreadable::Partly(records)); - } - match serde_json::from_str::(&line) { - Ok(record) => records.push(record), - Err(_) => damaged = true, + // A bad line followed by more lines is not a torn tail. + damaged |= pending_damage; + match serde_json::from_slice::(&line) { + Ok(record) => { + records.push(record); + pending_damage = false; + } + Err(_) => pending_damage = true, } } - Ok(records) + if damaged { + Err(Unreadable::Partly(records)) + } else { + Ok(records) + } } /// Resolves one interrupted write. Returns the records to keep for later. @@ -427,7 +447,10 @@ fn restore_backup(out: &Path, backup: &Path, tmp: Option<&Path>) -> Result