diff --git a/crates/engine/src/tests/archive_render_revision.rs b/crates/engine/src/tests/archive_render_revision.rs index 5ccac5f546..6c4b857101 100644 --- a/crates/engine/src/tests/archive_render_revision.rs +++ b/crates/engine/src/tests/archive_render_revision.rs @@ -370,3 +370,51 @@ 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(); + let counted = cache::in_flight() > 0; + // Held far longer than six idle passes take: the render is still waiting for it. + 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); + 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..2488427cbc 100644 --- a/crates/tiles/src/mbtiles.rs +++ b/crates/tiles/src/mbtiles.rs @@ -322,6 +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. + 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)); @@ -329,10 +334,14 @@ 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); }); + 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}")); } }