Skip to content

Commit 384c923

Browse files
committed
Fix blocking lock on evicting map
1 parent fbf8069 commit 384c923

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

nativelink-util/src/evicting_map.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -438,36 +438,35 @@ where
438438
let (result, items_to_unref, removal_futures) = {
439439
let mut state = self.state.lock();
440440
// Check if the requested item is expired before promoting it.
441-
let result = if let Some(entry) = state.lru.peek(key.borrow()) {
441+
if let Some(entry) = state.lru.peek(key.borrow()) {
442442
if self.should_evict(state.lru.len(), entry, 0, u64::MAX) {
443443
// Item is expired, remove it.
444444
if let Some((k, eviction_item)) = state.lru.pop_entry(key.borrow()) {
445445
let (data, futures) = state.remove(k.borrow(), &eviction_item, false);
446446
let (mut items, mut removals) = self.evict_items(&mut *state);
447447
items.push(data);
448448
removals.extend(futures);
449-
return {
450-
Self::unref_items(items, removals).await;
451-
None
452-
};
449+
(None, items, removals)
450+
} else {
451+
let (items, removals) = self.evict_items(&mut *state);
452+
(None, items, removals)
453453
}
454-
None
455454
} else {
456455
// Item is valid. Promote it in LRU so it's safe from eviction.
457-
state.lru.get_mut(key.borrow()).map(|entry| {
456+
let data = state.lru.get_mut(key.borrow()).map(|entry| {
458457
entry.seconds_since_anchor =
459458
i32::try_from(self.anchor_time.elapsed().as_secs()).unwrap_or(i32::MAX);
460459
entry.data.clone()
461-
})
460+
});
461+
let (items, removals) = self.evict_items(&mut *state);
462+
(data, items, removals)
462463
}
463464
} else {
464-
None
465-
};
466-
// Evict other items if needed
467-
let (items_to_unref, removal_futures) = self.evict_items(&mut *state);
468-
(result, items_to_unref, removal_futures)
465+
let (items, removals) = self.evict_items(&mut *state);
466+
(None, items, removals)
467+
}
469468
};
470-
// Unref items outside of lock
469+
// Unref items outside of lock — lock is guaranteed dropped here.
471470
Self::unref_items(items_to_unref, removal_futures).await;
472471
result
473472
}

nativelink-util/src/instant_wrapper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl InstantWrapper for MockInstantWrapped {
8888
let baseline = self.0.elapsed();
8989
loop {
9090
tokio::task::yield_now().await;
91-
if self.0.elapsed() - baseline >= duration {
91+
if self.0.elapsed().saturating_sub(baseline) >= duration {
9292
break;
9393
}
9494
}

0 commit comments

Comments
 (0)