diff --git a/pkgs/id/src/world_store.rs b/pkgs/id/src/world_store.rs index 5f7ad89425..939f08d508 100644 --- a/pkgs/id/src/world_store.rs +++ b/pkgs/id/src/world_store.rs @@ -84,11 +84,15 @@ impl FileJournal { good_len += read as u64; } parsed => { - // Only an unterminated or unparsable *last* line is a - // torn append; anything after it means corruption. + // An unterminated last line is a torn append: the crash + // came before the entry was acknowledged. A newline- + // terminated line that does not parse was fully written, + // so it is corruption or an entry from a newer binary, + // and it must never be dropped silently, even as the + // last line. let mut rest = Vec::new(); reader.read_to_end(&mut rest)?; - if !rest.is_empty() { + if complete || !rest.is_empty() { bail!( "world journal {} is corrupt at byte {good_len}: {}", path.display(), @@ -879,6 +883,26 @@ mod tests { assert!(err.to_string().contains("corrupt"), "{err:#}"); } + #[test] + fn a_terminated_entry_this_binary_cannot_read_is_refused_not_dropped() { + let dir = TempDir::new().unwrap(); + let path = dir.path().join("journal.jsonl"); + drop(FileJournal::open(&path, "w").unwrap()); + let good = std::fs::read(&path).unwrap(); + + // An entry from a newer binary, fully written, as the final line. + let mut newer = good.clone(); + newer.extend_from_slice(b"{\"entry\":\"future_variant\",\"x\":1}\n"); + std::fs::write(&path, &newer).unwrap(); + let err = FileJournal::open(&path, "w").unwrap_err(); + assert!(err.to_string().contains("corrupt"), "{err:#}"); + assert_eq!( + std::fs::read(&path).unwrap(), + newer, + "the unreadable entry is left in place, not truncated" + ); + } + #[tokio::test] async fn a_journal_for_another_world_is_refused() { let dir = TempDir::new().unwrap();