Skip to content

Commit 4281ad2

Browse files
Use lock file instead of tmp file for compiling contract
1 parent 4d4849a commit 4281ad2

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/executor/contract.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use std::{
7272
ffi::c_void,
7373
fs::{self, File},
7474
io,
75-
path::PathBuf,
75+
path::{Path, PathBuf},
7676
ptr::NonNull,
7777
sync::Arc,
7878
};
@@ -256,13 +256,10 @@ impl AotContractExecutor {
256256
})
257257
.collect::<Result<BTreeMap<_, _>>>()?;
258258

259-
// Build the shared library into a temporary path.
260-
let temp_path = NamedTempFile::new()?
261-
.into_temp_path()
262-
.keep()
263-
.to_native_assert_error("can only fail on windows")?;
264259
let object_data = crate::module_to_object(&module, opt_level)?;
265-
crate::object_to_shared_lib(&object_data, &temp_path)?;
260+
261+
// Build the shared library into the lockfile, to avoid using a tmp file.
262+
crate::object_to_shared_lib(&object_data, &lock_file.0)?;
266263

267264
// Write the contract info.
268265
fs::write(
@@ -275,9 +272,8 @@ impl AotContractExecutor {
275272

276273
// Atomically move the built shared library to the correct path. This will avoid data races
277274
// when loading contracts.
278-
fs::rename(temp_path, &output_path)?;
275+
lock_file.rename(&output_path)?;
279276

280-
drop(lock_file);
281277
Self::from_path(output_path)
282278
}
283279

@@ -637,6 +633,15 @@ impl LockFile {
637633
Err(e) => Err(e),
638634
}
639635
}
636+
637+
pub fn rename(self, path: impl AsRef<Path>) -> io::Result<()> {
638+
fs::rename(&self.0, path.as_ref())?;
639+
640+
// don't remove lockfile, as we just renamed it
641+
std::mem::forget(self);
642+
643+
Ok(())
644+
}
640645
}
641646

642647
impl Drop for LockFile {

0 commit comments

Comments
 (0)