Removes `remove_on_drop` field from AppendVec (#32741)

This commit is contained in:
Brooks 2023-08-07 13:25:07 -04:00 committed by GitHub
parent 511cf28be8
commit c2dec254c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 91 additions and 95 deletions

View File

@ -217,9 +217,6 @@ pub struct AppendVec {
/// The number of bytes available for storing items. /// The number of bytes available for storing items.
file_size: u64, file_size: u64,
/// True if the file should automatically be deleted when this AppendVec is dropped.
remove_on_drop: bool,
} }
lazy_static! { lazy_static! {
@ -228,17 +225,15 @@ lazy_static! {
impl Drop for AppendVec { impl Drop for AppendVec {
fn drop(&mut self) { fn drop(&mut self) {
if self.remove_on_drop {
APPEND_VEC_MMAPPED_FILES_OPEN.fetch_sub(1, Ordering::Relaxed); APPEND_VEC_MMAPPED_FILES_OPEN.fetch_sub(1, Ordering::Relaxed);
if let Err(_e) = remove_file(&self.path) { if let Err(_err) = remove_file(&self.path) {
// promote this to panic soon. // promote this to panic soon.
// disabled due to many false positive warnings while running tests. // disabled due to many false positive warnings while running tests.
// blocked by rpc's upgrade to jsonrpc v17 // blocked by rpc's upgrade to jsonrpc v17
//error!("AppendVec failed to remove {:?}: {:?}", &self.path, e); //error!("AppendVec failed to remove {}: {err}", &self.path.display());
inc_new_counter_info!("append_vec_drop_fail", 1); inc_new_counter_info!("append_vec_drop_fail", 1);
} }
} }
}
} }
impl AppendVec { impl AppendVec {
@ -294,12 +289,11 @@ impl AppendVec {
append_lock: Mutex::new(()), append_lock: Mutex::new(()),
current_len: AtomicUsize::new(initial_len), current_len: AtomicUsize::new(initial_len),
file_size: size as u64, file_size: size as u64,
remove_on_drop: true,
} }
} }
pub fn set_no_remove_on_drop(&mut self) { pub fn set_no_remove_on_drop(&mut self) {
self.remove_on_drop = false; // noop: will be removed next
} }
fn sanitize_len_and_size(current_len: usize, file_size: usize) -> Result<()> { fn sanitize_len_and_size(current_len: usize, file_size: usize) -> Result<()> {
@ -398,7 +392,6 @@ impl AppendVec {
append_lock: Mutex::new(()), append_lock: Mutex::new(()),
current_len: AtomicUsize::new(current_len), current_len: AtomicUsize::new(current_len),
file_size, file_size,
remove_on_drop: true,
}) })
} }
@ -667,7 +660,7 @@ pub mod tests {
account::{accounts_equal, Account, AccountSharedData, WritableAccount}, account::{accounts_equal, Account, AccountSharedData, WritableAccount},
timing::duration_as_ms, timing::duration_as_ms,
}, },
std::time::Instant, std::{mem::ManuallyDrop, time::Instant},
}; };
impl AppendVec { impl AppendVec {
@ -1176,8 +1169,9 @@ pub mod tests {
fn test_new_from_file_crafted_data_len() { fn test_new_from_file_crafted_data_len() {
let file = get_append_vec_path("test_new_from_file_crafted_data_len"); let file = get_append_vec_path("test_new_from_file_crafted_data_len");
let path = &file.path; let path = &file.path;
let mut av = AppendVec::new(path, true, 1024 * 1024); let accounts_len = {
av.set_no_remove_on_drop(); // wrap AppendVec in ManuallyDrop to ensure we do not remove the backing file when dropped
let av = ManuallyDrop::new(AppendVec::new(path, true, 1024 * 1024));
let crafted_data_len = 1; let crafted_data_len = 1;
@ -1194,8 +1188,8 @@ pub mod tests {
assert_eq!(account.data_len(), crafted_data_len); assert_eq!(account.data_len(), crafted_data_len);
av.flush().unwrap(); av.flush().unwrap();
let accounts_len = av.len(); av.len()
drop(av); };
let result = AppendVec::new_from_file(path, accounts_len); let result = AppendVec::new_from_file(path, accounts_len);
assert_matches!(result, Err(ref message) if message.to_string().contains("incorrect layout/length/data")); assert_matches!(result, Err(ref message) if message.to_string().contains("incorrect layout/length/data"));
} }
@ -1204,8 +1198,9 @@ pub mod tests {
fn test_new_from_file_too_large_data_len() { fn test_new_from_file_too_large_data_len() {
let file = get_append_vec_path("test_new_from_file_too_large_data_len"); let file = get_append_vec_path("test_new_from_file_too_large_data_len");
let path = &file.path; let path = &file.path;
let mut av = AppendVec::new(path, true, 1024 * 1024); let accounts_len = {
av.set_no_remove_on_drop(); // wrap AppendVec in ManuallyDrop to ensure we do not remove the backing file when dropped
let av = ManuallyDrop::new(AppendVec::new(path, true, 1024 * 1024));
let too_large_data_len = u64::max_value(); let too_large_data_len = u64::max_value();
av.append_account_test(&create_test_account(10)).unwrap(); av.append_account_test(&create_test_account(10)).unwrap();
@ -1220,8 +1215,8 @@ pub mod tests {
assert_matches!(accounts.first(), None); assert_matches!(accounts.first(), None);
av.flush().unwrap(); av.flush().unwrap();
let accounts_len = av.len(); av.len()
drop(av); };
let result = AppendVec::new_from_file(path, accounts_len); let result = AppendVec::new_from_file(path, accounts_len);
assert_matches!(result, Err(ref message) if message.to_string().contains("incorrect layout/length/data")); assert_matches!(result, Err(ref message) if message.to_string().contains("incorrect layout/length/data"));
} }
@ -1230,8 +1225,9 @@ pub mod tests {
fn test_new_from_file_crafted_executable() { fn test_new_from_file_crafted_executable() {
let file = get_append_vec_path("test_new_from_crafted_executable"); let file = get_append_vec_path("test_new_from_crafted_executable");
let path = &file.path; let path = &file.path;
let mut av = AppendVec::new(path, true, 1024 * 1024); let accounts_len = {
av.set_no_remove_on_drop(); // wrap AppendVec in ManuallyDrop to ensure we do not remove the backing file when dropped
let av = ManuallyDrop::new(AppendVec::new(path, true, 1024 * 1024));
av.append_account_test(&create_test_account(10)).unwrap(); av.append_account_test(&create_test_account(10)).unwrap();
{ {
let mut executable_account = create_test_account(10); let mut executable_account = create_test_account(10);
@ -1282,8 +1278,8 @@ pub mod tests {
} }
av.flush().unwrap(); av.flush().unwrap();
let accounts_len = av.len(); av.len()
drop(av); };
let result = AppendVec::new_from_file(path, accounts_len); let result = AppendVec::new_from_file(path, accounts_len);
assert_matches!(result, Err(ref message) if message.to_string().contains("incorrect layout/length/data")); assert_matches!(result, Err(ref message) if message.to_string().contains("incorrect layout/length/data"));
} }