@@ -683,13 +683,35 @@ fn restore_changelogs( |
683 | 683 | Ok(changelogs) |
684 | 684 | } |
685 | 685 |
|
686 | | -/// Writes a snapshot to `path` atomically: the new document lands |
687 | | -/// under a temporary name in the same directory and is renamed over |
688 | | -/// the old one, so a crash mid-write leaves the previous snapshot |
689 | | -/// (or none), never a torn one for the next restart to read. |
| 686 | +/// Writes a snapshot to `path` durably and atomically, through the |
| 687 | +/// crate-wide durable-write idiom (#16358): the new document is |
| 688 | +/// written under a temporary name in the same directory, fsynced, |
| 689 | +/// renamed over the old one, and the parent directory is synced |
| 690 | +/// after the rename. A crash mid-write therefore leaves the previous |
| 691 | +/// snapshot (or none) — never a torn one for the next restart to |
| 692 | +/// read — and once this returns `Ok(())` the new snapshot itself |
| 693 | +/// survives a crash: neither its bytes nor the rename that installed |
| 694 | +/// it can be silently rolled back. A failure of the post-rename |
| 695 | +/// directory sync surfaces as an error, not success, even though the |
| 696 | +/// new file is already installed at that point. |
690 | 697 | fn write_note_snapshot_file( |
691 | 698 | path: &std::path::Path, |
692 | 699 | snapshot: &NoteCheckpointSnapshot, |
| 700 | +) -> Result<(), NoteSnapshotError> { |
| 701 | + write_note_snapshot_file_with_parent_sync(path, snapshot, |dir| { |
| 702 | + std::fs::File::open(dir)?.sync_all() |
| 703 | + }) |
| 704 | +} |
| 705 | + |
| 706 | +/// [`write_note_snapshot_file`] with the parent-directory sync step |
| 707 | +/// supplied by the caller: the same injectable seam |
| 708 | +/// [`id_core::durable_write::write_durable_with_parent_sync`] |
| 709 | +/// provides, so a test can observe — and inject — the post-rename |
| 710 | +/// sync failure. |
| 711 | +fn write_note_snapshot_file_with_parent_sync( |
| 712 | + path: &std::path::Path, |
| 713 | + snapshot: &NoteCheckpointSnapshot, |
| 714 | + sync_parent: impl FnOnce(&std::path::Path) -> std::io::Result<()>, |
693 | 715 | ) -> Result<(), NoteSnapshotError> { |
694 | 716 | let bytes = |
695 | 717 | serde_json::to_vec_pretty(snapshot).map_err(|source| NoteSnapshotError::Corrupt { |
@@ -699,14 +721,11 @@ fn write_note_snapshot_file( |
699 | 721 | let mut tmp_os = path.as_os_str().to_owned(); |
700 | 722 | tmp_os.push(".tmp"); |
701 | 723 | let tmp = PathBuf::from(tmp_os); |
702 | | - std::fs::write(&tmp, bytes).map_err(|source| NoteSnapshotError::Io { |
703 | | - path: path.to_path_buf(), |
704 | | - source, |
705 | | - })?; |
706 | | - std::fs::rename(&tmp, path).map_err(|source| NoteSnapshotError::Io { |
707 | | - path: path.to_path_buf(), |
708 | | - source, |
709 | | - })?; |
| 724 | + id_core::durable_write::write_durable_with_parent_sync(path, &tmp, &bytes, sync_parent) |
| 725 | + .map_err(|source| NoteSnapshotError::Io { |
| 726 | + path: path.to_path_buf(), |
| 727 | + source: std::io::Error::other(source), |
| 728 | + })?; |
710 | 729 | Ok(()) |
711 | 730 | } |
712 | 731 |
|
@@ -3964,6 +3983,69 @@ mod tests { |
3964 | 3983 | assert!(!fresh.restore_persisted_note_snapshot().await.unwrap()); |
3965 | 3984 | } |
3966 | 3985 |
|
| 3986 | + #[test] |
| 3987 | + fn test_note_snapshot_file_write_round_trips_and_leaves_no_temp() { |
| 3988 | + // #16358: the persist half of the snapshot round-trip — what |
| 3989 | + // write_note_snapshot_file lands at the path parses back as |
| 3990 | + // the same snapshot, a rewrite replaces it, and no temp file |
| 3991 | + // is left behind. |
| 3992 | + let dir = tempfile::tempdir().unwrap(); |
| 3993 | + let path = dir.path().join(".note-checkpoints.json"); |
| 3994 | + let snapshot = NoteCheckpointSnapshot { |
| 3995 | + events: Vec::new(), |
| 3996 | + policies: Vec::new(), |
| 3997 | + changelogs: Vec::new(), |
| 3998 | + grants: None, |
| 3999 | + }; |
| 4000 | + |
| 4001 | + write_note_snapshot_file(&path, &snapshot).unwrap(); |
| 4002 | + let persisted: NoteCheckpointSnapshot = |
| 4003 | + serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap(); |
| 4004 | + assert_eq!(persisted, snapshot); |
| 4005 | + |
| 4006 | + write_note_snapshot_file(&path, &snapshot).unwrap(); |
| 4007 | + let persisted: NoteCheckpointSnapshot = |
| 4008 | + serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap(); |
| 4009 | + assert_eq!(persisted, snapshot); |
| 4010 | + |
| 4011 | + let mut tmp_os = path.as_os_str().to_owned(); |
| 4012 | + tmp_os.push(".tmp"); |
| 4013 | + assert!(!PathBuf::from(tmp_os).exists()); |
| 4014 | + } |
| 4015 | + |
| 4016 | + #[test] |
| 4017 | + fn test_note_snapshot_parent_sync_failure_is_an_error_not_success() { |
| 4018 | + // #16358: a post-rename parent-directory sync failure must |
| 4019 | + // surface as an error — reporting success would claim a |
| 4020 | + // durability the rename does not have. The new file is |
| 4021 | + // already installed at that point (the AfterRename stage); |
| 4022 | + // the error is how the caller learns the install is not yet |
| 4023 | + // durable. |
| 4024 | + let dir = tempfile::tempdir().unwrap(); |
| 4025 | + let path = dir.path().join(".note-checkpoints.json"); |
| 4026 | + let snapshot = NoteCheckpointSnapshot { |
| 4027 | + events: Vec::new(), |
| 4028 | + policies: Vec::new(), |
| 4029 | + changelogs: Vec::new(), |
| 4030 | + grants: None, |
| 4031 | + }; |
| 4032 | + |
| 4033 | + let err = write_note_snapshot_file_with_parent_sync(&path, &snapshot, |_| { |
| 4034 | + Err(std::io::Error::other("injected parent sync failure")) |
| 4035 | + }) |
| 4036 | + .unwrap_err(); |
| 4037 | + |
| 4038 | + assert!( |
| 4039 | + matches!(err, NoteSnapshotError::Io { .. }), |
| 4040 | + "a parent sync failure is an I/O error, not success: {err}" |
| 4041 | + ); |
| 4042 | + assert!( |
| 4043 | + err.to_string().contains("AfterRename"), |
| 4044 | + "the error names the post-rename stage: {err}" |
| 4045 | + ); |
| 4046 | + assert!(path.exists(), "the rename already installed the file"); |
| 4047 | + } |
| 4048 | + |
3967 | 4049 | /// A note owned by `owner`, scoped so nobody else reads it, in a |
3968 | 4050 | /// state persisting to `path`; returns the state and the note. |
3969 | 4051 | async fn private_note_state(path: PathBuf, owner: AuthorKey, byte: u8) -> (CollabState, Hash) { |
|