From 2f48005f5999b054eb3475f428c87521e9a80bd3 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 18:51:56 -0500 Subject: [PATCH 1/2] fix(id): drop a parseable torn tail in Outbox::open (#1620) --- pkgs/id/src/envelope_outbox.rs | 48 ++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/envelope_outbox.rs b/pkgs/id/src/envelope_outbox.rs index 56ea6584ed..2e1b909090 100644 --- a/pkgs/id/src/envelope_outbox.rs +++ b/pkgs/id/src/envelope_outbox.rs @@ -139,13 +139,22 @@ impl Outbox { }; let mut kept = 0; for (number, line) in text.split_inclusive('\n').enumerate() { - let complete = line.ends_with('\n'); + if !line.ends_with('\n') { + // The final line has no newline: a crash cut the append + // short. Parseable or not, it was never completely + // written, so it is torn and dropped — the same rule + // ArtifactLog::open and Directory::replay apply. Keeping + // a parseable torn line would let the next append merge + // two records onto one line and brick the queue (#1620). + // `kept` stays at the last complete line, so the file is + // truncated to it below. + break; + } match serde_json::from_str::(line.trim_end()) { Ok(record) => { outbox.apply(record)?; kept += line.len(); } - Err(_) if !complete => break, Err(error) => { return Err(error).with_context(|| { format!("envelope outbox line {} is corrupt", number + 1) @@ -538,6 +547,41 @@ mod tests { assert!(reopened.entry("e1").is_some() && reopened.entry("e2").is_some()); } + #[test] + fn a_parseable_torn_last_line_is_also_dropped() { + // #1620: the torn tail here is a complete JSON record missing + // only its trailing newline. Before the fix, open accepted it, + // and the next append merged two records onto one line. + let dir = tempfile::tempdir().unwrap(); + let mut outbox = Outbox::open(path(&dir)).unwrap(); + outbox.enqueue(&target(), envelope("e1")).unwrap(); + drop(outbox); + // A whole record line for e2, written by a healthy outbox, with + // its newline stripped to simulate the crash cut. + let scratch = tempfile::tempdir().unwrap(); + let mut other = Outbox::open(path(&scratch)).unwrap(); + other.enqueue(&target(), envelope("e2")).unwrap(); + drop(other); + let line = fs::read_to_string(path(&scratch)).unwrap(); + let mut file = OpenOptions::new().append(true).open(path(&dir)).unwrap(); + file.write_all(line.trim_end_matches('\n').as_bytes()).unwrap(); + drop(file); + + let mut outbox = Outbox::open(path(&dir)).unwrap(); + assert!(outbox.entry("e1").is_some()); + assert!( + outbox.entry("e2").is_none(), + "the parseable torn record must be dropped, not accepted" + ); + // The file was truncated to the last complete line, so the next + // append starts on its own line and round-trips. + outbox.enqueue(&target(), envelope("e2")).unwrap(); + drop(outbox); + + let reopened = Outbox::open(path(&dir)).unwrap(); + assert!(reopened.entry("e1").is_some() && reopened.entry("e2").is_some()); + } + #[test] fn a_corrupt_complete_line_stops_the_load() { let dir = tempfile::tempdir().unwrap(); From 7fe044ac0954802ba266c8a42196852ea1296851 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 21:16:42 -0500 Subject: [PATCH 2/2] style(id): rustfmt wrap in outbox torn-tail regression test (#1620) --- pkgs/id/src/envelope_outbox.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/envelope_outbox.rs b/pkgs/id/src/envelope_outbox.rs index 2e1b909090..99eb366bd3 100644 --- a/pkgs/id/src/envelope_outbox.rs +++ b/pkgs/id/src/envelope_outbox.rs @@ -564,7 +564,8 @@ mod tests { drop(other); let line = fs::read_to_string(path(&scratch)).unwrap(); let mut file = OpenOptions::new().append(true).open(path(&dir)).unwrap(); - file.write_all(line.trim_end_matches('\n').as_bytes()).unwrap(); + file.write_all(line.trim_end_matches('\n').as_bytes()) + .unwrap(); drop(file); let mut outbox = Outbox::open(path(&dir)).unwrap();