Skip to content

Make flaky test 03568_udf_memory_tracking lighter to avoid TSan timeout - #107699

Merged
Algunenano merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-03568-udf-memory-tracking-tsan-timeout
Jun 17, 2026
Merged

Algunenano merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-03568-udf-memory-tracking-tsan-timeout

Conversation

@groeneai

@groeneai groeneai commented Jun 17, 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 into CHANGELOG.md):

...

Description

03568_udf_memory_tracking was flaky on Stateless tests (amd_tsan, s3 storage, parallel): clean 600s per-test timeouts (CIDB: OK runs p50 247s / p90 303s / max 506s, plus 5 timeouts over 40 days). It streamed 300 single-row blocks (numbers(100) + numbers(200), max_block_size = 1) through an executable UDF; each block forks /bin/sh -c awk, which is ~100x slower under TSan, and parallel CPU contention pushed it past the timeout.

Reduce the workload to numbers(40) + numbers(60) = 100 blocks (3x fewer fork/exec) and drop the (previously proposed) no-parallel tag.

The fix under test (fb79ace) attaches the data-sending threads to the query thread group, so query memory_usage stays flat regardless of block count. Without it, each block leaks ~1MB into the query tracker. Measured locally with a pre-fix vs current binary:

blocks without fix with fix
40 46.6 MB 5.4 MB
60 67.8 MB 5.4 MB

So both queries stay 2.3x-3.4x above the 20MB assertion without the fix (the test still fails on a regression) and ~3.7x below it with the fix. The two < 20MB assertions and the executable UDF are unchanged.

Verified via clickhouse-test: pre-fix binary FAILS (min/max_less_then_20mb: 0), current binary passes 20/20 with randomization in the parallel bucket.

Version info

  • Merged into: 26.6.1.934

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? The failure is a 600s per-test timeout, not a logic error, so there is no single failing query. The cause is deterministic and data-proven: on amd_tsan, s3 storage, parallel OK runs are p50 248s / p90 303s / max 506s and all 5 failures are exactly 600.0s; the same test runs in 15-23s in the amd_tsan, ..., sequential bucket. The trigger (300 fork/exec of an executable UDF under TSan + parallel CPU contention) is fully isolated.
b Root cause explained? max_block_size = 1 over 100+200 rows makes the executable UDF (execute_direct = 0, forks /bin/sh -c awk) run once per block = 300 sequential fork/exec. TSan instrumentation makes fork in a multithreaded process ~100x slower; under parallel CPU contention the run crosses the 600s --timeout wall and is killed ("Timeout! Processes left in process group").
c Fix matches root cause? Yes. no-parallel removes the CPU contention by running the test in the sequential bucket, directly addressing the slowdown mechanism. It does not touch the workload or assertions.
d Test intent preserved? Yes. The 300-block workload (the exact thing that exercises per-block memory-tracking drift) and both < 20MB memory assertions are byte-for-byte unchanged. The test still fails if memory tracking regresses, on every build including TSan.
e Demonstrated in both directions? "Fails without": CIDB shows 5 clean 600.0s timeouts on the parallel bucket across 8 unrelated PRs while sequential runs finish in 15-23s — the parallel bucket is the failing direction the tag removes. "Passes with": ran the exact test workload locally against a memory-capped server with the UDF installed; output matched the reference exactly (queries: 2, min_less_then_20mb: 1, max_less_then_20mb: 1).
f Fix is general, not a narrow patch? N/A - this is a test-scheduler tag change, not a source-code bug fix. No C++ code paths are involved.

Session: cron:clickhouse-worker-slot-4:20260617-035100

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rschu1ze — could you review this? Test-only: tags 03568_udf_memory_tracking as no-parallel because its 300-block executable-UDF fork/exec workload runs ~100x slower under TSan and intermittently crosses the 600s per-test timeout under parallel CPU contention (clean 600.0s timeouts across 8 unrelated PRs; 15-23s in the sequential bucket). Workload and the <20MB memory assertions are unchanged.

@Algunenano Algunenano self-assigned this Jun 17, 2026
@@ -1,3 +1,6 @@
-- Tags: no-parallel

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.

Using no-parallel should be the last resort, and ideally reserved for tests that modify the process global state (drop cache for example). Can't you instead make the test smaller / less resource intensive while still testing the memory tracking of UDFs?

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, dropped no-parallel. Instead I made the test lighter: 300 blocks (numbers(100)+numbers(200)) -> 100 blocks (numbers(40)+numbers(60)), 3x fewer fork/exec. That alone clears the TSan timeout (the cost is per-block fork of /bin/sh -c awk).

The drift the test catches is ~1MB/block (one TimeoutWriteBufferFromFileDescriptor freed off the query thread group), so a smaller block count still exceeds the 20MB assertion without the fix. Measured pre-fix vs current binary:

blocks without fix with fix
40 46.6 MB 5.4 MB
60 67.8 MB 5.4 MB

So it still FAILS on a regression (clickhouse-test on a pre-fix binary: min/max_less_then_20mb: 0) and passes 20/20 with randomization in the parallel bucket. Assertions and the UDF are unchanged.

@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Jun 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e01d582]

Summary: ✅


AI Review

Summary

This PR reduces 03568_udf_memory_tracking from 300 to 100 single-row executable-UDF blocks and marks it long so clickhouse-test flaky-check does not fail the 180s soft runtime gate. I reviewed the prior discussion, current diff, test_function configuration, clickhouse-test handling for long, and the latest Praktika report; no correctness or coverage regression stood out.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 17, 2026
The test streamed 300 single-row blocks (numbers(100) + numbers(200) with
max_block_size = 1) through an executable UDF. Each block forks /bin/sh -c awk,
which under TSan is roughly two orders of magnitude slower than a debug build.
On the parallel test bucket the CPU contention pushed total run time past the
600s per-test timeout (CIDB: OK runs p50 247s / p90 303s / max 506s, plus 5
clean 600s timeouts over 40 days), making the test flaky on
Stateless tests (amd_tsan, s3 storage, parallel).

Reduce the workload to 40 + 60 = 100 blocks (3x fewer fork/exec) so it runs
well inside the timeout while still exercising the per-block memory tracking
that fb79ace fixed.

The fix is the thread-group attachment of the data-sending threads, so query
memory_usage stays flat regardless of block count; without it, each block
leaks ~1MB into the query tracker (one TimeoutWriteBufferFromFileDescriptor,
DBMS_DEFAULT_BUFFER_SIZE) freed off the query thread group. Measured locally:

  blocks   without fix   with fix
  40       46.6 MB       5.4 MB
  60       67.8 MB       5.4 MB

So 40 and 60 blocks both stay 2.3x-3.4x above the 20MB assertion without the
fix (test still fails on a regression) and ~3.7x below it with the fix. The
two assertions and the executable UDF are unchanged. Dropping no-parallel:
the smaller workload no longer needs it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the fix-03568-udf-memory-tracking-tsan-timeout branch from dcad003 to e0243f7 Compare June 17, 2026 10:40
@groeneai groeneai changed the title Fix flaky 03568_udf_memory_tracking timeout under TSan (no-parallel) Make flaky test 03568_udf_memory_tracking lighter to avoid TSan timeout Jun 17, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (updated for the lighter-test approach)

# Question Answer
a Deterministic repro? Yes. The flake is a 600s per-test timeout on amd_tsan, s3 storage, parallel (CIDB: OK runs p50 247s / p90 303s / max 506s, plus 5 clean 600s timeouts / 40d). Root: 300 single-row blocks each fork /bin/sh -c awk, ~100x slower under TSan; parallel CPU contention crosses the timeout.
b Root cause explained? Cost is dominated by per-block fork/exec, which scales linearly with block count. The drift the test verifies (fb79ace) is ~1MB/block: each block allocates a TimeoutWriteBufferFromFileDescriptor (DBMS_DEFAULT_BUFFER_SIZE = 1MiB) in the query thread but frees it in a pool thread; the fix attaches that thread to the query thread group so the free is accounted. So memory stays flat with the fix and grows ~1MB/block without it, independent of how many blocks we use.
c Fix matches root cause? Yes. Reducing 300 -> 100 blocks (numbers(40)+numbers(60)) cuts the fork/exec cost ~3x (the timeout driver) while keeping the drift well above the 20MB assertion without the fix. No no-parallel.
d Test intent preserved / new tests added? Preserved. The two < 20MB assertions and the executable UDF are byte-for-byte unchanged. The test still detects a memory-tracking regression (40 and 60 blocks both exceed 20MB without the fix).
e Both directions demonstrated? Yes, via clickhouse-test. Pre-fix binary (thread-group attach removed): FAIL, min/max_less_then_20mb: 0 (46.6 MB and 67.8 MB). Current binary (master HEAD, fix present): PASS 20/20 with randomization, "Found 20 parallel tests and 0 sequential tests".
f Fix is general, not a narrow patch? N/A (test-only change; no source modified). The measured per-block drift (~1MB) and flat with-fix profile confirm the chosen block count is not a coincidental margin.

Session id: cron:clickhouse-worker-slot-6:20260617-095600

@Algunenano

Copy link
Copy Markdown
Member

The reduced 100-block workload still runs over the flaky-check 180s
soft-check (TEST_MAX_RUN_TIME_IN_SECONDS) under amd_tsan, where each
per-block fork/exec of the executable UDF is ~100x slower than debug.
clickhouse-test marks such a run FAIL/TOO_LONG unless the test carries
the long tag (tests/clickhouse-test gate: test_runs>1 and flaky_check
and total_time>180 and "long" not in tags).

The long tag exempts that soft-check and cuts the flaky-check repeat
count to the long ratio (~10%), which also reduces self-contention. It
does NOT make the test sequential (only no-parallel/sequential do), so
the test keeps running in the parallel bucket. The 100-block workload
and both < 20MB assertions are unchanged, so it still catches a
memory-tracking regression on every build including TSan.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Follow-up: after the 300->100 block reduction, CI surfaced a separate failure on Stateless tests (amd_tsan, flaky check): the test still runs over the flaky-check 180s soft-check (TEST_MAX_RUN_TIME_IN_SECONDS), where each per-block fork/exec of the executable UDF is far slower than debug.

That soft-check (tests/clickhouse-test: test_runs>1 and flaky_check and total_time>180 and "long" not in tags) is gated by the long tag, so I added -- Tags: long (commit e01d582). This is distinct from no-parallel: long only exempts the flaky-check soft-check and cuts the flaky-check repeat count to ~10%; it does NOT serialize the test. clickhouse-test still reports "Found 1 parallel tests and 0 sequential tests", so the block-reduction approach you approved is unchanged and the test keeps running in the parallel bucket. Workload and both < 20MB assertions are byte-for-byte unchanged.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (updated for the long tag, commit e01d582)

Addresses a second, distinct CI failure that surfaced after the block reduction: Stateless tests (amd_tsan, flaky check) marked the test FAIL/TOO_LONG ("Test runs too long (> 180s). Make it faster.").

# Question Answer
a Deterministic repro? Yes. The gate is deterministic: tests/clickhouse-test fails a test when test_runs>1 and flaky_check and total_time>180 and "long" not in tags. The 100-block fork/exec workload still exceeds the 180s soft-check under amd_tsan (each per-block /bin/sh -c awk is far slower than debug; the flaky-check repeats the test many times).
b Root cause explained? The 600s regular-timeout flake was fixed by the 300->100 reduction, but the flaky-check job has a SEPARATE 180s per-run soft-check (TEST_MAX_RUN_TIME_IN_SECONDS) that is only bypassed by the long tag. The test is correct but legitimately heavy under TSan, so it crosses 180s in flaky-check mode.
c Fix matches root cause? Yes. -- Tags: long is exactly what gates that soft-check (line ... and "long" not in tags), and it also cuts the flaky-check repeat count to the long ratio (~10%), reducing self-contention. It is additive to the approved block reduction.
d Test intent preserved / new tests added? Preserved. long does NOT serialize the test (only no-parallel/sequential do), so it keeps running in the parallel bucket. Workload and both < 20MB assertions are byte-for-byte unchanged.
e Both directions demonstrated? Yes. Replicating the gate logic: without long, total_time>180 in flaky-check -> FAIL=True; with long -> FAIL=False (even at 500s). End-to-end clickhouse-test with the fix: [ OK ], "Found 1 parallel tests and 0 sequential tests". Tag parser confirms is_long_test=True, is_sequential=False.
f Fix is general, not a narrow patch? N/A (test-only change; no source modified). long is the idiomatic tag for tests that are correct but legitimately heavy under sanitizers.

Session id: cron:clickhouse-worker-slot-4:20260617-115700

@clickhouse-gh

clickhouse-gh Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.50% 77.40% -0.10%

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

Newly covered by added/modified tests: 306 line(s), 6 function(s) across 107 file(s) · Details

Top files
  • src/Storages/StorageReplicatedMergeTree.cpp: 34 line(s)
  • src/Common/ZooKeeper/ZooKeeperImpl.cpp: 16 line(s)
  • src/AggregateFunctions/AggregateFunctionQuantileGK.cpp: 14 line(s)
  • src/Storages/MergeTree/ReplicatedMergeTreeRestartingThread.cpp: 10 line(s)
  • src/Processors/Transforms/WindowTransform.cpp: 9 line(s)

Full report

@Algunenano
Algunenano added this pull request to the merge queue Jun 17, 2026
Merged via the queue into ClickHouse:master with commit 0867bd9 Jun 17, 2026
165 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 17, 2026
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Aug 3, 2026
03568_udf_memory_tracking fails sporadically on Stateless tests (amd_msan,
WasmEdge, parallel, 2/2) with exit code 242 (UDF_EXECUTION_FAILED) wrapping
"Code: 159 Pipe read timeout exceeded 10000 milliseconds".

This is not a test timeout: the failing runs die in 13.7 s / 16.3 s / 27.3 s
against the 600 s per-test limit. It is a per-invocation bound.

test_function is declared with execute_direct=0 and no command_read_timeout,
so ExternalUserDefinedExecutableFunctionsLoader.cpp:234 gives it the 10000 ms
default. execute_direct=0 means every call vfork+execs /bin/sh -c "awk ...",
and test_function is not an executable_pool, so a fresh pipe and process are
created per call. The test uses max_block_size = 1 over numbers(40) and
numbers(60), i.e. 100 independent fork/exec round-trips; that per-block shape
is the coverage the test exists for.

The read clock is armed independently of the writer's progress:
ShellCommandSource spawns each SendDataTask on a separate thread and builds
the input format over timeout_command_out immediately, so the 10 s budget
covers vfork, exec, sh and awk startup plus the scheduling delay of our own
writer thread. Under MSan in a loaded parallel bucket one round-trip can
exceed it, and one slow read out of 100 fails the whole query. The two
distinct throw sites across the reports (once while reading the header, twice
inside ParallelParsingInputFormat) confirm a startup-window stall.

PR ClickHouse#107699 cut the round-trips from 300 to 100. That lowers the number of
dice rolls but leaves the per-roll ceiling untouched, which is why the
failure carried over from TSan to MSan; trimming the workload again would
attack the wrong term and erode coverage.

Every assertion is unchanged: both memory checks, count() = 2, the 100-block
workload and the reference file. Nothing is masked either: awk on one row is
sub-millisecond work, all 100 round-trips take 0.43 s locally, and the bound
stays finite at 60 s so a genuine hang still fails. The same knob at the same
value already fixed the identical signature for executable dictionaries in
ClickHouse#108933.

Closes: ClickHouse#112994
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.

3 participants