Fix io_uring WAL write corruption by ensuring buffer lifetime

Ensure the Arc<RefCell<Buffer>> in UringFile::pwrite remains alive until
the io_uring write completes by referencing it in the completion callback.
This prevents WAL file corruption where the correct buffer data was
overwritten with stale memory (e.g., 00 18 27 xx instead of 37 7f 06 82).

Validation:
- Tested with limbo -v io_uring and WAL operations.
- Verified with xxd and wal-browser.

Signed-off-by: Daniel Boll <danielboll.academico@gmail.com>
This commit is contained in:
Daniel Boll
2025-03-19 23:55:43 -03:00
parent 38d2afc8dd
commit fc50609491

View File

@@ -1,4 +1,4 @@
use super::{common, Completion, File, OpenFlags, IO};
use super::{common, Completion, File, OpenFlags, WriteCompletion, IO};
use crate::{LimboError, Result};
use rustix::fs::{self, FlockOperation, OFlags};
use rustix::io_uring::iovec;
@@ -279,7 +279,14 @@ impl File for UringFile {
.build()
.user_data(io.ring.get_key())
};
io.ring.submit_entry(&write, c);
io.ring.submit_entry(
&write,
Completion::Write(WriteCompletion::new(Box::new(move |result| {
c.complete(result);
// NOTE: Explicitly reference buffer to ensure it lives until here
let _ = buffer.borrow();
}))),
);
Ok(())
}