Skip to content

Commit

Permalink
Do not panic when failing to release resources in Drop impls.
Browse files Browse the repository at this point in the history
C.f. rust-lang/lang-team#97 for a general discussion
on why this is undesirable.
  • Loading branch information
adamreichold committed Sep 19, 2021
1 parent 0632c6a commit 8f19b19
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,12 @@ impl Drop for MmapInner {
let alignment = self.ptr as usize % page_size();
let len = self.len + alignment;
let len = len.max(1);
// Any errors during unmapping/closing are ignored as the only way
// to report them would be through panicking which is highly discouraged
// in Drop impls, c.f. https://github.com/rust-lang/lang-team/issues/97
unsafe {
let ptr = self.ptr.offset(-(alignment as isize));
assert!(
libc::munmap(ptr, len as libc::size_t) == 0,
"unable to unmap mmap: {}",
io::Error::last_os_error()
);
libc::munmap(ptr, len as libc::size_t);
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,12 @@ impl Drop for MmapInner {
return;
}
let alignment = self.ptr as usize % allocation_granularity();
// Any errors during unmapping/closing are ignored as the only way
// to report them would be through panicking which is highly discouraged
// in Drop impls, c.f. https://github.com/rust-lang/lang-team/issues/97
unsafe {
let ptr = self.ptr.offset(-(alignment as isize));
assert!(
UnmapViewOfFile(ptr) != 0,
"unable to unmap mmap: {}",
io::Error::last_os_error()
);
UnmapViewOfFile(ptr);

if let Some(handle) = self.handle {
CloseHandle(handle);
Expand Down

0 comments on commit 8f19b19

Please sign in to comment.