Skip to content

Conversation

@jussisaurio
Copy link
Contributor

Currently in logger.rs we are not issuing any fsyncs at all. We do flush() in write_header() but this is a no-op on Unix because there are no userspace buffers to flush since write_all_at() writes directly to kernel.

Not fsyncing can cause the following bug:

  • We write X frames
  • We write wallog header (containing frame count X)
  • Kernel reorders writes so that header has been written out but not all of the frame data
  • Crash
  • Sqld starts up, reads wallog header which claims X frames exist in wallog, but only Y frames (Y < X) have been written
  • short read in read_frame_byte_offset_mut() returns an error and crashes the server

It's also important to sync after writing frames but before writing the header so that there is no write reordering scenario where the header is persistent before the frame data is.

Currently in logger.rs we are not issuing any fsyncs at all.
We do `flush()` in `write_header()` but this is a no-op on Unix
because there are no userspace buffers to flush since `write_all_at()`
writes directly to kernel.

Not fsyncing can cause the following bug:

- We write X frames
- We write wallog header (containing frame count X)
- Kernel reorders writes so that header has been written out
  but not all of the frame data
- Crash
- Sqld starts up, reads wallog header which claims X frames
  exist in wallog, but only Y frames (Y < X) have been written
- short read in read_frame_byte_offset_mut() returns an error
  and crashes the server

It's also important to sync after writing frames but before
writing the header so that there is no write reordering scenario
where the header is persistent before the frame data is.
@jussisaurio jussisaurio requested a review from sivukhin December 19, 2025 09:01
@jussisaurio jussisaurio changed the title Fsync frame data in wallog before writing header server/replication: fsync frame data in wallog before writing header Dec 19, 2025
@jussisaurio jussisaurio added this pull request to the merge queue Dec 19, 2025
Merged via the queue into main with commit ac9c733 Dec 19, 2025
19 checks passed
@jussisaurio jussisaurio deleted the fsync-frames-before-writing-header branch December 19, 2025 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants