Skip to content

Commit

Permalink
Fix memory leak in TempDir
Browse files Browse the repository at this point in the history
  • Loading branch information
akoshelev committed Oct 13, 2023
1 parent 47b4120 commit 73b0d65
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
5 changes: 2 additions & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ pub fn test_multiply(config_dir: &Path, https: bool) {
}

pub fn test_network(https: bool) {
// set to true to always keep the temp dir after test finishes
let dir = TempDir::new(false);
let dir = TempDir::new_delete_on_drop();
let path = dir.path();

println!("generating configuration in {}", path.display());
Expand All @@ -206,7 +205,7 @@ pub fn test_ipa(mode: IpaSecurityModel, https: bool) {
pub fn test_ipa_with_config(mode: IpaSecurityModel, https: bool, config: IpaQueryConfig) {
const INPUT_SIZE: usize = 10;
// set to true to always keep the temp dir after test finishes
let dir = TempDir::new(false);
let dir = TempDir::new_delete_on_drop();
let path = dir.path();

println!("generating configuration in {}", path.display());
Expand Down
12 changes: 6 additions & 6 deletions tests/common/tempdir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{mem, path::Path, thread};
use std::{path::Path, thread};

use tempfile::tempdir;

Expand All @@ -12,16 +12,16 @@ pub struct TempDir {
}

impl TempDir {
/// Creates a new temporary directory. If `delete` is set to `false`, then it won't be cleaned up
/// after drop.
/// Creates a new temporary directory that will be deleted when this instance is dropped.
/// There is an exception if thread is panicking, then it won't be.
///
/// ## Panics
/// Panics if a new temp dir cannot be created.
#[must_use]
pub fn new(delete: bool) -> Self {
pub fn new_delete_on_drop() -> Self {
Self {
inner: Some(tempdir().expect("Can create temp directory")),
delete,
delete: true,
}
}

Expand All @@ -37,7 +37,7 @@ impl Drop for TempDir {
fn drop(&mut self) {
if !self.delete || thread::panicking() {
let td = self.inner.take().unwrap();
mem::forget(td);
let _ = td.into_path();
}
}
}
2 changes: 1 addition & 1 deletion tests/helper_networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn https_semi_honest_ipa() {
#[test]
#[cfg(all(test, web_test))]
fn keygen_confgen() {
let dir = TempDir::new(false);
let dir = TempDir::new_delete_on_drop();
let path = dir.path();

let sockets: [_; 3] = array::from_fn(|_| TcpListener::bind("127.0.0.1:0").unwrap());
Expand Down

0 comments on commit 73b0d65

Please sign in to comment.