From f30a6645a93a4f84d796c091e135892d3ddc2f1d Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Mon, 30 Nov 2026 08:19:59 +0000 Subject: [PATCH] Don't let WAITAOF ack AOF data that has not been fsynced When the AOF buffer is empty, flushAppendOnlyFile() publishes primary_repl_offset as fsynced so that WAITAOF doesn't hang on offsets that only reached replicas (RM_Call with NO_AOF). It did that whenever no fsync was visibly in progress, without checking that the AOF had been fsynced at all. Under appendfsync everysec a write that lands between two fsyncs was acknowledged by WAITAOF 1 0 on the next event loop iteration, up to a second before it reached the disk. The same happened while a background fsync was still running, since aof_last_incr_fsync_offset advances when the fsync is requested, and while the fsync of the previous INCR file queued by an AOF rewrite was still running. Only publish the offset when everything written has had an fsync requested and no AOF fsync job is pending. The jobs publish the offset themselves when they complete. Signed-off-by: Madelyn Olson --- src/aof.c | 7 +++++-- tests/unit/wait.tcl | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/aof.c b/src/aof.c index a995d2ab5e8..618c2306e11 100644 --- a/src/aof.c +++ b/src/aof.c @@ -1208,8 +1208,11 @@ void flushAppendOnlyFile(int force) { * This is needed to avoid a WAITAOF hang in case a module used RM_Call with the NO_AOF flag, * in which case primary_repl_offset will increase but fsynced_reploff_pending won't be updated * (because there's no reason, from the AOF POV, to call fsync) and then WAITAOF may wait on - * the higher offset (which contains data that was only propagated to replicas, and not to AOF) */ - if (!sync_in_progress && server.aof_fsync != AOF_FSYNC_NO) + * the higher offset (which contains data that was only propagated to replicas, and not to AOF). + * aof_last_incr_fsync_offset advances when an fsync is requested, not when it completes, so + * also wait for the background fsync jobs, which publish the offset when the fsync succeeds. */ + if (server.aof_fsync != AOF_FSYNC_NO && server.aof_last_incr_fsync_offset == server.aof_last_incr_size && + !aofFsyncInProgress() && !bioPendingJobsOfType(BIO_CLOSE_AOF)) atomic_store_explicit(&server.fsynced_reploff_pending, server.primary_repl_offset, memory_order_relaxed); return; diff --git a/tests/unit/wait.tcl b/tests/unit/wait.tcl index caea1de65df..bcf4ae9c289 100644 --- a/tests/unit/wait.tcl +++ b/tests/unit/wait.tcl @@ -175,6 +175,30 @@ tags {"wait aof network external:skip"} { assert_equal [$master waitaof 1 0 0] {1 0} } + test {WAITAOF local copy everysec does not ack a write that was not fsynced} { + # Fsync now, so the next write lands inside the one second window of everysec. + test_config_set r appendfsync always + $master incr foo + $master config set appendfsync everysec + + # With no-appendfsync-on-rewrite and a child process, the write reaches the + # AOF but no fsync happens until the child exits. + test_config_set r no-appendfsync-on-rewrite yes + test_config_set r rdb-key-save-delay 100000000 + $master bgsave + test_defer { + catch {exec kill -9 [get_child_pid 0]} + waitForBgsave r + } + assert_equal [s rdb_bgsave_in_progress] 1 + $master incr foo + assert_equal [$master waitaof 1 0 100] {0 0} ;# exits on timeout + + catch {exec kill -9 [get_child_pid 0]} + waitForBgsave r + assert_equal [$master waitaof 1 0 10000] {1 0} + } + test {WAITAOF local copy with appendfsync always} { r config set appendfsync always $master incr foo