Skip to content

Commit

Permalink
fix: disable lock_event tracing to fix tests leak
Browse files Browse the repository at this point in the history
The tracing-tests crate provides the #[traced_test] macro.

When used, log data gets written to a global static buffer.  See:
https://docs.rs/tracing-test/latest/tracing_test/

This was causing a huge mem leak, eg 6.5Gb+.

We may wish to re-evaluate use of #[traced_test], but for now
disabling lock_event tracing cures most of the leak.

Anyway, this tracing is very verbose and not usually needed.
we can easily re-enable if we need to debug a deadlock.
  • Loading branch information
dan-da committed Feb 14, 2024
1 parent 46b6fb7 commit 5b81b10
Showing 1 changed file with 76 additions and 60 deletions.
136 changes: 76 additions & 60 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,36 +303,44 @@ where
// we can track which threads+tasks are acquiring
// which locks for reads and/or mutations.
pub(crate) fn log_lock_event(lock_event: LockEvent) {
let tokio_id = match tokio::task::try_id() {
Some(id) => format!("{}", id),
None => "?".to_string(),
};

let (event_type, info, acquisition) = match lock_event {
LockEvent::TryAcquire {
ref info,
acquisition,
} => ("TryAcquire", info, acquisition),
LockEvent::Acquire {
ref info,
acquisition,
} => ("Acquire", info, acquisition),
LockEvent::Release {
ref info,
acquisition,
} => ("Release", info, acquisition),
};
trace!(
?lock_event,
"{} lock `{}` of type `{}` for `{}` by\n\t|-- thread {}, (`{}`)\n\t|-- tokio task {}\n\t|--",
event_type,
info.name().unwrap_or("?"),
info.lock_type(),
acquisition,
current_thread_id(),
std::thread::current().name().unwrap_or("?"),
tokio_id,
);
// Disabling lock-event logging for now.
// Reasons:
// 1. It is very verbose in the logs.
// 2. It's not really needed right now.
// 3. tracing-tests is causing a big mem-leak for tests.
// 4. We can re-enable if needed by toggling false/true.
if false {
let tokio_id = match tokio::task::try_id() {
Some(id) => format!("{}", id),
None => "?".to_string(),
};

let (event_type, info, acquisition) = match lock_event {
LockEvent::TryAcquire {
ref info,
acquisition,
} => ("TryAcquire", info, acquisition),
LockEvent::Acquire {
ref info,
acquisition,
} => ("Acquire", info, acquisition),
LockEvent::Release {
ref info,
acquisition,
} => ("Release", info, acquisition),
};
trace!(
?lock_event,
"{} lock `{}` of type `{}` for `{}` by\n\t|-- thread {}, (`{}`)\n\t|-- tokio task {}\n\t|--",
event_type,
info.name().unwrap_or("?"),
info.lock_type(),
acquisition,
current_thread_id(),
std::thread::current().name().unwrap_or("?"),
tokio_id,
);
}
}
const LOG_LOCK_EVENT_CB: LockCallbackFn = log_lock_event;

Expand Down Expand Up @@ -360,35 +368,43 @@ pub(crate) fn current_thread_id() -> u64 {
// we can track which threads+tasks are acquiring
// which locks for reads and/or mutations.
pub(crate) fn log_tokio_lock_event(lock_event: sync_tokio::LockEvent) {
let tokio_id = match tokio::task::try_id() {
Some(id) => format!("{}", id),
None => "?".to_string(),
};

let (event_type, info, acquisition) = match lock_event {
sync_tokio::LockEvent::TryAcquire {
ref info,
acquisition,
} => ("TryAcquire", info, acquisition),
sync_tokio::LockEvent::Acquire {
ref info,
acquisition,
} => ("Acquire", info, acquisition),
sync_tokio::LockEvent::Release {
ref info,
acquisition,
} => ("Release", info, acquisition),
};
trace!(
?lock_event,
"{} tokio lock `{}` of type `{}` for `{}` by\n\t|-- thread {}, (`{}`)\n\t|-- tokio task {}\n\t|--",
event_type,
info.name().unwrap_or("?"),
info.lock_type(),
acquisition,
current_thread_id(),
std::thread::current().name().unwrap_or("?"),
tokio_id,
);
// Disabling lock-event logging for now.
// Reasons:
// 1. It is very verbose in the logs.
// 2. It's not really needed right now.
// 3. tracing-tests is causing a big mem-leak for tests.
// 4. We can re-enable if needed by toggling false/true.
if false {
let tokio_id = match tokio::task::try_id() {
Some(id) => format!("{}", id),
None => "?".to_string(),
};

let (event_type, info, acquisition) = match lock_event {
sync_tokio::LockEvent::TryAcquire {
ref info,
acquisition,
} => ("TryAcquire", info, acquisition),
sync_tokio::LockEvent::Acquire {
ref info,
acquisition,
} => ("Acquire", info, acquisition),
sync_tokio::LockEvent::Release {
ref info,
acquisition,
} => ("Release", info, acquisition),
};
trace!(
?lock_event,
"{} tokio lock `{}` of type `{}` for `{}` by\n\t|-- thread {}, (`{}`)\n\t|-- tokio task {}\n\t|--",
event_type,
info.name().unwrap_or("?"),
info.lock_type(),
acquisition,
current_thread_id(),
std::thread::current().name().unwrap_or("?"),
tokio_id,
);
}
}
const LOG_TOKIO_LOCK_EVENT_CB: sync_tokio::LockCallbackFn = log_tokio_lock_event;

0 comments on commit 5b81b10

Please sign in to comment.