From c95c6b67ee9927a924554cac4743da9b06d9b3d3 Mon Sep 17 00:00:00 2001 From: "Levy A." Date: Sun, 27 Jul 2025 16:05:07 -0300 Subject: [PATCH 1/2] fix: thread-safe `WindowsFile` --- core/io/windows.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/core/io/windows.rs b/core/io/windows.rs index 3f7ea3a53..6fd264fb8 100644 --- a/core/io/windows.rs +++ b/core/io/windows.rs @@ -1,5 +1,6 @@ use super::MemoryIO; use crate::{Clock, Completion, File, Instant, LimboError, OpenFlags, Result, IO}; +use parking_lot::RwLock; use std::cell::RefCell; use std::io::{Read, Seek, Write}; use std::sync::Arc; @@ -13,9 +14,6 @@ impl WindowsIO { } } -unsafe impl Send for WindowsIO {} -unsafe impl Sync for WindowsIO {} - impl IO for WindowsIO { fn open_file(&self, path: &str, flags: OpenFlags, direct: bool) -> Result> { trace!("open_file(path = {})", path); @@ -29,7 +27,7 @@ impl IO for WindowsIO { let file = file.open(path)?; Ok(Arc::new(WindowsFile { - file: RefCell::new(file), + file: RwLock::new(file), })) } @@ -66,12 +64,9 @@ impl Clock for WindowsIO { } pub struct WindowsFile { - file: RefCell, + file: RwLock, } -unsafe impl Send for WindowsFile {} -unsafe impl Sync for WindowsFile {} - impl File for WindowsFile { fn lock_file(&self, exclusive: bool) -> Result<()> { unimplemented!() @@ -82,7 +77,7 @@ impl File for WindowsFile { } fn pread(&self, pos: usize, c: Arc) -> Result> { - let mut file = self.file.borrow_mut(); + let mut file = self.file.write(); file.seek(std::io::SeekFrom::Start(pos as u64))?; let nr = { let r = c.as_read(); @@ -101,7 +96,7 @@ impl File for WindowsFile { buffer: Arc>, c: Arc, ) -> Result> { - let mut file = self.file.borrow_mut(); + let mut file = self.file.write(); file.seek(std::io::SeekFrom::Start(pos as u64))?; let buf = buffer.borrow(); let buf = buf.as_slice(); @@ -111,14 +106,14 @@ impl File for WindowsFile { } fn sync(&self, c: Arc) -> Result> { - let file = self.file.borrow_mut(); + let file = self.file.write(); file.sync_all().map_err(LimboError::IOError)?; c.complete(0); Ok(c) } fn size(&self) -> Result { - let file = self.file.borrow(); + let file = self.file.read(); Ok(file.metadata().unwrap().len()) } } From 1f57ab02cfbcdb6cb01f92a60bd6692c8f4ff124 Mon Sep 17 00:00:00 2001 From: "Levy A." Date: Sun, 27 Jul 2025 16:05:44 -0300 Subject: [PATCH 2/2] feat: instrument `WindowsIO` functions --- core/io/windows.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/io/windows.rs b/core/io/windows.rs index 6fd264fb8..6ffdc005b 100644 --- a/core/io/windows.rs +++ b/core/io/windows.rs @@ -4,7 +4,7 @@ use parking_lot::RwLock; use std::cell::RefCell; use std::io::{Read, Seek, Write}; use std::sync::Arc; -use tracing::{debug, trace}; +use tracing::{debug, instrument, trace, Level}; pub struct WindowsIO {} impl WindowsIO { @@ -15,6 +15,7 @@ impl WindowsIO { } impl IO for WindowsIO { + #[instrument(err, skip_all, level = Level::TRACE)] fn open_file(&self, path: &str, flags: OpenFlags, direct: bool) -> Result> { trace!("open_file(path = {})", path); let mut file = std::fs::File::options(); @@ -31,6 +32,7 @@ impl IO for WindowsIO { })) } + #[instrument(err, skip_all, level = Level::TRACE)] fn wait_for_completion(&self, c: Arc) -> Result<()> { while !c.is_completed() { self.run_once()?; @@ -38,16 +40,19 @@ impl IO for WindowsIO { Ok(()) } + #[instrument(err, skip_all, level = Level::TRACE)] fn run_once(&self) -> Result<()> { Ok(()) } + #[instrument(skip_all, level = Level::TRACE)] fn generate_random_number(&self) -> i64 { let mut buf = [0u8; 8]; getrandom::getrandom(&mut buf).unwrap(); i64::from_ne_bytes(buf) } + #[instrument(skip_all, level = Level::TRACE)] fn get_memory_io(&self) -> Arc { Arc::new(MemoryIO::new()) } @@ -68,14 +73,17 @@ pub struct WindowsFile { } impl File for WindowsFile { + #[instrument(err, skip_all, level = Level::TRACE)] fn lock_file(&self, exclusive: bool) -> Result<()> { unimplemented!() } + #[instrument(err, skip_all, level = Level::TRACE)] fn unlock_file(&self) -> Result<()> { unimplemented!() } + #[instrument(skip(self, c), level = Level::TRACE)] fn pread(&self, pos: usize, c: Arc) -> Result> { let mut file = self.file.write(); file.seek(std::io::SeekFrom::Start(pos as u64))?; @@ -90,6 +98,7 @@ impl File for WindowsFile { Ok(c) } + #[instrument(skip(self, c, buffer), level = Level::TRACE)] fn pwrite( &self, pos: usize, @@ -105,6 +114,7 @@ impl File for WindowsFile { Ok(c) } + #[instrument(err, skip_all, level = Level::TRACE)] fn sync(&self, c: Arc) -> Result> { let file = self.file.write(); file.sync_all().map_err(LimboError::IOError)?; @@ -112,6 +122,7 @@ impl File for WindowsFile { Ok(c) } + #[instrument(err, skip_all, level = Level::TRACE)] fn size(&self) -> Result { let file = self.file.read(); Ok(file.metadata().unwrap().len())