From b771a40a3da69dab54771bf336b2b0907133aed0 Mon Sep 17 00:00:00 2001 From: onyinyang Date: Mon, 5 Feb 2024 15:59:16 -0500 Subject: [PATCH] Fix deadlock error --- crates/lox-distributor/src/lox_context.rs | 34 ++++++++++------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/crates/lox-distributor/src/lox_context.rs b/crates/lox-distributor/src/lox_context.rs index 67d83ad..a9f8dc1 100644 --- a/crates/lox-distributor/src/lox_context.rs +++ b/crates/lox-distributor/src/lox_context.rs @@ -194,16 +194,12 @@ impl LoxServerContext { accounted_for_bridges, ); } - let mut ba_clone = self.ba.lock().unwrap(); - let total_reachable = ba_clone.bridge_table.reachable.len(); - match total_reachable.cmp(&accounted_for_bridges.len()) { - Ordering::Greater => { - let unaccounted_for = ba_clone.find_and_remove_unaccounted_for_bridges(accounted_for_bridges); - for bridgeline in unaccounted_for { - match self.replace_with_new(bridgeline) { - lox_library::ReplaceSuccess::Replaced => { - println!("BridgeLine {:?} not found in rdsys update was successfully replaced.", bridgeline.uid_fingerprint); - self.metrics.removed_bridges.inc(); + let unaccounted_for = self.ba.lock().unwrap().find_and_remove_unaccounted_for_bridges(accounted_for_bridges); + for bridgeline in unaccounted_for { + match self.replace_with_new(bridgeline) { + lox_library::ReplaceSuccess::Replaced => { + println!("BridgeLine {:?} not found in rdsys update was successfully replaced.", bridgeline.uid_fingerprint); + self.metrics.removed_bridges.inc(); } lox_library::ReplaceSuccess::NotReplaced => { // Try again to replace at the next update (nothing changes in the Lox Authority) @@ -217,18 +213,13 @@ impl LoxServerContext { ), } } - } - Ordering::Less => println!("Something unexpected occurred: The number of reachable bridges should not be less than those updated from rdsys"), - _ => (), - - } // Finally, assign any extra_bridges to new buckets if there are enough while self.extra_bridges.lock().unwrap().len() >= MAX_BRIDGES_PER_BUCKET { let bucket = self.remove_extra_bridges(); // TODO: Decide the circumstances under which a bridge is allocated to an open_inv or spare bucket, // eventually also do some more fancy grouping of new resources, i.e., by type or region let mut db_obj = self.db.lock().unwrap(); - match ba_clone.add_spare_bucket(bucket, &mut db_obj) { + match self.ba.lock().unwrap().add_spare_bucket(bucket, &mut db_obj) { Ok(_) => (), Err(e) => { println!("Error: {:?}", e); @@ -238,6 +229,9 @@ impl LoxServerContext { } } } + // Any remaining extra bridges should be cleared from the Lox Context after each sync + // Currently bridgetable updating behaviour does not occur without receiving a resource list + // from rdsys so if the extra bridge is still working, it can be added to the table later self.extra_bridges.lock().unwrap().clear(); } @@ -803,10 +797,10 @@ mod tests { reachable_expected_length, "Unexpected number of reachable bridges" ); - assert_eq!( - th.context.extra_bridges.lock().unwrap().len(), - 0, - "Unexpected number of extra bridges" + // Extra bridges should be cleared from the Lox Context after each sync + assert!( + th.context.extra_bridges.lock().unwrap().is_empty(), + "Extra bridges should be empty after syncUnexpected number of extra bridges" ); }