From ca449ece89866edfb9f49012d6520c2bff8c5ca8 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Wed, 11 Nov 2026 00:49:26 -0300 Subject: [PATCH 1/3] fix(tiles): count an MBTiles file's metadata read in in_flight() (#1761) mbtiles::info_ready read a local file's metadata on a thread of its own, outside IN_FLIGHT. While it ran, a complete render saw its tile layer pending with nothing under way, spent its six idle passes in milliseconds and gave up with the "still on their way" note; under CPU load this made archive_render_revision.rs fail. A cache::Counted guard now counts the read from before the thread is spawned until it ends (answered, failed, cancelled, stale after a reload or clear, panicked, or never spawned), drawing again as a fetch's end does. A new test holds the real archive read through the request's still check, for PMTiles and MBTiles, and checks the render waits for it. Co-Authored-By: Claude Opus 5.5 --- .../src/tests/archive_render_revision.rs | 45 +++++++++++++++++++ crates/tiles/src/cache.rs | 22 +++++++++ crates/tiles/src/mbtiles.rs | 5 ++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/tests/archive_render_revision.rs b/crates/engine/src/tests/archive_render_revision.rs index 5ccac5f546..5135536824 100644 --- a/crates/engine/src/tests/archive_render_revision.rs +++ b/crates/engine/src/tests/archive_render_revision.rs @@ -370,3 +370,48 @@ fn a_reload_between_series_pages_refuses_the_engine_pdf_without_replacing_destin assert!(pdf.starts_with(b"%PDF") && pdf.windows(b"/Count 2".len()).any(|part| part == b"/Count 2")); } } + +/// An archive's first read held on the thread that reads it (by the request's still check, asked +/// there before the read): a complete render waits for that read, however long it is held, rather +/// than running out of idle passes and drawing the archive as tiles still on their way. +#[test] +fn a_complete_render_waits_for_a_held_archive_read() { + for kind in [TileKind::PmTiles, TileKind::MbTiles] { + let dir = Fixture::new(); + let path = dir.path().join(format!("held.{}", kind.code())); + archive(&path, kind, false); + let mut map = Map::new("Held"); + map.graticule = false; + map.camera = Camera::new(data::Coord::new(0.0, 0.0), 100_000.0); + map.tiles.push(TileLayer::new("Held", kind, path.to_string_lossy())); + let view = render::View { width: 64.0, height: 64.0, pixels_per_point: 1.0, camera: map.camera, sky: false }; + // A PMTiles archive is read by range on the tile workers, an MBTiles file's metadata on + // a thread of its own. + let reader = if kind == TileKind::PmTiles { "rustgis-tiles-" } else { "rustgis-mbtiles-info" }; + let (paused, receive) = mpsc::sync_channel(1); + let resume = Arc::new(Barrier::new(2)); + let held = Arc::new(AtomicBool::new(false)); + let still: cache::Still = { + let resume = resume.clone(); + Arc::new(move || { + if std::thread::current().name().is_some_and(|n| n.starts_with(reader)) && !held.swap(true, Ordering::SeqCst) { + paused.send(()).unwrap(); + resume.wait(); + } + true + }) + }; + let (placed, answer) = mpsc::channel(); + let worker = std::thread::spawn(move || placed.send(cache::with_still(still, || render::render_placed(&mut map, &view))).unwrap()); + receive.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); + assert!(cache::in_flight() > 0, "a held {kind:?} read is under way"); + // Held far longer than six idle passes take: the render is still waiting for it. + assert!(answer.recv_timeout(std::time::Duration::from_millis(500)).is_err(), "the render gave up on a held {kind:?} read"); + resume.wait(); + let placed = answer.recv_timeout(std::time::Duration::from_secs(60)).unwrap().unwrap(); + worker.join().unwrap(); + assert_eq!(placed.credits, ["Old archive credits"], "{kind:?}: {:?}", placed.gaps); + assert!(placed.pixmap.pixel(8, 8).unwrap().red() > 200, "{kind:?}: {:?}", placed.gaps); + assert!(placed.gaps.iter().all(|g| !g.contains("still on their way")), "{kind:?}: {:?}", placed.gaps); + } +} diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index e931e59e03..9829cc8d16 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -1735,6 +1735,28 @@ pub(crate) fn fetch_local(url: String, done: Done) { ); } +/// Work the map waits for that runs off the fetch workers (an MBTiles file's metadata, read on a +/// thread of its own), counted in [`in_flight`] from [`Counted::start`] until dropped, as a fetch +/// is: an export waiting for the map's tiles waits for it too. However it ends (answered, unwound +/// by a panic, or its thread never started), the drop draws again and the count goes down once. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) struct Counted(()); + +#[cfg(not(target_arch = "wasm32"))] +impl Counted { + pub(crate) fn start() -> Self { + IN_FLIGHT.fetch_add(1, Ordering::Relaxed); + Self(()) + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl Drop for Counted { + fn drop(&mut self) { + finished(); + } +} + /// Something the map draws from arrived outside the cache (a PMTiles archive's header): draw /// again. pub(crate) fn notify() { diff --git a/crates/tiles/src/mbtiles.rs b/crates/tiles/src/mbtiles.rs index d73d43b519..eb8749dc5e 100644 --- a/crates/tiles/src/mbtiles.rs +++ b/crates/tiles/src/mbtiles.rs @@ -322,6 +322,8 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result>, let address = archive.to_string(); let worker_key = key.clone(); let still = crate::cache::current_still(); + // Under way until the read is over, as a tile is: an export waits for it. + let counted = crate::cache::Counted::start(); let spawned = std::thread::Builder::new().name("rustgis-mbtiles-info".into()).spawn(move || { crate::cache::with_optional_still(still, || { let result = crate::cache::check_still(&crate::cache::current_still()).and_then(|_| read_info(&address)); @@ -329,7 +331,8 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result>, }); pending.lock().unwrap_or_else(|e| e.into_inner()).remove(&worker_key); ready.notify_all(); - crate::cache::notify(); + // The map draws again with what it read, then the read no longer counts. + drop(counted); }); if let Err(error) = spawned { all.remove(&key); From 7b7213a97a08a1619a3b6a95d792d5c7c14e3e13 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Wed, 11 Nov 2026 01:17:15 -0300 Subject: [PATCH 2/3] fix(tiles): #1761 review round 1: say the waker mustn't take the metadata lock; release the held read before asserting Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/tests/archive_render_revision.rs | 7 +++++-- crates/tiles/src/mbtiles.rs | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/tests/archive_render_revision.rs b/crates/engine/src/tests/archive_render_revision.rs index 5135536824..6c4b857101 100644 --- a/crates/engine/src/tests/archive_render_revision.rs +++ b/crates/engine/src/tests/archive_render_revision.rs @@ -404,10 +404,13 @@ fn a_complete_render_waits_for_a_held_archive_read() { let (placed, answer) = mpsc::channel(); let worker = std::thread::spawn(move || placed.send(cache::with_still(still, || render::render_placed(&mut map, &view))).unwrap()); receive.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); - assert!(cache::in_flight() > 0, "a held {kind:?} read is under way"); + let counted = cache::in_flight() > 0; // Held far longer than six idle passes take: the render is still waiting for it. - assert!(answer.recv_timeout(std::time::Duration::from_millis(500)).is_err(), "the render gave up on a held {kind:?} read"); + let waited = answer.recv_timeout(std::time::Duration::from_millis(500)).is_err(); + // Released before asserting, so a failure doesn't leave the reader on the barrier. resume.wait(); + assert!(counted, "a held {kind:?} read is under way"); + assert!(waited, "the render gave up on a held {kind:?} read"); let placed = answer.recv_timeout(std::time::Duration::from_secs(60)).unwrap().unwrap(); worker.join().unwrap(); assert_eq!(placed.credits, ["Old archive credits"], "{kind:?}: {:?}", placed.gaps); diff --git a/crates/tiles/src/mbtiles.rs b/crates/tiles/src/mbtiles.rs index eb8749dc5e..2abcd78438 100644 --- a/crates/tiles/src/mbtiles.rs +++ b/crates/tiles/src/mbtiles.rs @@ -322,7 +322,8 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result>, let address = archive.to_string(); let worker_key = key.clone(); let still = crate::cache::current_still(); - // Under way until the read is over, as a tile is: an export waits for it. + // Under way until the read is over, as a tile is: an export waits for it. A thread + // that can't start drops it here, under `all`: the waker mustn't take this lock. let counted = crate::cache::Counted::start(); let spawned = std::thread::Builder::new().name("rustgis-mbtiles-info".into()).spawn(move || { crate::cache::with_optional_still(still, || { From c328331f9d52aed59d2cb636cc513a7b2f495026 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Wed, 11 Nov 2026 06:26:53 -0300 Subject: [PATCH 3/3] fix(tiles): #1761 CodeRabbit round 1: spawn the MBTiles metadata thread unlocked A thread that can't start drops its Counted guard inside spawn, and the guard's drop calls the waker: it now does so without the pending-reads lock held, so a waker asking for the metadata again can't deadlock. The failed read's key is removed under the lock again and its waiters woken. Co-Authored-By: Claude Opus 5.5 --- crates/tiles/src/mbtiles.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/tiles/src/mbtiles.rs b/crates/tiles/src/mbtiles.rs index 2abcd78438..2488427cbc 100644 --- a/crates/tiles/src/mbtiles.rs +++ b/crates/tiles/src/mbtiles.rs @@ -322,9 +322,11 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result>, let address = archive.to_string(); let worker_key = key.clone(); let still = crate::cache::current_still(); - // Under way until the read is over, as a tile is: an export waits for it. A thread - // that can't start drops it here, under `all`: the waker mustn't take this lock. + // Under way until the read is over, as a tile is: an export waits for it. let counted = crate::cache::Counted::start(); + // Spawned unlocked: a thread that can't start drops `counted` inside `spawn`, and that + // drop wakes the map, whose waker may ask for this file's metadata again. + drop(all); let spawned = std::thread::Builder::new().name("rustgis-mbtiles-info".into()).spawn(move || { crate::cache::with_optional_still(still, || { let result = crate::cache::check_still(&crate::cache::current_still()).and_then(|_| read_info(&address)); @@ -335,8 +337,11 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result>, // The map draws again with what it read, then the read no longer counts. drop(counted); }); + all = pending.lock().unwrap_or_else(|e| e.into_inner()); if let Err(error) = spawned { all.remove(&key); + // A caller that began waiting for it meanwhile stops waiting. + ready.notify_all(); return Err(format!("could not start an MBTiles metadata read: {error}")); } }