From fc506094915bf02b93f940239efcb063953dd2f6 Mon Sep 17 00:00:00 2001 From: Daniel Boll Date: Wed, 19 Mar 2025 23:55:43 -0300 Subject: [PATCH] Fix io_uring WAL write corruption by ensuring buffer lifetime Ensure the Arc> 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 --- core/io/io_uring.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/io/io_uring.rs b/core/io/io_uring.rs index f98f6f36f..cca473790 100644 --- a/core/io/io_uring.rs +++ b/core/io/io_uring.rs @@ -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(()) }