public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH datacenter-manager 08/15] task cache: archive iterator: don't yield more items if reading from file failed
Date: Thu,  2 Jul 2026 11:22:51 +0200	[thread overview]
Message-ID: <20260702092258.174740-9-l.wagner@proxmox.com> (raw)
In-Reply-To: <20260702092258.174740-1-l.wagner@proxmox.com>

Otherwise `get_tasks` runs into an endless loop if any of the archive
files is corrupted.

Also add a test case that verifies the absence of the issue.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 server/src/remote_tasks/task_cache.rs | 48 +++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 6 deletions(-)

diff --git a/server/src/remote_tasks/task_cache.rs b/server/src/remote_tasks/task_cache.rs
index f0e3533a..02be9ca1 100644
--- a/server/src/remote_tasks/task_cache.rs
+++ b/server/src/remote_tasks/task_cache.rs
@@ -1244,6 +1244,7 @@ where
 /// tasks are read line by line, without leading the entire archive file into memory.
 struct ArchiveIterator {
     iter: Lines<Box<dyn BufRead>>,
+    failed: bool,
 }
 
 impl ArchiveIterator {
@@ -1251,7 +1252,10 @@ impl ArchiveIterator {
     pub fn new(reader: Box<dyn BufRead>) -> Self {
         let lines = reader.lines();
 
-        Self { iter: lines }
+        Self {
+            iter: lines,
+            failed: false,
+        }
     }
 }
 
@@ -1259,11 +1263,18 @@ impl Iterator for ArchiveIterator {
     type Item = Result<TaskCacheItem, Error>;
 
     fn next(&mut self) -> Option<Self::Item> {
-        self.iter.next().map(|result| {
-            result
-                .and_then(|line| Ok(serde_json::from_str(&line)?))
-                .map_err(Into::into)
-        })
+        if self.failed {
+            // Don't return any more items if we have failed reading a line once
+            return None;
+        }
+
+        self.iter.next().map(|line| match line {
+            Ok(line) => Some(serde_json::from_str(&line).context("failed to decode JSON")),
+            Err(err) => {
+                self.failed = true;
+                Some(Err(err).context("failed to read line"))
+            }
+        })?
     }
 }
 
@@ -1661,4 +1672,29 @@ mod tests {
         assert_eq!(first.iter().unwrap().unwrap().count(), 0);
         assert_eq!(second.iter().unwrap().unwrap().count(), 1);
     }
+
+    #[test]
+    fn corrupted_archive_file_does_not_lead_to_endless_loop() {
+        let (_tmp_dir, cache) = make_cache().unwrap();
+        let cache = cache.write().unwrap();
+
+        // Create compressed file
+        cache.new_file(1000, true).unwrap();
+        add_tasks(&cache, vec![task(1100, Some(1110))]).unwrap();
+        cache.apply_journal().unwrap();
+
+        assert_eq!(cache.get_tasks(GetTasks::Archived).unwrap().count(), 1);
+
+        let files = cache.cache.archive_files(&cache.lock).unwrap();
+        let file = files.get(0).expect("there is one archive file");
+
+        // truncate existing compressed file, corrupting the zstd file header
+        let _file = OpenOptions::new()
+            .write(true)
+            .truncate(true)
+            .open(&file.path)
+            .expect("file truncated");
+
+        assert_eq!(cache.get_tasks(GetTasks::Archived).unwrap().count(), 0);
+    }
 }
-- 
2.47.3





  parent reply	other threads:[~2026-07-02  9:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02  9:22 [PATCH datacenter-manager/proxmox 00/15] task cache improvements (archive corruption handling, error handling) Lukas Wagner
2026-07-02  9:22 ` [PATCH proxmox 01/15] sys: fs: don't replace file extension make_tmp_file Lukas Wagner
2026-07-02  9:29   ` Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 02/15] task cache: fix missing cutoff state for PBS remotes Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 03/15] task cache: refresh task: don't apply journal if the archive was rotated Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 04/15] task cache: rotate: align timestamp for new files to UTC midnight Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 05/15] task cache: add test case for task cache rotation Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 06/15] task cache: pre-compute static paths during initialization Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 07/15] task cache: only initialize `TaskCache` struct once Lukas Wagner
2026-07-02  9:22 ` Lukas Wagner [this message]
2026-07-02  9:22 ` [PATCH datacenter-manager 09/15] task cache: include archive file path in error log messages Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 10/15] task cache: introduce ArchiveFileWriter Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 11/15] task cache: use ArchiveFileWriter when creating new archive files Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 12/15] task cache: trigger repair of corruption when applying journal Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 13/15] task cache: trigger repair of corruption when compressing archive files Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 14/15] task cache: trigger repair of corruption after read-accesses Lukas Wagner
2026-07-02  9:22 ` [PATCH datacenter-manager 15/15] task cache: handle potentially duplicated archive files after 'compress_archive_file' Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260702092258.174740-9-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal