Skip to content

Fix close an unopened AOF file during shutdown - #15827

Merged
ShooterIT merged 4 commits into
redis:unstablefrom
vitahlin:fix-close-aof-wait-write
Sep 24, 2026
Merged

ShooterIT merged 4 commits into
redis:unstablefrom
vitahlin:fix-close-aof-wait-write

Conversation

@vitahlin

@vitahlin vitahlin commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Issue

stopAppendOnly() unconditionally called fsync() and close() on server.aof_fd. When AOF was in AOF_WAIT_REWRITE, the initial rewrite could be scheduled but not started because another background operation was still running. In that state, no AOF file had been opened and server.aof_fd was -1.

Failed daily CI: https://github.com/redis/redis/actions/runs/35547037033/job/106174688222

The unconditional close(server.aof_fd) was not introduced by PR #15665. #15665 introduced the aof_cmd_duration feature and its test, which exercised this AOF lifecycle state.

The CI failure occurred when the test disabled AOF before the scheduled rewrite had actually started. The test helper waited only for aof_rewrite_in_progress and returned while the rewrite was still scheduled, exposing the existing close(-1) bug during cleanup.

Change

  1. Check server.aof_fd before flushing, fsyncing, and closing the AOF file.
  2. Update waitForBgrewriteaof to wait until both aof_rewrite_in_progress and aof_rewrite_scheduled are cleared, preventing the test from disabling AOF while a rewrite is still pending.
  3. Add a test for the close(-1) case.

Test

The waitForBgrewriteaof method is used in many places. I runned the fully daily CI: https://github.com/vitahlin/redis/actions/runs/35679811939


Note

Medium Risk
Touches runtime AOF disable path in stopAppendOnly(), which is safety-sensitive but the change is a narrow guard around fd I/O; broader test helper behavior may affect any suite that waits on AOF rewrites.

Overview
Fixes a crash/invalid close(-1) when appendonly is turned off at runtime while AOF is enabled but the first background rewrite is only scheduled (e.g. blocked by an in-flight BGSAVE), so server.aof_fd is still -1.

stopAppendOnly() now runs flush, fsync, and close only when a file descriptor is actually open; other shutdown steps (state reset, killing the rewrite child, etc.) are unchanged.

Test support is tightened so waitForBgrewriteaof waits until both aof_rewrite_in_progress and aof_rewrite_scheduled are clear, avoiding tests that disable AOF while a rewrite is still pending. A new aofrw test covers enabling AOF under a slow BGSAVE, then disabling AOF before the initial rewrite starts.

Reviewed by Cursor Bugbot for commit 42a6ebc. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 7964542. Configure here.

Comment thread tests/support/util.tcl

# A rewrite can be scheduled but paused while another background task is running,
# so we need to check two conditions
if {$rewrite_in_progress eq 1 || $rewrite_scheduled eq 1} {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite wait samples flags non-atomically

Low Severity

waitForBgrewriteaof reads aof_rewrite_in_progress and aof_rewrite_scheduled via two separate INFO calls, in-progress first. If a scheduled rewrite starts between those queries, both flags can look cleared and the helper returns while the rewrite is still running.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 7964542. Configure here.

@codecov

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 77.24%. Comparing base (0d6266f) to head (42a6ebc).
⚠️ Report is 2 commits behind head on unstable.

Files with missing lines Patch % Lines
src/aof.c 83.33% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable   #15827      +/-   ##
============================================
- Coverage     77.63%   77.24%   -0.39%     
============================================
  Files           146      146              
  Lines         87994    87995       +1     
============================================
- Hits          68314    67974     -340     
- Misses        19680    20021     +341     
Files with missing lines Coverage Δ
src/aof.c 82.99% <83.33%> (-0.05%) ⬇️

... and 21 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sundb
sundb requested a review from yinon-bit September 22, 2026 03:56
@sundb

sundb commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

The unconditional close(server.aof_fd) was not introduced by PR #15665. #15665 introduced the aof_cmd_duration feature and its test, which exercised this AOF lifecycle state.

I saw that close(server.aof_fd) also exist before #15665, am i missing something?

@vitahlin

Copy link
Copy Markdown
Contributor Author

close(server.aof_fd) also exist before #15665

Yes, the PR15665 just added some tests, and Valgrind detected this close(-1) issue.

@sundb

sundb commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

close(server.aof_fd) also exist before #15665

Yes, the PR15665 just added some tests, and Valgrind detected this close(-1) issue.

ok, so it means that this bug exist for a long time, can you check it in the old version?

@vitahlin

Copy link
Copy Markdown
Contributor Author

close(server.aof_fd) also exist before #15665

Yes, the PR15665 just added some tests, and Valgrind detected this close(-1) issue.

ok, so it means that this bug exist for a long time, can you check it in the old version?

I think PR #9788 changed when the AOF file is opened but did not update the close path accordingly.

Location Before #9788 After #9788
startAppendOnly() Opened the AOF file before enabling AOF and assigned a valid aof_fd, even when the rewrite was postponed. Removed the open() call, allowing AOF to enter AOF_WAIT_REWRITE without an open file.
Opening the AOF file Opened immediately when AOF was enabled. Deferred until the rewrite actually started, through openNewIncrAofForAppend().
stopAppendOnly() Unconditionally called fsync and close. Still unconditionally called fsync and close.

@sundb
sundb removed the request for review from yinon-bit September 22, 2026 07:54

@ShooterIT ShooterIT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch.

Comment thread src/aof.c Resolved
Co-authored-by: Yuan Wang <[email protected]>
@ShooterIT
ShooterIT merged commit 41e6a37 into redis:unstable Sep 24, 2026
19 checks passed
@vitahlin
vitahlin deleted the fix-close-aof-wait-write branch September 24, 2026 10:08
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