diff --git a/crates/engine/src/tests/archive_render_revision.rs b/crates/engine/src/tests/archive_render_revision.rs index 5ccac5f546..9eea2b2e1d 100644 --- a/crates/engine/src/tests/archive_render_revision.rs +++ b/crates/engine/src/tests/archive_render_revision.rs @@ -100,7 +100,7 @@ fn paused_map(path: &std::path::Path, kind: TileKind) -> (Map, render::View, mps map.tiles.push(TileLayer::new("Below", kind, path.to_string_lossy())); let view = render::View { width: 128.0, height: 128.0, pixels_per_point: 1.0, camera: map.camera, sky: false }; let old = render::render_placed(&mut map, &view).unwrap(); - assert_eq!(old.credits, ["Old archive credits"]); + assert_eq!(old.credits, ["Old archive credits"], "{:?}", old.gaps); assert!(old.pixmap.pixel(8, 8).unwrap().red() > 200); let mut ds = Dataset::new("Between", GeometryType::Polyline, Vec::new()); ds.push(Some(Geometry::Line(vec![vec![data::Coord::new(0.0, 0.0), data::Coord::new(0.1, 0.1)]])), Vec::new()); @@ -195,7 +195,7 @@ fn archive_reload_refuses_spanning_exports_without_replacing_destination() { resume.wait(); let result = worker.join().unwrap(); let fresh = fresh.unwrap(); - assert_eq!(fresh.credits, ["New archive credits"]); + assert_eq!(fresh.credits, ["New archive credits"], "{:?}", fresh.gaps); assert!(fresh.pixmap.pixel(8, 8).unwrap().green() > 200); let error = result.unwrap_err(); assert!(error.contains("archive changed during rendering") && error.contains("export again"), "{error}"); @@ -244,7 +244,7 @@ fn a_spanning_render_keeps_progressive_arrivals_and_unrelated_reloads() { let result = worker.join().unwrap().unwrap(); fresh.unwrap(); assert_ne!(cache::generation(), arrived_before); - assert_eq!(result.credits, ["Old archive credits"]); + assert_eq!(result.credits, ["Old archive credits"], "{:?}", result.gaps); assert!(result.pixmap.pixel(8, 8).unwrap().red() > 200); data::mbtiles::close(&target); data::mbtiles::close(&unrelated); @@ -359,7 +359,7 @@ fn a_reload_between_series_pages_refuses_the_engine_pdf_without_replacing_destin let wrote = work().unwrap(); drop(installed); let fresh = reloaded.join().unwrap().unwrap(); - assert_eq!(fresh.credits, ["New archive credits"]); + assert_eq!(fresh.credits, ["New archive credits"], "{:?}", fresh.gaps); assert!(fresh.pixmap.pixel(8, 8).unwrap().green() > 200); assert_eq!(probe.pages.load(Ordering::SeqCst), 2, "both individually coherent pages were drawn"); let error = wrote.then.unwrap()(&mut session, &mut serde_json::Value::Null).unwrap_err(); @@ -370,3 +370,29 @@ 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 export waits for an MBTiles file's metadata read as for any other read it needs: one slower +/// than a few drawings (a loaded machine, a file a virus scanner holds open) is no "tiles still on +/// their way" gap. The read is held up here by another connection's lock. +#[test] +fn an_export_waits_for_a_slow_mbtiles_metadata_read() { + let dir = Fixture::new(); + let path = dir.path().join("slow.mbtiles"); + archive(&path, TileKind::MbTiles, false); + let lock = rusqlite::Connection::open(&path).unwrap(); + lock.execute_batch("BEGIN EXCLUSIVE").unwrap(); + let mut map = Map::new("Archive"); + map.graticule = false; + map.camera = Camera::new(data::Coord::new(0.0, 0.0), 100_000.0); + map.tiles.push(TileLayer::new("Archive", TileKind::MbTiles, path.to_string_lossy())); + let view = render::View { width: 32.0, height: 32.0, pixels_per_point: 1.0, camera: map.camera, sky: false }; + let unlocked = std::thread::spawn(move || { + std::thread::sleep(std::time::Duration::from_millis(300)); + lock.execute_batch("ROLLBACK").unwrap(); + }); + let placed = render::render_placed(&mut map, &view).unwrap(); + unlocked.join().unwrap(); + assert_eq!(placed.credits, ["Old archive credits"], "{:?}", placed.gaps); + assert!(placed.gaps.is_empty(), "{:?}", placed.gaps); + assert!(placed.pixmap.pixel(8, 8).unwrap().red() > 200); +} diff --git a/crates/engine/tests/portable_projects/reload_tiles.rs b/crates/engine/tests/portable_projects/reload_tiles.rs index 15c89aebf8..ab611d3cfd 100644 --- a/crates/engine/tests/portable_projects/reload_tiles.rs +++ b/crates/engine/tests/portable_projects/reload_tiles.rs @@ -86,10 +86,6 @@ fn decoded(t: &TileLayer) -> Arc { fn style(t: &TileLayer) -> Arc { for _ in 0..100 { - if t.kind == rustgis_engine::map::TileKind::MbTiles { - // MBTiles metadata workers have their own bounded wait, not cache::in_flight. - let _ = mbtiles::info_ready(&t.url, Duration::from_millis(100)).unwrap(); - } match source::resolve(&render::tile_spec(t)) { source::Resolve::Ready(r) => return r, source::Resolve::Failed(e) => panic!("{e}"), diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 5a2bdbbd6a..4a71522553 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -547,7 +547,9 @@ pub fn backoff(wait: Duration, failures: u32) -> Duration { wait.saturating_mul(1u32 << doublings).min(most) } -/// How many fetches are under way. Once a fetch no longer counts, its [`generation`] shows. +/// How many fetches are under way, with the reads the map waits for outside the fetch workers +/// (an MBTiles file's metadata: [`Counted`]). Once a fetch no longer counts, its [`generation`] +/// shows. pub fn in_flight() -> usize { IN_FLIGHT.load(Ordering::Acquire) } @@ -1756,6 +1758,28 @@ fn finished() { } } +/// A read the map needs before it can ask for tiles that runs on its own thread, not the fetch +/// workers (an MBTiles file's metadata), counted as a fetch under way while it lasts: an export +/// waits for it ([`wait_for_news`]) instead of drawing again without news and giving up on a +/// gap. Dropped, however its thread ends, it is over as a fetch is ([`finished`]). +#[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(); + } +} + /// Stores an explicitly fetched answer; composed answers use their inherited ceiling. #[cfg(test)] fn store(url: &str, entry: Entry) { diff --git a/crates/tiles/src/mbtiles.rs b/crates/tiles/src/mbtiles.rs index d73d43b519..e00194e88b 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(); + // Counted before the answer says pending, so a caller waiting for fetches 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(); + // Over as a fetch is: the map draws again with what was read. + drop(counted); }); if let Err(error) = spawned { all.remove(&key);