Skip to content

Do not leave a FileLog table locked after a failed read - #123395

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-filelog-stream-slot-leak-123368
Oct 2, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-filelog-stream-slot-leak-123368

Conversation

@groeneai

@groeneai groeneai commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #123368

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed a FileLog table that stopped reading all of its files forever after a single read error (such as a line that failed to parse or a partially written last line): every later poll reported Another select query is running on this table and every direct SELECT failed with CANNOT_SELECT until the server was restarted. Related to #123368.

Description

On 26.8 (also 26.3), after one read of a FileLog table fails, the table never reads again: materialized views stop receiving rows and every SELECT fails with CANNOT_SELECT until the server restarts. This is what makes item 1 of #123368 permanent.

Cause: FileLogSource releases its reader slot (StorageFileLog::reduceStreams) only in close(), and its destructor called close() only if (!finished). That flag is ISource::finished, which ISource::work() also sets when generate() throws, so after any exception inside the source the slot was never released.

Change: FileLogSource keeps its own closed flag, and close() releases the slot with SCOPE_EXIT, so it is also released when storing the metadata throws.

After the fix a failed read behaves like an error raised later in the pipeline (for example by a materialized view's SELECT) already does on master: the rows read in that batch are not retried, and the table keeps reading. An incomplete last line is re-read on the next poll. Isolating one bad file from the others, and not raising an error for an incomplete last line at all, are separate PRs for #123368.

Validation: the new test fails on master with CANNOT_SELECT and passes 50/50 with randomized settings; the existing FileLog tests pass.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/123395]

Version info

  • Merged into: 26.10.1.1327-master (included in 26.10 and later)

FileLogSource takes a reader slot in its constructor (increaseStreams) and
releases it only in close(). The destructor called close() only if
ISource::finished was not set, but ISource::work() sets that flag when
generate() throws. So any exception inside the source (a line that does not
parse with the default handle_error_mode, a partially written last line, an
I/O error) left running_streams at 1 forever: every later streamToViews()
logged "Another select query is running on this table" and every direct
SELECT failed with CANNOT_SELECT until the server was restarted.

The source now keeps its own closed flag, and close() releases the slot with
SCOPE_EXIT, so it is also released when closeFilesAndStoreMeta() throws.
close() no longer writes ISource::finished; ISource::work() sets it after
generate() returns the empty chunk.

The leak dates from 7b1d463, which moved reduceStreams() from the
unconditional destructor into the finished-guarded path.

Related: ClickHouse#123368

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 2, 2026
@groeneai

groeneai commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding) plus my own review (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test appended to a directory-watched table without waiting for the watch to be armed, so the append could be missed (05316 .sh) AGREE, fixed the test now uses a single-file table, which re-checks the file on every read; second round: 0 findings
2 💡 The 0 line pins that rows of a failed batch are not retried AGREE, noted existing FileLog behavior for errors after the source; stated in the description
3 💡 With a partial last line, other files read by the same stream lose that poll's rows until the line completes AGREE, noted master stops the whole table instead; the partial-line PR removes the error

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261002-020400

@groeneai

groeneai commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05316_file_log_error_does_not_lock_table fails on master 1/1, and 10/10 with randomized settings: the read after the failed one gets CANNOT_SELECT.
b Root cause explained? FileLogSource released its reader slot only in close(), and its destructor skipped close() when ISource::finished was set, which ISource::work() does when generate() throws. So any error inside the source left running_streams at 1.
c Fix matches root cause? Yes. The source keeps its own closed flag and releases the slot with SCOPE_EXIT. No guard in the readers.
d Test intent preserved / new tests added? New shell test (no tags, parallel-safe). The existing FileLog tests pass unchanged.
e Both directions demonstrated? Master FAIL (CANNOT_SELECT), fix OK, 50/50 randomized. Reverting only the destructor condition makes it fail again.
f Fix is general across code paths? FileLogSource is the only place that takes and releases the slot, for both SELECT and materialized views (both checked). A throw while storing the metadata no longer leaks the slot either.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked: parse error, partially written last line (default and stream modes), two files read by two streams, and a materialized view. The fix is not type-dependent.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or metadata format change. Only the error path changes: the table keeps reading.
i Invariants and contracts preserved? Each increaseStreams() is matched by exactly one reduceStreams() on every path (normal end, exception, cancel, throw inside close()). In the materialized view path the exception-path cleanup still runs under file_infos_mutex, as before.

Session id: cron:clickhouse-impl-slot-6:20261002-004503

@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d1cc314]

Summary: ✅


AI Review

Summary

This PR fixes the FileLogSource cleanup path so a read exception no longer leaves StorageFileLog::running_streams permanently incremented, and it adds a focused stateless regression test for the direct-SELECT repro. I do not see a remaining correctness or concurrency issue in the current diff: the new closed flag correctly decouples cleanup from ISource::finished, and SCOPE_EXIT now releases the reader slot even when closeFilesAndStoreMeta throws.

Missing context / blind spots
  • ⚠️ Most full build and sanitizer jobs were still pending at review time; a green CI run would close the remaining validation gap for platform-specific regressions.
Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit d1cc314.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.80% 79.80% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). labels Oct 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing d1cc31447 with master c9c24473f (stripped binary size, per-symbol sizes and ThinLTO time; 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.

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

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 Oct 2, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 2, 2026
Merged via the queue into ClickHouse:master with commit eba45c1 Oct 2, 2026
149 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants