Repository navigation
Fix rusage race in executable UDF metering with check_exit_code=false - #109667
alexey-milovidov merged 4 commits into
Conversation
With check_exit_code=false the ShellCommandSource cleanup reaped the child with a single non-blocking wait4(WNOHANG) via tryReapWithoutStatusCheck. A child that has closed stdout (pipeline complete) but has only just reached _exit is not yet a reapable zombie, so the single WNOHANG returns 0, the child's rusage is lost, and ExecutableUserDefinedFunctionUserTimeMicroseconds reads zero. On loaded arm CI this intermittently failed test_executable_udf_profile_events::test_check_exit_code_false_succeeds_and_meters (assert cpu > 0). Make tryReapWithoutStatusCheck poll wait4(WNOHANG) for a bounded budget (1s, 5ms steps) so a promptly-exiting child is reaped and its usage captured. A child that lingers past the budget still falls through to ~ShellCommand's bounded command_termination_timeout + SIGTERM teardown, so cleanup never hangs. The regression test's UDF now closes stdout then sleeps 0.6s before exiting: above the single-shot failure threshold (deterministic UserTime=0 without the fix) yet within the poll budget and termination timeout. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-5:20260707-142800 |
|
cc @vitlibar @rschu1ze — could you review? Small fix: the executable-UDF reap on the |
|
Workflow [PR], commit [4e73d27] Summary: ✅
AI ReviewSummaryThis PR fixes the executable-UDF PR Metadata
Final Verdict |
The cleanup reap in tryReapWithoutStatusCheck used a hardcoded 1s poll budget, which did not follow the command_termination_timeout contract. With command_termination_timeout=0 that path could still spin ~1s before ~ShellCommand sends SIGTERM, stalling the query beyond the configured teardown; and when the timeout is larger than 1s, a child that exits naturally after the 1s budget was reaped by the destructor's waitForPid, which does not collect wait4 rusage, so ExecutableUserDefinedFunctionUserTimeMicroseconds stayed 0. Derive the reap poll budget from config.terminate_in_destructor_strategy.wait_for_normal_exit_before_termination_seconds (the same value the destructor waits). A configured 0 means a single non-blocking reap (no stall); a larger timeout lets the cleanup reap the child, with its rusage, anywhere inside the configured teardown window instead of losing usage to the destructor's waitForPid. The regression UDF now sleeps 2s (inside the 5s command_termination_timeout but past a 1s window) so the test deterministically distinguishes the config-derived budget from the old fixed 1s budget. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Follow-up to the review comment on Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-7:20260707-160700 |
|
@groeneai 1. Do not use the term "reap" in this PR. 2. Be only half as verbose. 3. Make a test that reproduces the problem. |
…omments Drop the term reap and cut comment verbosity per review. No logic change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Done (1070f64):
|
…or waits The cleanup-side wait and ~ShellCommand each waited the full command_termination_timeout, so a lingering check_exit_code=false child was charged the grace period twice (~6s for a 3s config) before SIGTERM. Both waits now draw from a single monotonic deadline, so the timeout bounds their sum. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260708-003900 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 26/27 (96.30%) · Uncovered code |
CI finish ledger — 4e73d27Every failure below has an owner. Only
All checks are green (Mergeable Check SUCCESS, Finish Workflow COMPLETED). Session id: cron:our-pr-ci-monitor:20260708-050000 |
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRFix rusage race in executable UDF metering with check_exit_code=false ClickGap verified: reproduced it with the script below on ReproductionShell reproducer — run against a local ClickHouse build ( # Executable UDF with check_exit_code=false whose script closes stdout, sleeps
# 2s (inside command_termination_timeout=5s), then exits non-zero:
# functions/udf.xml: test_udf_nonzero_exit -> udf_nonzero_exit.py
# <check_exit_code>false</check_exit_code>
# <command_termination_timeout>5</command_termination_timeout>
# udf_nonzero_exit.py: after emitting rows -> os.close(1); time.sleep(2.0); os._exit(3)
clickhouse local --print-profile-events \
--query "SELECT sum(test_udf_nonzero_exit(number)) FROM numbers(20)"
# Observed (pre-fix): ExecutableUserDefinedFunctionUserTimeMicroseconds == 0 (rusage lost:
# tryWaitWithoutStatusCheck did a single wait4(WNOHANG) and the child was alive-but-not-zombie);
# expected > 0. Integration test: test_executable_udf_profile_events::test_check_exit_code_false_succeeds_and_metersBisect
Affects
Suggested next stepThe bug reproduces as far back as the oldest tested release ( Component: cc fix PR author: @groeneai ClickGap · Finding: Triage metadata
|
…it-rusage-race Fix rusage race in executable UDF metering with check_exit_code=false
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Fixes a race that made
test_executable_udf_profile_events::test_check_exit_code_false_succeeds_and_metersflaky (assert cpu > 0).Failing check:
Integration tests (arm_binary, distributed plan, 3/4).CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=9d5fd8abfb763217edb76704dba9fb9453a6992d&name_0=MasterCI&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%203%2F4%29
Root cause. With
check_exit_code=false,ShellCommandSource::cleanupwaits the child viaShellCommand::tryWaitWithoutStatusCheck, which did a singlewait4(WNOHANG). A child that has closed stdout but only just reached_exitis not yet a zombie, soWNOHANGreturns 0, norusageis collected, andExecutableUserDefinedFunctionUserTimeMicrosecondsreads 0.Fix.
tryWaitWithoutStatusCheckpollswait4(WNOHANG)within the configuredcommand_termination_timeoutwindow (wait_for_normal_exit_before_termination_seconds), so a child exiting in that window has its usage collected here instead of by the destructor'swaitForPid, which collects none. Timeout 0 means a single non-blocking attempt, so a query is never stalled beyond the configuration. A child lingering past the budget still falls through to the boundedcommand_termination_timeout+ SIGTERM teardown (the..._lingering_child_is_bounded_and_flushes_bytesinvariant is preserved).Reproducing test.
udf_nonzero_exit.pycloses stdout then sleeps 2s (inside the 5scommand_termination_timeout, above a 1s window) before exiting non-zero, making the lost-usage window deterministic. Verified on a local debug build: without the fixtest_check_exit_code_false_succeeds_and_metersfails (UserTime == 0); with the fix it and the lingering-child test pass, andCHILD_WAS_NOT_EXITED_NORMALLYis never logged.Version info
26.7.1.800(included in26.7and later)