Skip to content

Commit d9048c6

Browse files
authored
fix(sidecar): Cleanup limiters on sidecar shutdown (#1659)
Not really a big problem, just not nice. Co-authored-by: bob.weinand <bob.weinand@datadoghq.com>
1 parent b13e787 commit d9048c6

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

datadog-sidecar/src/service/exception_hash_rate_limiter.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@ use datadog_ipc::rate_limiter::{ShmLimiter, ShmLimiterMemory};
66
use libdd_common::{rate_limiter::Limiter, MutexExt};
77
use std::ffi::CString;
88
use std::io;
9+
use std::mem::ManuallyDrop;
910
use std::sync::atomic::{AtomicU64, Ordering};
1011
use std::sync::{LazyLock, Mutex};
1112
use std::time::Duration;
1213

13-
pub(crate) static EXCEPTION_HASH_LIMITER: LazyLock<Mutex<ManagedExceptionHashRateLimiter>> =
14-
LazyLock::new(|| {
15-
#[allow(clippy::unwrap_used)]
16-
Mutex::new(ManagedExceptionHashRateLimiter::create().unwrap())
17-
});
14+
pub(crate) static EXCEPTION_HASH_LIMITER: LazyLock<
15+
Mutex<ManuallyDrop<ManagedExceptionHashRateLimiter>>,
16+
> = LazyLock::new(|| {
17+
unsafe { libc::atexit(drop_exception_hash_limiter) };
18+
#[allow(clippy::unwrap_used)]
19+
Mutex::new(ManuallyDrop::new(
20+
ManagedExceptionHashRateLimiter::create().unwrap(),
21+
))
22+
});
23+
24+
extern "C" fn drop_exception_hash_limiter() {
25+
let mut guard = EXCEPTION_HASH_LIMITER
26+
.lock()
27+
.unwrap_or_else(|e| e.into_inner());
28+
// SAFETY: atexit runs once at program exit; no code accesses this static afterward.
29+
unsafe { ManuallyDrop::drop(&mut *guard) };
30+
}
1831

1932
pub(crate) struct ManagedExceptionHashRateLimiter {
2033
limiter: ExceptionHashRateLimiter,

datadog-sidecar/src/tracer.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ use libdd_common::Endpoint;
88
use libdd_trace_utils::config_utils::trace_intake_url_prefixed;
99
use std::borrow::Cow;
1010
use std::ffi::CString;
11+
use std::mem::ManuallyDrop;
1112
use std::str::FromStr;
1213
use std::sync::{LazyLock, Mutex};
1314

14-
pub static SHM_LIMITER: LazyLock<Mutex<ShmLimiterMemory<()>>> = LazyLock::new(|| {
15+
pub static SHM_LIMITER: LazyLock<Mutex<ManuallyDrop<ShmLimiterMemory<()>>>> = LazyLock::new(|| {
16+
unsafe { libc::atexit(drop_shm_limiter) };
1517
#[allow(clippy::unwrap_used)]
16-
Mutex::new(ShmLimiterMemory::create(shm_limiter_path()).unwrap())
18+
Mutex::new(ManuallyDrop::new(
19+
ShmLimiterMemory::create(shm_limiter_path()).unwrap(),
20+
))
1721
});
1822

23+
extern "C" fn drop_shm_limiter() {
24+
let mut guard = SHM_LIMITER.lock().unwrap_or_else(|e| e.into_inner());
25+
// SAFETY: atexit runs once at program exit; no code accesses this static afterward.
26+
unsafe { ManuallyDrop::drop(&mut *guard) };
27+
}
28+
1929
#[derive(Default)]
2030
pub struct Config {
2131
pub endpoint: Option<Endpoint>,

0 commit comments

Comments
 (0)