Skip to content

Commit f88d470

Browse files
RaicyAugustoRaicy Augusto
andauthored
rs_loader: defer temp dir cleanup to Drop (#712)
The temp directory was being removed immediately after dlopen, before the library was unloaded. Cleanup is now deferred to the Drop impl for MemoryRegistration, ensuring the temp dir is only removed after the DynlinkLibrary is dropped Co-authored-by: Raicy Augusto <raicy.augusto.rodrigues@gmail.com>
1 parent e4d1b56 commit f88d470

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

source/loaders/rs_loader/rust/compiler/src/memory.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ impl MemoryRegistration {
2828
Ok(instance) => instance,
2929
Err(error) => return Err(RegistrationError::DynlinkError(error)),
3030
};
31-
// cleanup temp dir
32-
let mut destination = state.output.clone();
33-
destination.pop();
34-
std::fs::remove_dir_all(destination).expect("Unable to cleanup tempdir");
35-
3631
Ok(MemoryRegistration {
3732
name,
3833
state,
@@ -50,3 +45,14 @@ impl MemoryRegistration {
5045
}
5146
}
5247
}
48+
49+
impl Drop for MemoryRegistration {
50+
fn drop(&mut self) {
51+
drop(std::mem::replace(&mut self.dynlink, None));
52+
53+
let mut path = std::mem::take(&mut self.state.output);
54+
if path.pop() {
55+
let _ = std::fs::remove_dir_all(path);
56+
}
57+
}
58+
}

source/loaders/rs_loader/rust/src/lifecycle/loader.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ impl LoadingMethod {
3232
}
3333
None => Err(String::from("consume_dlib was called more than once")),
3434
},
35-
Self::Memory(MemoryRegistration { mut dynlink, .. }) => match dynlink {
36-
Some(_) => {
37-
let dl = dynlink.take();
38-
Ok(dl.expect("Unexpected: Dynlink library is None"))
35+
Self::Memory(mut memory) => {
36+
let dl = std::mem::replace(&mut memory.dynlink, None);
37+
match dl {
38+
Some(dl) => Ok(dl),
39+
None => Err(String::from("consume_dlib was called more than once")),
3940
}
40-
None => Err(String::from("consume_dlib was called more than once")),
4141
},
4242
}
4343
}

0 commit comments

Comments
 (0)