message: Introduce `wait_for_arc_drop` utility function
diff: @@ -11,6 +11,7 @@ pub(crate) mod rlimit;
  mod round_robin;
  use std::{
  + any,
  cell::RefCell,
  fmt::{self, Debug, Display, Formatter},
  fs,
  @@ -19,6 +20,8 @@ use std::{
  ops::{Add, BitXorAssign, Div},
  os::unix::fs::OpenOptionsExt,
  path::{Path, PathBuf},
  + sync::Arc,
  + time::Duration,
  };
  #[cfg(test)]
  use std::{env, str::FromStr};
  @@ -29,7 +32,7 @@ use libc::{c_long, sysconf, _SC_PAGESIZE};
  use once_cell::sync::Lazy;
  use serde::Serialize;
  use thiserror::Error;
  -use tracing::warn;
  +use tracing::{error, warn};
  pub(crate) use counting_channel::{counting_unbounded_channel, CountingReceiver, CountingSender};
  #[cfg(test)]
  @@ -388,6 +391,41 @@ pub fn xor(lhs: &mut [u8], rhs: &[u8]) {
  .for_each(|(sb, &cb)| sb.bitxor_assign(cb));
  }
  +/// Wait until all strong references for a particular arc have been dropped.
  +///
  +/// Downgrades and immediately drops the `Arc`, keeping only a weak reference. The reference will
  +/// then be polled `attempts` times, unless it has a strong reference count of 0.
  +///
  +/// Returns whether or not `arc` has zero strong references left.
  +///
  +/// # Note
  +///
  +/// Using this function is usually a potential architectural issue and it should be used very
  +/// sparingly. Consider introducing a different access pattern for the value under `Arc`.
  +pub async fn wait_for_arc_drop<T>(arc: Arc<T>, attempts: usize, retry_delay: Duration) -> bool {
  + // Ensure that if we do hold the last reference, we are now going to 0.
  + let weak = Arc::downgrade(&arc);
  + drop(arc);
  +
  + for _ in 0..attempts {
  + let strong_count = weak.strong_count();
  +
  + if strong_count == 0 {
  + // Everything has been dropped, we are done.
  + return true;
  + }
  +
  + tokio::time::sleep(retry_delay).await;
  + }
  +
  + error!(
  + attempts, ?retry_delay, ty=%any::type_name::<T>(),
  + "failed to clean up shared reference"
  + );
  +
  + false
  +}
  +
  #[cfg(test)]
  mod tests {
  use super::xor;
  