Skip to content

Fix Keeper crash after a failed changelog preallocation - #122337

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-keeper-changelog-enospc-crash
Sep 26, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-keeper-changelog-enospc-crash

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

When fallocate for a Keeper changelog file fails with ENOSPC, the batch fails and Changelog::appendCompletionThread called current_writer->finalize without holding writer_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 next ChangelogWriter::appendRecord then 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:

<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. It is removed, and the chassert in appendRecord is 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):

  • Critical Bug Fix (crash, data loss, RBAC)

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

  • Merged into: 26.10.1.717-master (included in 26.10 and later)
  • Backported to: 26.9.3.38, 26.8.14.3, 26.7.16.3, 26.3.35.3

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]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [07e188e]

Summary: ✅


AI Review

Summary

This PR removes the racy current_writer->finalize call after a failed Keeper changelog preallocation and adds a regression test for the ENOSPC path. The crash goes away, but the new "keep appending after flush() returned false" behavior breaks the changelog's durable-prefix contract: later successful flushes can be reported as durable even though the earlier failed entry never reached disk.

Findings

❌ Blockers

  • [src/Coordination/tests/gtest_coordination_changelog.cpp:371] The new post-failure contract is unsafe. appendEntry records the failed log in memory and advances max_log_id before the async writer ever tries to persist it (src/Coordination/Changelog.cpp:4305). If appendRecord returns false on the ENOSPC path (src/Coordination/Changelog.cpp:374), the batch is marked failed, but nothing rolls back that in-memory index. Because this patch removes the old "crash on next append" path, the next append can now succeed and flush_logs will advance last_durable_idx to the later flush.index (src/Coordination/Changelog.cpp:4206) even though the earlier index was never written. After an ENOSPC on index 1 followed by a successful append of index 2, the durable index becomes 2 while the on-disk file starts at 2, so index 1 is silently lost on restart. The fix needs to fail closed after the first append error: either latch the changelog into a permanent failure state that rejects later appends and flushes until shutdown, or roll entry_storage and max_log_id back to the last durable prefix before accepting more work.
Tests
  • Not run locally. There is no build* directory in this checkout, so I reviewed the patch statically only.
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 Report

Measured on commit 07e188e.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.70% -0.10%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 22/22 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Sep 25, 2026

auto entry = getLogEntry("hello world", 77);
changelog.append(entry);
EXPECT_FALSE(changelog.flush());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@clickhouse-gh clickhouse-gh Bot added the comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... label Sep 25, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 07e188ecc with master 0c2e8eefb (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of fb2810634; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

7 object files changed (+103.75 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/TimeSeries/PrometheusQueryToSQL/applyMinMaxOfFunction.cpp.o new 101.57 KiB +101.57 KiB

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

8 translation units recompiled, 16 s compile time in total, 8 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Sep 26, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 26, 2026
Merged via the queue into master with commit b227a2a Sep 26, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-keeper-changelog-enospc-crash branch September 26, 2026 05:02
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 26, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 26, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 26, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 26, 2026
Backport #122337 to 26.9: Fix Keeper crash after a failed changelog preallocation
alexey-milovidov added a commit that referenced this pull request Sep 27, 2026
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]>
robot-clickhouse-ci-1 added a commit that referenced this pull request Sep 27, 2026
Cherry pick #122337 to 26.7: Fix Keeper crash after a failed changelog preallocation
robot-clickhouse added a commit that referenced this pull request Sep 27, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Sep 27, 2026
Cherry pick #122337 to 26.8: Fix Keeper crash after a failed changelog preallocation
robot-clickhouse added a commit that referenced this pull request Sep 27, 2026
robot-clickhouse added a commit that referenced this pull request Sep 27, 2026
Cherry pick #122337 to 26.3: Fix Keeper crash after a failed changelog preallocation
robot-clickhouse added a commit that referenced this pull request Sep 27, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 27, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 28, 2026
Backport #122337 to 26.8: Fix Keeper crash after a failed changelog preallocation
clickhouse-gh Bot added a commit that referenced this pull request Sep 28, 2026
Backport #122337 to 26.7: Fix Keeper crash after a failed changelog preallocation
alexey-milovidov added a commit that referenced this pull request Sep 28, 2026
Backport #122337 to 26.3: Fix Keeper crash after a failed changelog preallocation
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 28, 2026
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]>
rorylshanks pushed a commit to rorylshanks/ClickHouse that referenced this pull request Sep 29, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants