Skip to content

Tolerate non-UTF-8 bytes when the integration harness decodes captured output - #118195

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:integration-harness-non-utf8-captured-output
Sep 13, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:integration-harness-non-utf8-captured-output

Conversation

@groeneai

@groeneai groeneai commented Sep 5, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #117974

One non-UTF-8 byte in docker.log aborts integration-test teardown and takes the sanitizer scan down with it.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Integration-test harness: tolerate non-UTF-8 bytes when decoding captured container output, so a single bad byte no longer aborts cluster teardown and the sanitizer scan, and honor shutdown(ignore_sanitizer=True) in the docker.log fallback scan.

Description

Observed. test_unknown_config_option/test.py::test_graphite_default_rule_type_not_all_rejected passed, then failed in teardown with UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8a in position 1655 at helpers/cluster.py:4460 (CI report).

Root cause: a silently reverted fix. #74334 fixed this same UnicodeDecodeError in shutdown by adding errors="replace" to the single handle used for both directions. 3deae2dc5d90f6d0 (2025-02-14) then split it: capture moved into save_logs, and the scan got its own open(..., "r") with no errors=. The keyword travelled with the write half, where a subprocess writes to the raw descriptor and the handler never runs, so only the side that mattered lost it.

Cost. That statement is the fallback sanitizer detector and nothing catches it, so teardown also abandons cleanup. Raw sanitizer output is itself a prime source of non-UTF-8 bytes, so the scan is likeliest to die when it has something to find.

Scope. Two commits. The first restores tolerance at four sites, chosen by one rule: the decoded text only reaches a message or an ASCII-marker scan. Sites where the decoded value is the caller's datum, such as get_container_logs, stay strict, since tolerating bad bytes there could mask a defect. The second closes an adjacent gap raised in review: of the three sanitizer scans in shutdown, only the docker.log fallback was not gated on not ignore_sanitizer, so it could raise even when the caller asked to ignore reports. That gap predates this branch (291dd76d7739487).

Validation. Each decode site was exercised both directions over a 0x8a fixture; reverting any one edit reddens only its own arm. Driving the real shutdown over five fixtures with ignore_sanitizer=True: master raises on 4 of 5, the decode fix alone on 3 of 5, with the guard 0 of 5. With ignore_sanitizer=False all three sign-bearing fixtures are still detected, so no signal is lost. CIDB: one occurrence in 90 days, none on master.


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

Version info

  • Merged into: 26.9.1.1356-master (included in 26.9 and later)
  • Backported to: 26.8.25.12, 26.7.28.9

…d output

`docker.log` is a multiplexed capture of every container's stdout and stderr, so
non-UTF-8 content is expected by construction: sanitizer reports, binary noise
and truncated multi-byte sequences all land there. One such byte was enough to
abort integration-test teardown:

    helpers/cluster.py:4460 in shutdown
        for line in f:
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8a in position 1655

This is a split-handle regression. Before `3deae2dc5d90f6d0` (2025-02-14),
`shutdown` used a single handle for both directions: it opened
`docker_logs_path` as `"w+"` with `errors="replace"`, wrote the captured logs
through it, then rewound and iterated that same `TextIOWrapper`, so the read
carried the error handler. That commit moved the capture into `save_logs` as a
`Popen` writing to a tolerant handle, and gave the scan its own
`open(..., "r")` with no `errors=`. Because a subprocess writes to the raw file
descriptor, the write-side handler never runs on those bytes: the tolerance that
mattered was on the read side, and only the read side lost it.
`c5db8d74126cec70` (2025-02-17) then relocated that reader, which is why
`git blame` points there.

The statement that raises is the fallback sanitizer detector, reached only when
the per-instance `stderr.log` scan found nothing, and nothing catches it. So
teardown abandons `cleanup`, the `docker_net_lock` release, the Keeper
`stderr.log` scan, the raise for a sanitizer report, and the raise carrying the
`<Fatal>` and `LOGICAL_ERROR` findings gathered earlier in the same function. A
genuine server abort is then replaced in the report by a decode error, and since
raw sanitizer output is itself a prime source of non-UTF-8 bytes, this scan is
likeliest to die exactly when it has something to find.

Four sites change, chosen by one rule: the decoded text is only interpolated
into a message or scanned for an ASCII marker, so a strict decode there has no
upside and can only turn a diagnostic into a decode error.

  - `shutdown`, the `docker.log` sanitizer scan.
  - `run_and_check`, which re-decoded `res.stderr` strictly for its exception
    message although `err` above it already holds those same bytes decoded
    tolerantly. It now reuses `err`, which removes a decode rather than adding
    tolerance.
  - `wait_for_start`, both startup-failure messages, which interpolate
    `handle.logs` and so died on the very output they exist to report.

Sites where the decoded value is the caller's own datum are deliberately left
strict, because tolerating bad bytes there could substitute U+FFFD for content a
test legitimately checks, and so mask a real defect. `errors="replace"` is
already the idiom for this in the same function, at the Keeper `stderr.log` scan.

Verified both directions at each site, with the statements taken from the tree
rather than retyped, and by driving `shutdown` over a dirty `docker.log` that
contains a sanitizer report: it now raises `Sanitizer assert found for instance
node3`, where that report was previously lost. Reverting any one of the four
edits reddens only its own arm, and behaviour on clean input is unchanged.

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect labels Sep 5, 2026
@groeneai

groeneai commented Sep 5, 2026

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

An independent cold review plus two adversarial gate rounds on 539939f8. No blocker or major survived.

⚠️ Verification ask: show that all four sites tolerate invalid UTF-8 while preserving their diagnostics, since the existing tests pass either way. Measured rather than committed as tests, per the repository note that CI-script changes need a throwaway test but not a committed one, and per #74334, the earlier fix to this same function, which was one file +2/-1 with no test:

  • All four sites, both directions, over a 0x8a fixture and over clean bytes: 15/15 arms. On clean input the result is byte-identical to base, and the line count is preserved.
  • Each edit reverted on its own reddens exactly one arm, its own: 4/4.
  • The real ClickHouseCluster.shutdown, over a dirty docker.log containing a sanitizer report: the base arm loses the report to UnicodeDecodeError, the fixed arm raises Sanitizer assert found for instance node3. cleanup at :4491 is unguarded and precedes that raise with no intervening branch, so reaching the raise is itself proof that teardown ran to completion.

💡 Noted, not blocking. The commit message names wait_for_start for the two sites that are really in its callee wait_until_port_is_ready; the rest of that bullet is accurate and the diff is unambiguous. Separately, the errors="replace" from #74334 is still present at :4375 on the write handle, where a subprocess writing to the raw file descriptor leaves it inert; it is left in place rather than moved, since the note there is the original author's.

@clickhouse-gh

clickhouse-gh Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [04efc5e]

Summary: ✅


AI Review

Summary

This PR hardens the integration-test harness against non-UTF-8 bytes in diagnostic-only decode paths and fixes the remaining shutdown(ignore_sanitizer=True) gap in the docker.log fallback sanitizer scan. After reviewing the current diff, surrounding code, prior discussion, and the current PR head, I did not find any remaining blocker or major issue.

Final Verdict

✅ No new findings.

LLVM Coverage Report

Measured on commit 04efc5e.

Metric Baseline Current Δ
Lines 88.80% 88.70% -0.10%
Functions 92.20% 92.10% -0.10%
Branches 81.10% 81.10% +0.00%

Changed lines: Uncovered code analysis did not run: No coverable C/C++ source files changed (contrib/ is excluded from coverage).

Newly covered: +822 lines in 230 files (-1033 lines lost coverage) · Details

Full report

@groeneai

groeneai commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator Author

cc @mstetsyuk, could you review this? The docker.log sanitizer scan in ClickHouseCluster.shutdown opens the file with no errors= handler, so one non-UTF-8 byte raises UnicodeDecodeError and teardown abandons sanitizer detection, cleanup and the network-lock release. #74334 had fixed exactly this, and a later refactor moved the handler onto the write half, where a subprocess writing to the raw file descriptor leaves it inert.

Comment thread tests/integration/helpers/cluster.py Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Sep 5, 2026
`ClickHouseCluster.shutdown` has three sanitizer-detection paths and only two of
them respect its `ignore_sanitizer` argument. The per-instance `stderr.log` scan
and the Keeper `stderr.log` scan are both gated on `not ignore_sanitizer`; the
`docker.log` fallback sitting between them is unconditional, so it can set
`sanitizer_assert_instance` and trip the unconditional raise at the end of
`shutdown` even when the caller asked for sanitizer reports to be ignored.

Two finalizers pass that argument deliberately and rely on it:
tests/integration/test_keeper_sanitizer_logs/test.py:19, which injects a
sanitizer marker on purpose, and tests/integration/test_crash_log/test.py:36.

The gap predates this branch. It dates to 291dd76, which introduced
`ignore_sanitizer` and gated the per-instance scan only.

Measured by driving the real `shutdown` over five `docker.log` fixtures with
`ignore_sanitizer=True`: on master 4 of the 5 raise, and the clean-bytes fixture
raises `Sanitizer assert found` despite the caller asking to ignore it. The
tolerant decode from the previous commit lowers that to 3 of 5, so it narrows
the gap rather than widening it, but does not close it. With this guard all 5
complete. With `ignore_sanitizer=False` all three sign-bearing fixtures are
still detected and the two sign-free ones still raise nothing, so no sanitizer
signal is lost.

Co-Authored-By: Claude Opus 5 <[email protected]>
@alexey-milovidov alexey-milovidov self-assigned this Sep 13, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 13, 2026
Merged via the queue into ClickHouse:master with commit e35c60d Sep 13, 2026
174 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 13, 2026
@PedroTadim

Copy link
Copy Markdown
Member

Backporting to fix CI

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 12, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 12, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 12, 2026
Backport #118195 to 26.7: Tolerate non-UTF-8 bytes when the integration harness decodes captured output
clickhouse-gh Bot added a commit that referenced this pull request Oct 12, 2026
Backport #118195 to 26.8: Tolerate non-UTF-8 bytes when the integration harness decodes captured output
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 12, 2026
… integration harness decodes captured output
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-testing Test content only (no production code). groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-ci 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 v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants