Skip to content

Fix FileNotFoundError race in clickhouse-test suite tag scan - #109120

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-clickhouse-test-tag-scan-race
Jul 2, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-clickhouse-test-tag-scan-race

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Fix a flaky Stress test failure caused by a race in clickhouse-test's suite construction.

When several clickhouse-test invocations run concurrently against one shared queries directory, a run that needs query substitutions writes a transient per-PID copy of a test file (<name>.<pid><ext>, see SharedEngineReplacer.temp_file_path) next to the original and removes it once the test finishes. Another invocation's suite scan enumerates the directory with os.listdir in get_selected_tests and can observe that copy. Because the copy ends in a supported extension, is_test_from_dir accepts it and it is scheduled as a real test. It then fails in one of two ways: if the copy is deleted before the run reads its tags in load_tags_and_random_settings_limits_from_file or checks it in is_valid_utf_8, the unguarded open raises FileNotFoundError and aborts the whole run during suite construction; if the copy is still present, it runs as a bogus PID-suffixed test with no reference file and is reported NO_REFERENCE.

This surfaced as a flaky Stress test (arm_asan_ubsan) failure (Test script failed, script exit code: 1): one of the 8 parallel clickhouse-test instances died with FileNotFoundError: ... 03745_buffers_input_output_format_roundtrip.4491.sh in load_tags_and_random_settings_limits_from_file, where 4491 is the PID of a Process-3 worker in a sibling instance. There was no server crash, sanitizer report, or logical error.

The fix recognizes and excludes these transient copies at the single discovery choke point, is_test_from_dir, so they never enter the test list. SharedEngineReplacer embeds a dedicated infix marker (SharedEngineReplacer.TEMP_FILE_MARKER) into the per-PID copies it writes (<name>.shared_engine_replacer_tmp.<pid><ext>), and is_transient_test_copy skips only the files carrying that marker. The marker is a namespace clickhouse-test owns and no real test filename uses, so this recognizes the copies unambiguously without inferring "transient copy" from the basename shape and thus without constraining the names of genuine tests — a real test such as foo.1.sql is discovered normally even when foo.sql is also present. The copy stays next to the original, so .sh/.sql tests that resolve helpers or reference files relative to their own location are unaffected. Excluding the copy at discovery also removes the need for a FileNotFoundError guard in load_tags_and_random_settings_limits_from_file, so the earlier guard is reverted rather than left as a fallback that only masked one symptom.

Found while reviewing the CI report of #108827 (the failure was unrelated to that PR, which only touches CI tooling).

Related: #108827

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):

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.452 (included in 26.7 and later)

When several `clickhouse-test` workers run concurrently against one shared
queries directory, a worker that needs query substitutions writes a transient
per-PID copy of a test file (`<name>.<pid><ext>`, see `TestCase.temp_file_path`)
next to the original and removes it once done. Another worker's suite scan
enumerates the directory with `os.listdir` in `get_selected_tests` and reads
each file's tags via `load_tags_and_random_settings_limits_from_file`; if a
sibling worker's transient copy is listed and then deleted before this worker
opens it, the unguarded `open` raises `FileNotFoundError` and aborts the entire
run during suite construction (before any test runs).

Guard the `open` so a file that vanished mid-scan is skipped (it has no tags to
contribute) instead of crashing the whole run. Mirrors the existing
`UnicodeDecodeError` handling in the same function.

Observed as a flaky `Stress test (arm_asan_ubsan)` failure ("Test script
failed", `script exit code: 1`): one of the 8 parallel `clickhouse-test`
instances died with
`FileNotFoundError: ... 03745_buffers_input_output_format_roundtrip.4491.sh`
in `load_tags_and_random_settings_limits_from_file`, where `4491` is the PID of
a `Process-3` worker in a sibling instance.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7d2f323]

Summary: ✅


AI Review

Summary

This PR changes SharedEngineReplacer to stamp its transient per-PID copies with a dedicated marker, filters those copies out in is_test_from_dir, and adds a focused ci/tests regression that preserves the foo.sql / foo.1.sql case that an earlier revision broke. I did not find a new correctness issue in the current head; the three previously raised review points are addressed in the final diff.

Missing context / blind spots
  • ⚠️ Praktika had not published actual job results for commit 7d2f323ecd3ece0a8736a6f427dbb762e000adee at review time: the linked result_pr.json reported Total: 0, so this review could not confirm the fix in the real Stress test job that originally flaked.
  • ⚠️ I did not run the full clickhouse-test stress job locally. Validation here was limited to reading the final code, python3 -m py_compile on the touched Python files, and direct helper traces confirming that marked temp copies are skipped while real look-alike tests such as foo.1.sql remain discoverable.
Final Verdict

✅ No new inline findings for the current PR head.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 2, 2026
Comment thread tests/clickhouse-test Resolved
The earlier `FileNotFoundError` guard in
`load_tags_and_random_settings_limits_from_file` only masked one symptom
of the race. `SharedEngineReplacer` writes a transient per-PID copy of a
test (`<name>.<pid><ext>`, see `SharedEngineReplacer.temp_file_path`) next
to the original while the test runs. A concurrent `clickhouse-test`
invocation's suite scan (`os.listdir` in `get_selected_tests`) can list
that copy and schedule it as a real test: it then either fails as
`FileNotFoundError` in `is_valid_utf_8` once the copy is deleted, or runs
as a bogus PID-suffixed test reported `NO_REFERENCE`.

Recognize and exclude these copies in `is_test_from_dir` (the single
discovery choke point used by both `get_selected_tests` and the stress
diagnosis scan) so they never enter the test list. The copy is only
skipped when the original it was made from is present, which makes a
false positive on a genuine test impossible: no test in the tree has a
`<name>.<digits><ext>` shape. This makes the earlier `open` guard
unnecessary, so it is reverted.

Related: #108827

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread tests/clickhouse-test Resolved
Address review: the previous `is_transient_test_copy` inferred "transient
copy" from the basename shape — it skipped any file named `<name>.<digits><ext>`
whenever `<name><ext>` also existed. That changes `clickhouse-test` discovery
semantics for valid suites: a real test named `foo.1.sql` would be silently
dropped whenever `foo.sql` is also present, coupling the race fix to an
undocumented restriction on genuine test names.

Instead, give the transient per-PID copies created by `SharedEngineReplacer`
a dedicated infix marker (`SharedEngineReplacer.TEMP_FILE_MARKER`) that no real
test filename uses, and skip files carrying that marker at discovery. This
recognizes the copies unambiguously without constraining the names of genuine
tests: `foo.1.sql` is now discovered normally, while
`foo.shared_engine_replacer_tmp.<pid>.sql` is skipped.

The copy stays next to the original (same directory), so `.sh`/`.sql` tests
that resolve helpers or reference files relative to their own location are
unaffected.

Verified by loading the real functions from `clickhouse-test`: the copy that
`SharedEngineReplacer` produces is recognized by `is_transient_test_copy`, and
`is_test_from_dir` discovers `foo.1.sql` (the reviewer's counterexample) while
skipping the marked transient copy.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread tests/clickhouse-test Resolved
The AI review requested automated coverage for the producer/filter contract
this PR introduces. `ci/tests/test_shared_engine_replacer_discovery.py` loads
`tests/clickhouse-test` via `runpy.run_path`, has `SharedEngineReplacer`
materialize a transient per-PID copy next to `foo.sql`, and asserts that
`is_test_from_dir` skips only the marked `foo.shared_engine_replacer_tmp.<pid>.sql`
while still discovering both `foo.sql` and the `foo.1.sql` look-alike that the
earlier basename-shape heuristic silently dropped.

Related: #108827

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@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.

Wow!

@alexey-milovidov alexey-milovidov self-assigned this Jul 2, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 2, 2026
Merged via the queue into master with commit a202010 Jul 2, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-clickhouse-test-tag-scan-race branch July 2, 2026 20:13
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 2, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…est-tag-scan-race

Fix FileNotFoundError race in clickhouse-test suite tag scan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants