Repository navigation
Fix Keeper crash after a failed changelog preallocation - #122337
Conversation
When `fallocate` for a changelog file failed with `ENOSPC`, the batch
failed and `Changelog::appendCompletionThread` called
`current_writer->finalize` without holding `writer_mutex`. Since the
preallocation was not done, this cancelled and destroyed the file buffers
while the write thread could still be using them, and left the writer
without a file. The next `ChangelogWriter::appendRecord` then
dereferenced the null (or already freed) buffer, so the server hit a
segmentation fault instead of stopping the server cleanly.
Seen on a server with a full disk:
<Fatal> Changelog: Failed to allocate enough space on disk for logs
<Fatal> RaftInstance: log appending failed, stop this server
...
Address: 0x8. Access: <not available>. Address not mapped to object.
2. DB::ChangelogWriter::appendRecord(DB::ChangelogRecord&&)
The `finalize` is not needed: `prealloc_done` stays false after the
failure, so the next append retries the preallocation, and the file is
finalized on shutdown. Remove it, and turn the `chassert` in
`appendRecord` into a proper check so that a writer without a file
produces an exception instead of a null pointer dereference.
Add the `keeper_changelog_preallocate_no_space` failpoint and a unit
test that reproduces the crash (5/5 runs abort without the fix).
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [07e188e] Summary: ✅
AI ReviewSummaryThis PR removes the racy Findings❌ Blockers
Tests
Final Verdict❌ Request changes — the crash fix as written can turn one failed append into a silent log hole that later successful flushes report as durable. LLVM Coverage ReportMeasured on commit 07e188e.
Changed lines: Changed C/C++ lines covered: 22/22 (100.00%) · Uncovered code |
|
|
||
| auto entry = getLogEntry("hello world", 77); | ||
| changelog.append(entry); | ||
| EXPECT_FALSE(changelog.flush()); |
There was a problem hiding this comment.
Continuing to append after the first flush() failure is not safe with the current bookkeeping. appendEntry has already inserted the failed log into entry_storage and advanced max_log_id before the writer hits the ENOSPC path (src/Coordination/Changelog.cpp:4305), and appendRecord returns false without rolling that back (src/Coordination/Changelog.cpp:374). With the old code the next append died immediately; after this change, the next append can succeed and flush_logs will move last_durable_idx forward to that later flush.index (src/Coordination/Changelog.cpp:4206) even though the earlier index never reached disk. A concrete trace is: fail preallocation on index 1, append index 2, flush() returns true, and now the durable index is 2 while the file on disk starts at 2, so index 1 is silently lost on restart. I think this needs to fail closed after the first append error (latch the changelog into a permanent failure state, or roll entry_storage/max_log_id back to the last durable prefix) instead of resuming later appends.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes7 object files changed (+103.75 KiB total), 1 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units8 translation units recompiled, 16 s compile time in total, 8 of them have a recent master baseline. |
Backport #122337 to 26.9: Fix Keeper crash after a failed changelog preallocation
Resolve the cherry-pick conflicts of #122337 against 26.7. The conflicts came from master's `Keeper` changelog read-ahead code (failpoints, profile events and tests) that is not on 26.7. Resolution applies only the fix itself: - register the `keeper_changelog_preallocate_no_space` failpoint; - add a `FailPoints` namespace and `Common/FailPoint.h` include to `Changelog.cpp`; - replace `chassert` in `appendRecord` with a `LOGICAL_ERROR` exception; - inject `ENOSPC` after `fallocate` via the failpoint; - stop finalizing the writer from `appendCompletionThread`; - add `ChangelogTestAppendAfterPreallocationFailure`, adapted to the 26.7 `KeeperLogStore` constructor (no `ReadAheadSettings`). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Cherry pick #122337 to 26.7: Fix Keeper crash after a failed changelog preallocation
Cherry pick #122337 to 26.8: Fix Keeper crash after a failed changelog preallocation
Cherry pick #122337 to 26.3: Fix Keeper crash after a failed changelog preallocation
Backport #122337 to 26.8: Fix Keeper crash after a failed changelog preallocation
Backport #122337 to 26.7: Fix Keeper crash after a failed changelog preallocation
Backport #122337 to 26.3: Fix Keeper crash after a failed changelog preallocation
Replace the one-name deny-list entry with the `enable_failpoints` BuzzHouse option from ClickHouse#94148 and set it to false in the BuzzHouse CI job, so CI runs no longer arm random fail points. BuzzHouse enables up to 10 random fail points from `system.fail_points` per run, and some of them take the server down by design. The latest one is `keeper_changelog_preallocate_no_space` (ClickHouse#122337): Keeper stops the server with `abort` at the next changelog rotation, which was reported as `Received signal 6 (STID: 3552-0de0)` on an unrelated PR: https://github.com/ClickHouse/ClickHouse/actions/runs/36298717968/job/108603160193 The option defaults to true, so a BuzzHouse config that does not set it still loads fail points. With it false, the list stays empty and the generator emits no fail point statements. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…il point `keeper_changelog_preallocate_no_space` (added by ClickHouse#122337) makes `ChangelogWriter::tryPreallocateForFile` fake `ENOSPC` when Keeper starts a new changelog file. The append then fails, NuRaft stops the server from `notify_log_append_completion` ("log appending failed, stop this server"), and `KeeperStateManager::system_exit` calls `abort`. Stopping the server is the intended outcome of a failed preallocation, and with the embedded Keeper it takes the whole clickhouse-server down with SIGABRT. BuzzHouse builds its list of fail points from `system.fail_points` and this one was not excluded, so a run that picked it ended at the next changelog rotation with `Received signal 6`, charged to the PR under test. Observed on `BuzzHouse (amd_debug)` of ClickHouse#103402, an access-rights change: https://github.com/ClickHouse/ClickHouse/actions/runs/36298717968/job/108603160193 The fuzzer enabled the fail point at 13:16:09; at 13:54:00 the next rotation logged `Failed to allocate enough space on disk for logs`, then `RaftInstance: log appending failed, stop this server`, then signal 6. The disk was never full. Exclude it alongside the other fail points that legitimately take the server down. It stays covered by the unit test `ChangelogTestAppendAfterPreallocationFailure`, which enables it in-process. Measured locally: the selection query returns 264 candidate names before and 263 after, the difference being exactly this name. On a master build with `rotate_log_storage_interval = 10`, enabling the fail point and writing to Keeper aborts the server at the first rotation with the CI job's stack. Co-Authored-By: Claude Opus 5.5 <[email protected]>
When
fallocatefor a Keeper changelog file fails withENOSPC, the batch fails andChangelog::appendCompletionThreadcalledcurrent_writer->finalizewithout holdingwriter_mutex. Because the preallocation was not done, this cancelled and destroyed the writer's file buffers while the write thread could still be using them, and left the writer without a file. The nextChangelogWriter::appendRecordthen dereferenced a null (or freed) buffer, so the server got a segmentation fault instead of stopping cleanly.Seen on a server with a full disk:
The
finalizeis not needed:prealloc_donestays false after the failure, so the next append retries the preallocation, and the file is finalized on shutdown. It is removed, and thechassertinappendRecordis turned into a proper check, so a writer without a file produces an exception instead of a null pointer dereference.A new failpoint,
keeper_changelog_preallocate_no_space, and a unit test reproduce the crash (5 of 5 runs abort without the fix).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a segmentation fault in Keeper when preallocating space for a changelog file fails because the disk is full.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122337]
Version info
26.10.1.717-master(included in26.10and later)26.9.3.38,26.8.14.3,26.7.16.3,26.3.35.3