History

fix(gix-odb): recheck handle state after index contention

A handle could verify its snapshot before blocking behind another index loader, then let load_next_index establish its change baseline only after that loader published. This made the loader report no change even though the caller's snapshot was stale. Recheck the caller's marker after an exhausted load attempt and collect current state before applying miss-refresh policy. Exercise the lock handoff deterministically with two indices and two handles.

Codex committed 3 months ago

Commit da0d0bc1 parent df91e533 files · +108 −1Browse files

Unified · Split

3 files
  1. gix-odb/src/store_impls/dynamic/init.rs +5 −0
  2. gix-odb/src/store_impls/dynamic/load_index.rs +8 −1
  3. gix-odb/tests/odb/store/dynamic_scenarios.rs +95 −0
gix-odb/src/store_impls/dynamic/init.rs+5−0
@@ -73,6 +73,11 @@ pub mod debug { · 72 unmodified lines
7373 },
7474 /// A loader found all index slots claimed by other loaders. No Store or slot lock is held.
7575 IndexLoadWaiting,
76+ /// A failed-load check is about to acquire an index slot lock.
77+ IndexRetrySlotLocking {
78+ /// The index slot about to be locked.
79+ slot: usize,
80+ },
7681 /// Snapshot collection is waiting for an in-progress index load. No Store or slot lock is held.
7782 SnapshotWaitingForIndexLoad,
7883 /// The claimed index slot was locked, immediately before its file is loaded.
gix-odb/src/store_impls/dynamic/load_index.rs+8−1
@@ -95,9 +95,14 @@ impl super::Store { · 94 unmodified lines
9595 } else {
9696 // always compare to the latest state
9797 // Nothing changed in the meantime, try to load another index…
98− if self.load_next_index(catalog)? {
98+ if self.load_next_index(Arc::clone(&catalog))? {
9999 Ok(Some(self.collect_snapshot()))
100100 } else {
101+ let current_catalog = self.catalog.load();
102+ let current_index = &current_catalog.index;
103+ if marker.generation != current_index.generation || marker.state_id != current_index.state_id() {
104+ return Ok(Some(self.collect_snapshot()));
105+ }
101106 // …and if that didn't yield anything new consider refreshing our disk state.
102107 match refresh_mode {
103108 RefreshMode::Never => Ok(None),
@@ -237,6 +242,8 @@ impl super::Store { · 133 unmodified lines
237242 if !Option::as_ref(&slot.files.load()).is_some_and(IndexAndPacks::index_is_failed) {
238243 continue;
239244 }
245+ #[cfg(feature = "test-support")]
246+ self.debug(crate::store::init::debug::Point::IndexRetrySlotLocking { slot: slot_id });
240247 let _lock = slot.write.lock();
241248 if slot.generation.load(Ordering::SeqCst) > index.generation {
242249 continue;
gix-odb/tests/odb/store/dynamic_scenarios.rs+95−0
@@ -298,6 +298,101 @@ fn an_index_load_is_registered_before_its_slot_is_reserved() -> Result { · 297 unmodified lines
298298 Ok(())
299299 }
300300
301+#[cfg(feature = "parallel")]
302+#[test]
303+fn a_handle_rechecks_its_snapshot_after_waiting_for_an_index_loader() -> Result {
304+ use std::{
305+ sync::{
306+ Arc,
307+ atomic::{AtomicBool, Ordering},
308+ },
309+ time::Duration,
310+ };
311+
312+ use gix_odb::store::init::debug::Point;
313+
314+ let mut fixture = OdbFixture::from_script()?;
315+ fixture.install_pack(Database::Primary, Pack::A)?;
316+ fixture.install_pack(Database::Alternate, Pack::B)?;
317+ fixture.corrupt_index(Database::Alternate, Pack::B)?;
318+ fixture.set_alternate(true)?;
319+
320+ let (point_tx, point_rx) = crossbeam_channel::unbounded();
321+ let (resume_waiter_tx, resume_waiter_rx) = crossbeam_channel::bounded(0);
322+ let pause_waiter = Arc::new(AtomicBool::new(false));
323+ let debug = gix_odb::store::init::debug::Options::new({
324+ let pause_waiter = Arc::clone(&pause_waiter);
325+ move |point| {
326+ point_tx
327+ .send(point)
328+ .expect("the test receives every synchronization point");
329+ if matches!(point, Point::IndexRetrySlotLocking { .. }) && pause_waiter.swap(false, Ordering::SeqCst) {
330+ resume_waiter_rx
331+ .recv()
332+ .expect("the test releases the stale handle's retry");
333+ }
334+ }
335+ });
336+ let mut handle = gix_odb::at_opts(
337+ fixture.objects_dir(Database::Primary),
338+ fixture.manifest.object_hash,
339+ Vec::new(),
340+ gix_odb::store::init::Options {
341+ slots: gix_odb::store::init::Slots::Limit(2),
342+ debug: Some(debug),
343+ ..Default::default()
344+ },
345+ )?
346+ .into_arc()?;
347+ assert_object(&handle, &fixture.manifest.pack(Pack::A).object_ids[0])?;
348+ let second_object_id = fixture.manifest.pack(Pack::B).object_ids[0];
349+ handle
350+ .try_header(&second_object_id)
351+ .expect_err("the second index records a failed load before it can be retried");
352+ handle.refresh_never();
353+ let stale = handle.clone();
354+ let loader = handle.clone();
355+ fixture.publish(Database::Alternate, Pack::B, Component::Index)?;
356+ point_rx.try_iter().for_each(drop);
357+
358+ let lookup = move |handle: gix_odb::HandleArc| {
359+ std::thread::spawn(move || {
360+ handle
361+ .try_header(&second_object_id)
362+ .map(|header| header.is_some())
363+ .map_err(|err| err.to_string())
364+ })
365+ };
366+ pause_waiter.store(true, Ordering::SeqCst);
367+ let stale_thread = lookup(stale);
368+ loop {
369+ let point = point_rx
370+ .recv_timeout(Duration::from_secs(10))
371+ .expect("the stale handle is about to lock the failed index for a retry");
372+ if matches!(point, Point::IndexRetrySlotLocking { .. }) {
373+ break;
374+ }
375+ }
376+ // Another loader publishes the repaired index while the stale handle is paused before acquiring its retry lock.
377+ let loaded = lookup(loader).join().expect("the loader does not panic");
378+ resume_waiter_tx
379+ .send(())
380+ .expect("the stale handle is waiting for its release");
381+
382+ assert!(
383+ loaded.expect("the loader lookup succeeds"),
384+ "the loading handle finds the object"
385+ );
386+ assert!(
387+ stale_thread
388+ .join()
389+ .expect("the stale lookup does not panic")
390+ .expect("the stale lookup succeeds"),
391+ "the stale handle observes the index published while it waited"
392+ );
393+ Ok(())
394+}
395+
301396 #[cfg(feature = "parallel")]
302397 #[test]
303398 fn debug_hooks_coordinate_contending_failed_index_loaders() -> Result {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t