memmap2-rs
memmap2-rs copied to clipboard
PermissionDenied, message: "Access is denied." at remove file after read on Github Action WIndows
I'm trying to test a rust project on Github Action but it yields an error only on Windows.
That's all right at my desktop and Github Action Linux/macos so I don't know what I should troubleshoot.
#[test]
fn file_remove() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("mmap");
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(path.clone())
.unwrap();
file.set_len(128).unwrap();
let mmap = unsafe { Mmap::map(&file) };
assert!(mmap.is_ok());
let remove_res = fs::remove_file(path.clone());
if remove_res.is_err() {
println!("remove_res: {:?}", remove_res);
}
assert!(remove_res.is_ok());
}
https://github.com/LokiSharp/memmap2-rs/blob/00de205e006aabb69e03172c04da869c18cd776f/src/lib.rs#L1575
---- test::file_remove stdout ----
remove_res: Err(Os { code: 5, kind: PermissionDenied, message: "Access is denied." })
thread 'test::file_remove' panicked at src\lib.rs:1595:9:
assertion failed: remove_res.is_ok()
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
https://github.com/LokiSharp/memmap2-rs/actions/runs/7223354238/job/19682434953
Isn't that how Windows generally behaves, i.e. that opened files cannot be deleted? (I think it is more forgiving w.r.t. to the mappings.) So I think you want to call drop(file);
before calling remove_file
in your test? Possibly even drop(mmap);
but I am not sure about the second one.
Theoretically, that's the case, but what confuses me is that on my local Windows 11 machine, I can delete the file normally without needing to drop mmap. However, only on Github Action's Windows Server 2022 does it throw an error. I will try to set up Windows Server 2022 locally for testing. I suspect that the difference in how Windows Server 2022 and Windows 11 handle memory mapping might be the cause.