Skip to content

Stop test 01658 from leaking files into user_files_path, fix 02843 flaky - #105602

Merged
azat merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-02843-context-has-expired-flaky-leak
Jun 11, 2026
Merged

azat merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-02843-context-has-expired-flaky-leak

Conversation

@groeneai

@groeneai groeneai commented May 21, 2026 •

Copy link
Copy Markdown
Collaborator

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 to CHANGELOG.md):

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Why

02843_context_has_expired.sql fails sporadically on CI with:

The query succeeded but the server error '107' was expected
(query: SELECT 1 in (SELECT file(materialize('a'))); -- { serverError FILE_DOESNT_EXIST })

CIDB shows ~4 hits in the past 90 days, all matching the same pattern, on unrelated PRs (e.g. #100651, #101798, #104697). Crucially, the diagnostic always reports "Test also fails without randomized settings (not a randomization issue)" — so this is not a randomized-settings flake, the server is in a stuck state.

Root cause

01658_read_file_to_stringcolumn.sh writes the short filenames a, b, c into user_files_path (line 88 in the current script) and removes them only via the manual rm at the bottom. The script runs under set -e. Any failing query between the write and the cleanup aborts the script and leaves the files behind, where they survive across the rest of that CI session.

Once ${USER_FILES_PATH}/a exists, 02843's assertion SELECT 1 in (SELECT file(materialize('a'))) returns 0 (the file is read successfully and 1 is not in the resulting Set) instead of throwing FILE_DOESNT_EXIST, and the -- { serverError FILE_DOESNT_EXIST } check trips the test.

Reproduced locally:

$ echo -n Hello > $USER_FILES_PATH/a
$ clickhouse-client -q "SELECT 1 in (SELECT file(materialize('a')))"
0    # ← bug
$ rm $USER_FILES_PATH/a
$ clickhouse-client -q "SELECT 1 in (SELECT file(materialize('a')))"
Code: 107. DB::ErrnoException: Cannot open file ... FILE_DOESNT_EXIST   # ← expected

50/50 deterministic.

Fix

Two changes:

  1. Root cause: 01658_read_file_to_stringcolumn.sh now registers trap cleanup EXIT immediately after sourcing shell_config.sh. The cleanup removes all files the test creates in user_files_path and /tmp, using rm -f so it's idempotent. The redundant manual cleanup at the bottom of the script is removed.

  2. Defense in depth: 02843_context_has_expired.sql uses '02843_nonexistent_'||generateUUIDv4() as the path passed to file(). The test's intent (verifying that file() throws FILE_DOESNT_EXIST on a missing file) is preserved, and the assertion can no longer be poisoned by another test leaking a short filename like a.

Verification

  • Pre-fix, with ${USER_FILES_PATH}/a pre-seeded: 02843 fails 50/50 (matches CI failure exactly).
  • Post-fix, with ${USER_FILES_PATH}/a pre-seeded: 02843 passes 50/50.
  • 01658 trap verified to fire on set -e mid-script abort (manual bash test with false).
  • bash -n syntax check on modified 01658 passes.

Version info

  • Merged into: 26.6.1.635

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (per task pipeline)

a) Deterministic repro? Yes. After echo -n Hello > ${USER_FILES_PATH}/a, the pre-fix 02843_context_has_expired.sql fails 50/50 with the exact CI signature (The query succeeded but the server error '107' was expected). With the fix in place, the same setup passes 50/50.

b) Root cause explained? Yes. 01658_read_file_to_stringcolumn.sh writes a, b, c into user_files_path and removes them only via the manual rm at the bottom. Under set -e, any failing query between the write and the cleanup aborts the script and leaks those files. Once ${USER_FILES_PATH}/a exists, 02843's SELECT 1 in (SELECT file(materialize('a'))) succeeds (returning 0) instead of throwing FILE_DOESNT_EXIST, and the -- { serverError FILE_DOESNT_EXIST } annotation trips.

c) Fix matches root cause? Yes. The root-cause fix is trap cleanup EXIT in 01658 so the files cannot leak even on mid-script abort. The defense-in-depth fix in 02843 swaps the hardcoded 'a' for '02843_nonexistent_'||generateUUIDv4(), so the assertion cannot be poisoned by any future test that leaks a short filename.

d) Test intent preserved? Yes. 02843 still asserts file() throws FILE_DOESNT_EXIST on a missing path. 01658 still runs every valid/invalid case it did before; only the cleanup mechanism changed (manual rm -> trap cleanup EXIT).

e) Both directions demonstrated? Yes. With ${USER_FILES_PATH}/a pre-seeded:

  • pre-fix 02843: passes=0 fails=50 (out of 50)
  • post-fix 02843: passes=50 fails=0 (out of 50)

f) Fix is general? Yes. The trap pattern is the canonical bash idiom for cleanup on abort, already used by many other stateless tests (e.g. 00309_formats_case_insensitive.sh, 00408_http_keep_alive.sh, 01278_min_insert_block_size_rows_for_materialized_views.sh). The defensive 02843 rewrite protects against any other test that might write a (or any short filename) into user_files_path and crash before cleaning up — the fix isn't narrow to 01658.

Session: cron:clickhouse-ci-task-worker:20260521-223000.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov — could you review this? Test-only fix for a flaky-test root cause: 01658_read_file_to_stringcolumn.sh leaks ${USER_FILES_PATH}/a on mid-script abort under set -e, which then makes 02843_context_has_expired's FILE_DOESNT_EXIST assertion succeed instead of throw. Adding trap cleanup EXIT to 01658 and a UUID-suffixed path to 02843.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label May 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0f93f37]

Summary: ✅


AI Review

Summary

This PR makes 01658_read_file_to_stringcolumn.sh clean up files in user_files_path through an EXIT trap and changes 02843_context_has_expired.sql to use a UUID-suffixed missing file path, so leaked short filenames no longer poison its FILE_DOESNT_EXIST assertion. I did not find any correctness or CI-reliability issues in the current diff, and the existing human thread about the fixed /tmp/c.txt path is addressed by the latest code.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 22, 2026
@azat azat self-assigned this Jun 2, 2026
cleanup() {
rm -f "${USER_FILES_PATH}"/{a,b,c}.txt
rm -f "${USER_FILES_PATH}"/{a,b,c}
rm -f /tmp/c.txt

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.

Why the test uses /tmp/c.txt, if you need something outside of user_files, just use regular tmp dir (that is unique for each test)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Done in 0f93f37: the file now lives in the per-test unique $CLICKHOUSE_TMP dir ($CLICKHOUSE_TMP/01658_outside.txt) instead of the fixed /tmp/c.txt, so concurrent runs can't collide on it, and the runner removes it afterwards.

In client mode file() rejects any path outside user_files before checking existence, so the rejection assertion is unaffected by the path. Only the local-mode read needs the contents. The ..-traversal case keeps its relative path since it only checks that escaping user_files is denied.

Verified locally via the runner: 01658 and 02843 pass 5/5 each with randomization, no /tmp/c.txt is created, and the EXIT trap still cleans up on a mid-script abort.

`01658_read_file_to_stringcolumn.sh` writes the short filenames `a`, `b`,
`c` into `user_files_path` and removes them only by manual `rm` at the
bottom of the script. The script runs under `set -e`, so any failing
query between the write and the cleanup aborts the script and leaves
those files behind. The next CI run on the same server then sees them.

`02843_context_has_expired.sql` relies on `user_files_path/a` being
absent so `SELECT 1 in (SELECT file(materialize('a')))` throws
`FILE_DOESNT_EXIST`. Once `a` is leaked, that query returns 0 instead,
the expected error never fires, and the test fails. CI shows this exact
pattern on multiple unrelated PRs over the last 90 days (the diagnostic
"Test also fails without randomized settings" matches a stuck server
state, not randomization).

This change:
- adds `trap cleanup EXIT` to `01658` so the files are removed even on
  mid-script abort. Drops the now-redundant manual `rm` block.
- defensively rewrites the `file()` assertion in `02843` to use a
  UUIDv4-suffixed path, so a future leak of `a` (or any other short
  filename) cannot poison this test.

Verified locally: with `a` pre-seeded in `user_files_path`, pre-fix
`02843` fails 50/50; post-fix it passes 50/50.
@groeneai
groeneai force-pushed the groeneai/fix-02843-context-has-expired-flaky-leak branch from fb2b0b6 to a85f400 Compare June 11, 2026 00:19
@groeneai

Copy link
Copy Markdown
Collaborator Author

Rebased onto current master (a85f4007) and re-pushed to refresh CI.

The previous run's only red was test_mysql_protocol/test.py::test_mysql_dotnet_client (Integration tests amd_llvm_coverage 3/5) — the .NET MySQL wire-protocol test, whose stdout was truncated mid-stream in the assert. This PR is test-only (two stateless .sql/.sh files), so it has no causal path to a .NET MySQL integration test; under the heavily-instrumented amd_llvm_coverage build that external-client test is timing-sensitive and flakes. The other three reds (Finish Workflow, Mergeable Check, PR) were rollups of that single failure.

The underlying fix is still needed: master still has 01658's manual rm-without-trap cleanup and 02843's hardcoded file(materialize('a')) assertion. Fresh CI running now.

The "outside user_files" file was created at a fixed /tmp/c.txt, a global
path shared by every concurrent run of this test. Move it into the per-test
unique $CLICKHOUSE_TMP dir (which the test runner creates per test and removes
afterwards) so concurrent tests cannot collide on it.

In client mode file() rejects any path outside user_files before checking
existence, so the rejection assertion is unaffected by the new path. Only the
local-mode read actually needs the file's contents. The ".." traversal case
keeps its relative path since it only tests that escaping user_files is denied.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@azat
azat enabled auto-merge June 11, 2026 07:21
@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.30% +0.10%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 295 line(s), 16 function(s) across 106 file(s) · Details

Top files
  • src/Storages/StorageReplicatedMergeTree.cpp: 14 line(s)
  • src/AggregateFunctions/AggregateFunctionWindowFunnel.cpp: 11 line(s), 6 function(s)
  • src/Processors/Formats/Impl/Parquet/Decoding.cpp: 11 line(s)
  • src/Interpreters/DatabaseCatalog.cpp: 10 line(s)
  • src/Common/ShellCommand.cpp: 9 line(s)

Full report

@azat
azat added this pull request to the merge queue Jun 11, 2026
Merged via the queue into ClickHouse:master with commit 44aecc8 Jun 11, 2026
166 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 11, 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 pr-ci 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