Skip to content

Do not throw a logical error from the Pretty writing thread after the query has finished - #125028

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-pretty-writer-expired-process-list-element
Oct 10, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-pretty-writer-expired-process-list-element

Conversation

@groeneai

@groeneai groeneai commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #117005

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

Description

05238_pretty_squash_broken_pipe (added by #117005) fails in Stateless tests (amd_debug, parallel) with Unexpected exit code 134: #125004, #112650. The clickhouse-local core dump of the first job aborts in Context::getProcessListElement (LOGICAL_ERROR "Weak pointer to process_list_elem expired"), called from PrettyBlockOutputFormat::writingThread.

In clickhouse-local the output format outlives the query. When the main thread notices the background write error while it polls, it cancels and finishes the query, which destroys its process list entry, and only then destroys the format and joins the writing thread. If that thread has not reached its cancel call yet, the strict getter throws. The thread now uses getProcessListElementSafe: an expired element means the query has already finished, and the stored write error still reaches the user (exit code 75).

Not for changelog: #117005 is not in a release.

Validation (debug build) with a temporary 3 s sleep before the cancel call, on the test's SELECT DISTINCT query: master aborts 10/10, this PR exits 75 10/10 (also 10/10 on the first subtest's query). Without the sleep: 05238_pretty_squash_broken_pipe and 05061_pretty_squash_flush 50/50 each with randomized settings.

🤖 Generated with Claude Code


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

Version info

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

… query has finished

When the background writer of squashed Pretty blocks fails to write (for
example, the reader of the output pipe has exited), it stores the error and
then cancels the query through its process list element. In
`clickhouse-local` the output format is owned by the client and outlives the
query: if the main thread notices the stored error first while polling
(`checkBackgroundError`), it cancels, drains and finishes the query, which
destroys the process list entry, and only then destroys the format and joins
the writer. The writer then called the strict `getProcessListElement`, which
throws `LOGICAL_ERROR` ("Weak pointer to process_list_elem expired") for an
expired element, aborting debug and sanitizer builds (exit code 134 in
`05238_pretty_squash_broken_pipe`).

Use `getProcessListElementSafe`: an expired element means the query has
already finished, so there is nothing left to cancel, and the stored error
still reaches the client through the main thread.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 10, 2026
@groeneai

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

Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 No deterministic regression test: 05238_pretty_squash_broken_pipe reaches the window only under load AGREE, noted, not blocking A deterministic test would need a failpoint that only widens a timing window. The fix is proven with a temporary sleep in the window: master aborts 10/10, this PR exits 75 10/10 on both local subtest queries.

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

Session id: cron:clickhouse-review-slot-8:20261010-021900

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, with a temporary 3 s sleep after lock.unlock() in writingThread's catch: the test's SELECT DISTINCT number % 100000 ... query in clickhouse-local aborts 10/10 on master (exit 134, getProcessListElement <- writingThread frames).
b Root cause explained? The main thread notices the background write error in checkBackgroundError, cancels and finishes the query (LocalConnection::finishQuery destroys the process list entry), then destroys the format and joins the writer; the writer's strict getProcessListElement throws LOGICAL_ERROR on the expired weak pointer, which aborts debug builds.
c Fix matches root cause? Yes: getProcessListElementSafe is the existing getter for threads that can outlive the entry; an expired element means the query has finished, so there is nothing to cancel.
d Test intent preserved / new tests added? Test unchanged; 05238_pretty_squash_broken_pipe covers the site. A deterministic test would need a failpoint that only widens a timing window.
e Both directions demonstrated? Master + sleep: 10/10 abort. This PR + sleep: 10/10 exit 75 on both local subtest queries. Without the sleep: 05238 and 05061 50/50 each, randomized settings.
f Fix is general across code paths? Yes: it is the only process-list access from a thread that can outlive the query in src/Processors/Formats; the server pipeline destroys the format before the entry, the remote client has no element.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (no data-dependent logic); all Pretty* formats share this one writingThread.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or protocol change.
i Invariants and contracts preserved? Yes: the stored error is still set under writing_mutex and rethrown by the main thread (exit 75); the returned shared_ptr keeps QueryStatus alive across cancelQuery.

Session id: cron:clickhouse-impl-slot-4:20261010-015700

@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b6242e5]

Summary: ✅


AI Review

Summary

This PR replaces the strict getProcessListElement lookup with getProcessListElementSafe in PrettyBlockOutputFormat::writingThread, which matches the real lifetime contract in clickhouse-local: the background writer can outlive the query's process-list entry after the main thread has already observed the stored write error, cancelled the query, and finished it. I did not find a remaining correctness, concurrency, or compatibility issue in the changed path; the original background write exception is still stored and surfaced to the client, and the current PR CI is all green.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 10, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Oct 10, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 10, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 10, 2026
Merged via the queue into ClickHouse:master with commit 9bbd2b7 Oct 10, 2026
163 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 10, 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-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-not-for-changelog This PR should not be mentioned in the changelog 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