Skip to content

Fix UDF registry loss during Keeper session expiry - #101891

Merged
fm4v merged 10 commits into
masterfrom
fix/udf-partial-refresh-keeper-session-expiry
Apr 8, 2026
Merged

fm4v merged 10 commits into
masterfrom
fix/udf-partial-refresh-keeper-session-expiry

Conversation

@fm4v

@fm4v fm4v commented Apr 6, 2026 •

Copy link
Copy Markdown
Member

Fix UserDefinedSQLObjectsZooKeeperStorage::refreshObjects replacing the in-memory UDF registry with a partial set when Keeper session expires mid-refresh. tryLoadObject caught all exceptions including Keeper hardware errors, so remaining UDFs silently became nullptr and setAllObjects overwrote the registry.

Split the catch block in tryLoadObject to re-throw Keeper hardware errors (session expired, connection loss). Wrap the load loop in ZooKeeperRetriesControl::retryLoop with exponential backoff. setAllObjects only executes after a fully successful load — if retries are exhausted, the existing registry is preserved.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix UDF registry loss when ZooKeeper session expires during periodic refresh — all user-defined functions could become unavailable until a full refresh succeeds.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.635
  • Backported to: 26.3.5.6, 26.2.8.16, 26.1.8.23

When refreshObjects() loads UDFs from ZooKeeper and the session expires
mid-loop, tryLoadObject() was catching the exception and returning nullptr.
The partially-loaded set was then passed to setAllObjects(), which replaced
the entire in-memory registry — causing all UDFs that failed to load to
become UNKNOWN_FUNCTION until the next successful refresh cycle.

The fix has two parts:

1. tryLoadObject() now re-throws Keeper hardware errors (session expired,
   connection loss) instead of silently returning nullptr. Non-hardware
   errors and parse failures still return nullptr as before.

2. refreshObjects() now uses ZooKeeperRetriesControl to retry the entire
   load loop on Keeper hardware errors with exponential backoff
   (200ms initial, 5s max, 10 retries). setAllObjects() is only called
   after a fully successful load. If all retries are exhausted, the
   exception propagates to the caller and the existing registry is
   preserved intact.
@clickhouse-gh

clickhouse-gh Bot commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [59cc611]

Summary: ❌

job_name test_name status info comment
Stress test (arm_ubsan) failure
Hung check failed, possible deadlock found FAIL cidb, issue ISSUE EXISTS
Finish Workflow failure
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py failure IGNORED
Integration tests (amd_asan_ubsan, targeted) error IGNORED

AI Review

Summary

This PR fixes a real correctness issue in UserDefinedSQLObjectsZooKeeperStorage: partial in-memory UDF registry replacement when Keeper hardware errors happen mid-refresh. It now re-throws hardware KeeperException in tryLoadObject and wraps refresh loading in ZooKeeperRetriesControl, so setAllObjects runs only after a successful full load. I did not find additional blocker/major issues in the current diff.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
Final Verdict
  • Status: ✅ Approve

@fm4v fm4v added the pr-bugfix Pull request with bugfix, not backported by default label Apr 6, 2026
@fm4v
fm4v requested a review from XuJia0210 April 6, 2026 22:39
@@ -0,0 +1,116 @@
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks like an accidental duplicate of tests/integration/test_replicated_user_defined_functions_keeper_fault/test.py committed under the repository tmp/ tree.

Please remove this file from the PR. Keeping test sources under tmp/ is fragile (it is typically used for transient/local artifacts) and can cause confusion about which test is authoritative.

"destination_port": 2181,
"protocol": "tcp",
"action": "DROP",
"probability": 0.9,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test uses probabilistic packet loss (probability: 0.9) plus fixed time.sleep(10) windows, so it is inherently nondeterministic and can be flaky in both directions: it may fail even when code is correct (no matching log within the time window), or pass without reliably reproducing the target race.

Please make the failure injection deterministic (for example: force a full disconnect long enough to guarantee session expiration, then restore connectivity and trigger refresh explicitly) and gate assertions with retry/polling on an observable state transition rather than fixed sleeps.

@clickhouse-gh

clickhouse-gh Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 72.97% (27/37) | lost baseline coverage: 4 line(s) · Uncovered code

Full report · Diff report

@fm4v fm4v added pr-must-backport Pull request should be backported intentionally. Use this label with great care! v25.10-must-backport v25.12-must-backport labels Apr 8, 2026
@fm4v
fm4v added this pull request to the merge queue Apr 8, 2026
Merged via the queue into master with commit 481febe Apr 8, 2026
159 of 163 checks passed
@fm4v
fm4v deleted the fix/udf-partial-refresh-keeper-session-expiry branch April 8, 2026 08:20
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 8, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 8, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Apr 8, 2026
clickhouse-gh Bot added a commit that referenced this pull request Apr 8, 2026
Backport #101891 to 26.2: Fix UDF registry loss during Keeper session expiry
fm4v added a commit that referenced this pull request Apr 8, 2026
Backport #101891 to 26.3: Fix UDF registry loss during Keeper session expiry
fm4v added a commit that referenced this pull request Apr 8, 2026
The `ZooKeeperRetriesControl` retry loop introduced in #101891 reused
the same expired ZooKeeper session on every retry iteration, without
calling `renewZooKeeper` as every other retry loop in the codebase does.
This caused repeated requests on a finalized session, leading to crashes
(clickhouse-core-incidents #1644, #1645).

Remove the broken retry loop. The `tryLoadObject` split-catch that
re-throws Keeper hardware errors is sufficient: the exception propagates
to `processWatchQueue`'s catch-all, which resets the session and retries
from scratch with a fresh connection. This preserves the original fix's
goal — `setAllObjects` is never called with a partial set — without the
stale-session misuse.
fm4v added a commit that referenced this pull request Apr 8, 2026
Backport #101891 to 26.1: Fix UDF registry loss during Keeper session expiry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v25.12-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants