Skip to content

Fix LLVM profile file contention in coverage builds by adding %p to patterns - #97020

Closed
alexey-milovidov wants to merge 5 commits into
masterfrom
fix-llvm-profile-stderr-integration-tests
Closed

alexey-milovidov wants to merge 5 commits into
masterfrom
fix-llvm-profile-stderr-integration-tests

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 15, 2026 •

Copy link
Copy Markdown
Member

Summary

  • In coverage builds, many short-lived clickhouse-client processes inherit the same LLVM_PROFILE_FILE env var with only %Nm (merge pool) in the pattern
  • When these processes exit concurrently, they contend over the same small pool of profraw files, causing LLVM Profile Error: Failed to write file ... File exists
  • Fixed by adding %p (PID) to all LLVM_PROFILE_FILE patterns (it-%p-%4m.profraw, ft-{batch}-%p-%2m.profraw, ft-server-%p-%m.profraw), so each process writes to its own set of files
  • llvm-profdata merge handles the additional files transparently

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=d9b2a1b80f514d66c594b524439026ff87193ada&name_0=MasterCI&name_1=Integration%20tests%20%28amd_llvm_coverage%2C%203%2F5%29

Test plan

  • Verified the root cause in LLVM compiler-rt source (InstrProfilingFile.c, InstrProfilingUtil.c)
  • The test_server_reload/test.py::test_change_listen_host test should pass in coverage builds without profile errors on stderr

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

...

In coverage builds, LLVM instrumentation outputs warnings and errors
to stderr like `LLVM Profile Error: Failed to write file ... File exists`.
These are not real ClickHouse errors but cause the test client to raise
`QueryRuntimeException` even when the return code is 0.

This was causing `test_server_reload/test.py::test_change_listen_host`
to fail in `amd_llvm_coverage` CI runs.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=d9b2a1b80f514d66c594b524439026ff87193ada&name_0=MasterCI&name_1=Integration%20tests%20%28amd_llvm_coverage%2C%203%2F5%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b1ebf80]

Summary: ✅

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The question is - why "file exists"? Is it due to pid collision? Are there better workarounds?

…terns

The `LLVM Profile Error: Failed to write file ... File exists` occurs because
many short-lived `clickhouse-client` processes inherit the same
`LLVM_PROFILE_FILE` env var and contend over a small merge pool. With only
`%Nm` in the pattern, all processes share N files and fight over file locks.

Adding `%p` (PID) gives each process its own set of profile files, eliminating
contention. `llvm-profdata merge` handles the additional files transparently.

This replaces the previous stderr filtering workaround with a proper fix.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The "File exists" error is not a PID collision per se — it's a file contention issue. All short-lived clickhouse-client processes inherit the same LLVM_PROFILE_FILE environment variable (e.g., it-%4m.profraw). The %Nm pattern creates a merge pool of only N files. When many clickhouse-client invocations exit concurrently, they all try to open/lock the same small pool of profraw files for merging, and the LLVM runtime's writeFile fails with EEXIST.

Fixed by adding %p (PID) to all LLVM_PROFILE_FILE patterns, so each process gets its own set of profile files. llvm-profdata merge handles the additional files transparently. The stderr filtering workaround is reverted.

@alexey-milovidov alexey-milovidov changed the title Filter LLVM Profile messages from stderr in integration test client Fix LLVM profile file contention in coverage builds by adding %p to patterns Feb 15, 2026
@fm4v

fm4v commented Feb 15, 2026

Copy link
Copy Markdown
Member

This might happen only if the clickhouse binary was overwritten by a different version during other tests. I'm looking into how to make it immutable here
#97029

@alexey-milovidov
alexey-milovidov marked this pull request as draft February 15, 2026 23:47
@alexey-milovidov

alexey-milovidov commented Feb 15, 2026 •

Copy link
Copy Markdown
Member Author

@fm4v, our compatibility tests run old binaries.
But old binaries should not have coverage.

Marouane-elfadli pushed a commit to Marouane-elfadli/ClickHouse that referenced this pull request Nov 6, 2026
On the per-test coverage lanes every process of the coverage binary merged
its counters at exit into a merge-pool file shared by the whole job (`%2m`
for stateless clients and the job env, `%4m` for integration containers and
host clients), without continuous mode. The exit-time merge maps the pool
file, merges it, truncates it and rewrites it from offset 0. A process
SIGKILLed during that rewrite (02435's `thread_cancel`, the runner's
process-group kill on a timeout) can leave the value-profile records cut
inside a record. Every later process whose pool slot (`getpid() % N`) is
that file then reads past the mapping in `lprofMergeValueProfData` and dies
with SIGSEGV after its output, and the file stays poisoned. NightlyCoverage
`Stateless tests (amd_llvm_coverage_per_test, per_test_coverage, 2/8)` on
358b830 lost 251 results this way: all 392 crashing client PIDs were even
(pool slot 0), and the 163 sequential tests, which use another pool file,
all passed.

Those lanes never merge a `.profraw`: per-test coverage is read inside the
server from the static counter section and exported from
`system.coverage_log`. So point `LLVM_PROFILE_FILE` at `/dev/null` there:
no process opens, reads or merges a pool file any more. The other lanes keep
their `%c` values, which never merge. Not `%p` (thousands of files, ClickHouse#97020),
not `%c` again (the shared-mapping slowdown ClickHouse#118370 removed).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants