Skip to content

Commit

Permalink
server: Fix heap profile temp file is dropped before reading (#16171)
Browse files Browse the repository at this point in the history
close #16169

Fix heap profile temp file is dropped before reading

Signed-off-by: Connor1996 <zbk602423539@gmail.com>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
Connor1996 and ti-chi-bot[bot] committed Dec 12, 2023
1 parent 95da026 commit 51a5af2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/server/status_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ where
let use_jeprof = query_pairs.get("jeprof").map(|x| x.as_ref()) == Some("true");

let result = {
let path = match dump_one_heap_profile() {
Ok(path) => path,
let file = match dump_one_heap_profile() {
Ok(file) => file,
Err(e) => return Ok(make_response(StatusCode::INTERNAL_SERVER_ERROR, e)),
};
let path = file.path();
if use_jeprof {
jeprof_heap_profile(path.to_str().unwrap())
} else {
Expand Down Expand Up @@ -1561,7 +1562,6 @@ mod tests {

#[cfg(feature = "mem-profiling")]
#[test]
#[ignore]
fn test_pprof_heap_service() {
let mut status_server = StatusServer::new(
1,
Expand Down
5 changes: 2 additions & 3 deletions src/server/status_server/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::{
fs::File,
io::{Read, Write},
path::PathBuf,
pin::Pin,
process::{Command, Stdio},
sync::Mutex,
Expand Down Expand Up @@ -83,11 +82,11 @@ impl<I, T> Future for ProfileRunner<I, T> {
}

/// Trigger a heap profile and return the content.
pub fn dump_one_heap_profile() -> Result<PathBuf, String> {
pub fn dump_one_heap_profile() -> Result<NamedTempFile, String> {
let f = NamedTempFile::new().map_err(|e| format!("create tmp file fail: {}", e))?;
let path = f.path();
dump_prof(path.to_str().unwrap()).map_err(|e| format!("dump_prof: {}", e))?;
Ok(path.to_owned())
Ok(f)
}

/// Trigger one cpu profile.
Expand Down

0 comments on commit 51a5af2

Please sign in to comment.