Repository navigation
Check close() before publishing temp files with rename() in AOF manifest, CONFIG, ACL and replica RDB load - #4594
KewangZhili wants to merge 4 commits into
Conversation
writeAofManifestFile() writes the manifest to a temp file and renames it over the real one, but it renames while the temp file's descriptor is still open and only closes it afterwards in the cleanup block, discarding the result. A write error the kernel deferred is not reported by write() or fsync(), only by close(). Because close() runs after the rename and its return value is thrown away, a short write can be published as the live manifest. aofLoadManifestFromFile() refuses to start on a corrupt manifest, so the node would fail to load its AOF on the next start. The manifest is the AOF analogue of nodes.conf: small, rewritten on every AOF state change, and fatal to the load path when corrupt. Close the descriptor before the rename and check the result, so the temp file is only published once it is known to be complete. rdbSaveInternal() and rewriteAppendOnlyFile() already do this. Signed-off-by: Himangshu Deka <[email protected]>
rewriteConfigOverwriteFile() renames the temp config over the real one while the temp file's descriptor is still open, and closes it afterwards in the cleanup block, discarding the result. A write error the kernel deferred is reported only by close(), so a truncated config file can be published. Close the descriptor before the rename and check the result. The fchmod() call needs the descriptor, so the close goes between it and the rename, which turns the fsync/fchmod/rename/fsyncFileDir else-if chain into guard clauses. The short-circuiting behaviour is unchanged: the first failure logs, leaves retval at -1 and reaches the cleanup label, which unlinks the temp file. The cleanup close() is now guarded so the descriptor is not closed twice, and close_errno is placed in errno so the caller still sees a meaningful error, as the function contract promises. Signed-off-by: Himangshu Deka <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review. 📝 WalkthroughWalkthroughTemporary ACL, AOF manifest, configuration, and replication RDB files now check ChangesAtomic file publication
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to ACL file saves now avoid publishing a temporary file when closing it reports a deferred write error, preserving the existing ACL file instead. No merge-blocking risk remains. Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| * published, or a truncated file replaces a good one. */ | ||
| if (close(fd) == -1) { | ||
| fd = -1; | ||
| serverLog(LL_WARNING, "Closing temp ACL file for ACL SAVE: %s", strerror(errno)); |
There was a problem hiding this comment.
the message is stating an action ("closing"), while it should be stating that the action failed.
Use something similar to the message below:
serverLog(LL_WARNING, "Failed to close temp ACL file for ACL SAVE. %s.", strerror(errno));There was a problem hiding this comment.
Fixed in 23c175f, used your wording — thanks!
ACLSaveToFile() already closes the temp file before renaming it into place, but discards what close() returns. A write error the kernel deferred is not reported by write() or fsync(), only by close(), so a short write can still be published as the live ACL file. Check the result and abort the save instead, leaving the previous ACL file in place. Signed-off-by: Himangshu Deka <[email protected]>
replicaLoadPrimaryRDBFromDisk() renames the fully received temp RDB over the persistence file while the transfer descriptor is still open, and closes it afterwards, only on the success path. A write error the kernel deferred is reported by close() and by nothing before it, so a short write could be published as the node's RDB. Close the descriptor before the rename and check the result. rdbLoad() reopens the file by name, so the descriptor is not needed past that point. The descriptor cannot simply be closed and set to -1, because cleanupTransferResources() asserts that repl_transfer_fd and repl_transfer_tmpfile are unset together, and every error path here reaches it through cancelReplicationHandshake(). Clearing only the descriptor makes that assertion fail and takes the replica down. Take ownership of the filename locally instead and clear both fields at once, then unlink and free the temp file on the paths that previously relied on cleanupTransferResources() to do it. The added test obstructs the rename by turning the replica's dbfilename into a directory, which reaches that teardown with the descriptor already closed. It passes before this change and with it, and fails with an assertion if the descriptor is cleared without the filename. Signed-off-by: Himangshu Deka <[email protected]>
439e7cf to
23c175f
Compare
|
Sorry for the long wait on this one! Are you still interested in working on this PR? We hope to review it in the next couple of weeks. If you are, please take care of these first:
🤖 Comment by Robo-Rain, for my human |
Follow-up to #4489, opened at @enjoy-binbin's suggestion there: he asked whether RDB/AOF/CONFIG/ACL share the close-before-rename problem being fixed for
nodes.conf. They do.The problem
A write error the kernel deferred is not reported by
write()orfsync()— only byclose(). Publishing a temp file withrename()before checkingclose()can therefore install a truncated file over a good one.Audit of every rename() in src/
rename()rdb.c:1690→ rename1743aof.c:2555→ rename2563aof.c:567config.c:1817replication.c:2620acl.c:2788→ rename2792RDB save and AOF rewrite already do it right and are the precedent followed here. One commit per subsystem.
Impact
nodes.conf: small, rewritten on every AOF state change, andaofLoadManifestFromFile()refuses to start on a corrupt manifest.Notes on each change
AOF manifest — close and check between the
fsync()and therename(). The error path leaves the temp manifest behind, matching the existingfsync()-failure path.CONFIG REWRITE —
fchmod()needs the descriptor, so the close goes between it and the rename. That turns thefsync/fchmod/rename/fsyncFileDirelse-if chain into guard clauses. Short-circuiting is unchanged: the first failure logs, leavesretvalat-1, and reachescleanup:, which unlinks the temp. The cleanupclose()is now guarded against a double close, andclose_errnois placed inerrnoso the caller still gets a meaningful error, as the function contract promises.ACL — check the existing
close().Replica RDB load — this one needs care. The descriptor cannot simply be closed and set to
-1, becausecleanupTransferResources()asserts thatrepl_transfer_fdandrepl_transfer_tmpfileare unset together:Every error path in
replicaLoadPrimaryRDBFromDisk()reaches that function viacancelReplicationHandshake(), so clearing only the descriptor makes the assertion fail and takes the replica down. I confirmed that by building it: the replica dies with=== ASSERTION FAILED ===.So the filename is taken into a local instead and both fields are cleared at once, with the temp file unlinked and freed on the paths that previously relied on
cleanupTransferResources()to do it.rdbLoad()reopens the file by name, so nothing needs the descriptor after the close.Every exit leaves the pairing intact:
repl_transfer_fdrepl_transfer_tmpfilefsyncfails (before the change)closefails-1NULL, freedrenamefails-1NULL, freedfsyncFileDir/rdbLoadfail-1NULL, freed-1NULL, freedTests
Replica survives a full sync whose RDB rename failsobstructs the rename by turning the replica'sdbfilenameinto a directory, givingEISDIR, which reaches the teardown above with the descriptor already closed. It asserts the replica is still answering, that a retry then syncs, and that no temp RDB is left behind.Its value is guarding the pairing, so I checked it actually does that:
fdopen + filename set is self-consistent)=== ASSERTION FAILED ===1148 tests pass across
unit/acl,unit/acl-v2,unit/introspection,unit/introspection-2,unit/other,unit/auth,unit/info,unit/info-command,unit/commandlog,integration/aof,integration/aof-multi-part,integration/rdb,integration/replication{,-2,-3,-4,-psync,-buffer},integration/dual-channel-replication,unit/cluster/diskless-load-swapdb, and the cluster suites that callCONFIG REWRITE.No test covers a failing
close()itself: it cannot be forced from Tcl, the same limitation noted in #4489. No existing test asserts on these failure messages, so the CONFIG restructure changes no expectations. On-disk formats are unchanged.Still not addressed: open handles across a rename
Separate from the integrity bug above, two places deliberately hold a handle across a rename.
replication.copens the rename destination on purpose sobioCreateCloseJob()can drop the old inode off the main thread, andaof.c:2828renames the temp incr AOF whileserver.aof_fdis still appending to it. Both are correct on POSIX and are not integrity bugs, but they are why AOF and replica full-sync still fail on SMB-backed volumes. Neither #4489 nor this PR makes Valkey generally SMB-safe.