From d8c95f203db7f8c00a7a5feda2ba018c5a2a431e Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Fri, 27 Nov 2026 09:55:38 +0100 Subject: [PATCH] fix(engine): a batch taken just before forget_pictures is always thrown away (M8) forget_pictures cleared the queue and the cache under two separate locks, and the worker read the cache's clear count only after it had taken a batch from the queue. A batch taken before the queue was cleared could therefore read the count after the cache was cleared, and its pictures of the forgotten document were kept. The cache is now cleared while the queue is held, and the worker reads the count while it still holds the queue, so a batch taken before a clear always sees the older count. --- crates/engine/src/render_service.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/render_service.rs b/crates/engine/src/render_service.rs index 9e7a159d..1db5d397 100644 --- a/crates/engine/src/render_service.rs +++ b/crates/engine/src/render_service.rs @@ -777,11 +777,14 @@ impl RenderService { /// back into the cache afterwards. pub fn forget_pictures(&self) { { + // The cache is cleared while the queue is held, so the worker takes a batch either + // before the clear (and sees the older count, throwing the batch away) or after it + // (and finds the queue empty). Lock order: queue, then cache. let mut q = self.queue(); q.urgent.clear(); q.prefetch.clear(); + self.cache().clear(); } - self.cache().clear(); *self.shared.scene.lock().unwrap_or_else(PoisonError::into_inner) = None; } @@ -824,7 +827,7 @@ fn worker_loop(s: &Shared) { let mut scenes = Scenes::new(); loop { // Take a whole batch: urgent first, topped up with prefetch work. - let jobs: Vec = { + let (jobs, cleared): (Vec, u64) = { let mut q = s.queue.lock().unwrap_or_else(PoisonError::into_inner); loop { if q.shutdown { @@ -844,15 +847,18 @@ fn worker_loop(s: &Shared) { for j in &batch { q.busy.insert(j.key); } - break batch; + // Read while the queue is held, as `forget_pictures` clears the cache: a + // batch taken before a clear is always seen as older than it. + let cleared = s.cache.lock().unwrap_or_else(PoisonError::into_inner).cleared; + break (batch, cleared); } q = s.wake.wait(q).unwrap_or_else(PoisonError::into_inner); } }; - let (todo, cleared): (Vec, u64) = { + let todo: Vec = { let cache = s.cache.lock().unwrap_or_else(PoisonError::into_inner); // Not what is cached already, nor what the cache would not keep (its set went). - (jobs.iter().filter(|j| !cache.contains(j.key) && cache.keeps(j.set)).cloned().collect(), cache.cleared) + jobs.iter().filter(|j| !cache.contains(j.key) && cache.keeps(j.set)).cloned().collect() }; let slot = s.raster.lock().unwrap_or_else(PoisonError::into_inner).clone(); let (imgs, evaluated) = render_batch_from(&todo, &slot, &scenes);