Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions crates/engine/src/tests/archive_render_revision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
// 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);
}
}
22 changes: 22 additions & 0 deletions crates/tiles/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
11 changes: 10 additions & 1 deletion crates/tiles/src/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,26 @@ pub fn info_ready(archive: &str, timeout: Duration) -> Result<Option<Arc<Info>>,
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();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// 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));
remember_info(&address, &result, epoch);
});
pending.lock().unwrap_or_else(|e| e.into_inner()).remove(&worker_key);
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
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 {
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
all.remove(&key);
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
// A caller that began waiting for it meanwhile stops waiting.
ready.notify_all();
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
return Err(format!("could not start an MBTiles metadata read: {error}"));
}
}
Expand Down
Loading