Skip to content

Commit e928056

Browse files
committed
Fixes
1 parent 795930f commit e928056

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

.github/workflows/deploy-profiling.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
description: 'Tag suffix for the profiling image (e.g., "profiling" or "profiling-v1.2.3")'
77
required: false
88
default: 'profiling'
9+
push:
10+
branches:
11+
- jemalloc-profiling-support
912

1013
jobs:
1114
deploy-profiling:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ serde_json = "1.0.117"
4646
serde_with = "3.8.1"
4747
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio", "tls-native-tls", "bigdecimal", "chrono", "postgres", "macros"] }
4848
strum = { version = "0.27.2", features = ["derive"] }
49-
tempfile = "3.11"
49+
tempfile = "3.10.1"
5050
thiserror = "1.0.61"
5151
toml = "0.8.14"
5252
tokio = { version = "1.38.0", features = ["tracing"] }

crates/driver/src/infra/observe/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub mod metrics;
3838
pub fn init(obs_config: observe::Config) {
3939
observe::tracing::initialize_reentrant(&obs_config);
4040
metrics::init();
41+
#[cfg(all(unix, feature = "jemalloc-profiling"))]
42+
observe::heap_dump_handler::spawn_heap_dump_handler();
4143
}
4244

4345
/// Observe a received auction.

crates/driver/src/run.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ use {
2727
/// driver from multiple binaries.
2828
pub async fn start(args: impl Iterator<Item = String>) {
2929
observe::panic_hook::install();
30-
#[cfg(all(unix, feature = "jemalloc-profiling"))]
31-
observe::heap_dump_handler::spawn_heap_dump_handler();
3230
let args = cli::Args::parse_from(args);
3331
run_with(args, None).await
3432
}

crates/observe/src/heap_dump_handler.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use tokio::{
22
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
33
net::{UnixListener, UnixStream},
4+
time::{Duration, timeout},
45
};
56

6-
/// Spawns a new thread that listens for connections to a UNIX socket
7+
/// Spawns a new async task that listens for connections to a UNIX socket
78
/// at "/tmp/heap_dump_<process_name>.sock".
89
/// When "dump" command is sent, it generates a heap profile using
910
/// jemalloc_pprof and streams the binary protobuf data back through the socket.
@@ -31,7 +32,7 @@ pub fn spawn_heap_dump_handler() {
3132
}
3233

3334
tokio::spawn(async move {
34-
let name = binary_name().unwrap_or_default();
35+
let name = binary_name().unwrap_or("unknown".to_string());
3536
let socket_path = format!("/tmp/heap_dump_{name}.sock");
3637

3738
tracing::info!(socket = socket_path, "heap dump handler started");
@@ -43,16 +44,29 @@ pub fn spawn_heap_dump_handler() {
4344
};
4445

4546
loop {
46-
// Catch any panics in connection handling to prevent the handler from crashing
47-
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
48-
let listener = &handle.listener;
49-
async move { handle_connection(listener).await }
50-
}));
51-
52-
match result {
53-
Ok(future) => future.await,
47+
// Accept connection in main loop, then spawn task for each connection
48+
// Sequential processing prevents multiple simultaneous expensive heap dumps
49+
match handle.listener.accept().await {
50+
Ok((socket, _addr)) => {
51+
let handle = tokio::spawn(async move {
52+
handle_connection_with_socket(socket).await;
53+
});
54+
55+
// 5-minute timeout to prevent stuck dumps from blocking future requests
56+
match timeout(Duration::from_secs(300), handle).await {
57+
Ok(Ok(())) => {
58+
// Task completed successfully
59+
}
60+
Ok(Err(err)) => {
61+
tracing::error!(?err, "panic in heap dump connection handler");
62+
}
63+
Err(_) => {
64+
tracing::error!("heap dump request timed out after 5 minutes");
65+
}
66+
}
67+
}
5468
Err(err) => {
55-
tracing::error!(?err, "panic in heap dump connection handler");
69+
tracing::debug!(?err, "failed to accept connection");
5670
}
5771
}
5872
}
@@ -80,12 +94,7 @@ fn binary_name() -> Option<String> {
8094
)
8195
}
8296

83-
async fn handle_connection(listener: &UnixListener) {
84-
let Ok((mut socket, _addr)) = listener.accept().await else {
85-
tracing::debug!("failed to accept connection");
86-
return;
87-
};
88-
97+
async fn handle_connection_with_socket(mut socket: UnixStream) {
8998
let message = read_line(&mut socket).await;
9099
match message.as_deref() {
91100
Some("dump") => {

crates/solvers/src/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use {
1212

1313
pub async fn start(args: impl IntoIterator<Item = String>) {
1414
observe::panic_hook::install();
15-
#[cfg(all(unix, feature = "jemalloc-profiling"))]
16-
observe::heap_dump_handler::spawn_heap_dump_handler();
1715
let args = cli::Args::parse_from(args);
1816
run_with(args, None).await;
1917
}
@@ -34,6 +32,8 @@ async fn run_with(args: cli::Args, bind: Option<oneshot::Sender<SocketAddr>>) {
3432
None,
3533
);
3634
observe::tracing::initialize_reentrant(&obs_config);
35+
#[cfg(all(unix, feature = "jemalloc-profiling"))]
36+
observe::heap_dump_handler::spawn_heap_dump_handler();
3737

3838
let commit_hash = option_env!("VERGEN_GIT_SHA").unwrap_or("COMMIT_INFO_NOT_FOUND");
3939

0 commit comments

Comments
 (0)