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