@@ -17,6 +17,19 @@ use tokio::{
1717/// go tool pprof -http=:8080 heap.pprof
1818/// ```
1919pub fn spawn_heap_dump_handler ( ) {
20+ // Check if jemalloc profiling is available before spawning the handler
21+ // This prevents panics that would crash the entire process
22+ let profiling_available =
23+ std:: panic:: catch_unwind ( || jemalloc_pprof:: PROF_CTL . as_ref ( ) . is_some ( ) ) . unwrap_or ( false ) ;
24+
25+ if !profiling_available {
26+ tracing:: warn!(
27+ "jemalloc profiling not available - heap dump handler not started. Ensure service is \
28+ built with jemalloc-profiling feature and MALLOC_CONF is set."
29+ ) ;
30+ return ;
31+ }
32+
2033 tokio:: spawn ( async move {
2134 let name = binary_name ( ) . unwrap_or_default ( ) ;
2235
@@ -82,22 +95,11 @@ async fn handle_connection(listener: &UnixListener) {
8295async fn generate_and_stream_dump ( socket : & mut UnixStream ) {
8396 tracing:: info!( "generating heap dump via jemalloc_pprof" ) ;
8497
85- // Access the global profiling controller
86- // Catch panic if jemalloc profiling is not available
87- let prof_ctl = match std:: panic:: catch_unwind ( || jemalloc_pprof:: PROF_CTL . as_ref ( ) ) {
88- Ok ( Some ( ctl) ) => ctl,
89- Ok ( None ) => {
90- tracing:: error!( "jemalloc profiling not initialized" ) ;
91- return ;
92- }
93- Err ( _) => {
94- tracing:: error!(
95- "jemalloc profiling not available - service not built with jemalloc-profiling \
96- feature or allocator not configured"
97- ) ;
98- return ;
99- }
100- } ;
98+ // PROF_CTL was already verified to be available in spawn_heap_dump_handler
99+ // so we can safely unwrap here. If this panics, it means there's a serious bug.
100+ let prof_ctl = jemalloc_pprof:: PROF_CTL
101+ . as_ref ( )
102+ . expect ( "PROF_CTL should be available - checked at handler spawn" ) ;
101103
102104 let mut prof_ctl = prof_ctl. lock ( ) . await ;
103105
0 commit comments