Skip to content

OCPBUGS-77056: Release lock before WaitForCacheSync in secret monitor#2132

Open
bentito wants to merge 6 commits intoopenshift:masterfrom
bentito:fix-external-cert-serialization
Open

OCPBUGS-77056: Release lock before WaitForCacheSync in secret monitor#2132
bentito wants to merge 6 commits intoopenshift:masterfrom
bentito:fix-external-cert-serialization

Conversation

@bentito
Copy link
Copy Markdown

@bentito bentito commented Feb 24, 2026

Summary

Routes using spec.tls.externalCertificate trigger a call to
AddSecretEventHandler for each route at router startup. The previous
implementation held the global write mutex (s.lock) for the entire duration
of the call, including the WaitForCacheSync round-trip to the API server and
etcd (~100–600ms per secret). This caused all N secret registrations to run
strictly serially:

Scale etcd latency Startup time
112 secrets ~587ms/secret 65s
1,738 secrets ~1,414ms/secret 40.9 minutes

The customer reports ~2,000 external cert routes causing router pods to fail
their 120s startup probe repeatedly and enter CrashLoopBackOff.

Root Cause

In addSecretEventHandler, WaitForCacheSync was called inside
defer s.lock.Unlock(), meaning the global write lock was held while waiting
for each secret's informer to complete its initial LIST to etcd. No other
registration could begin until the previous one finished.

Fix

Write the new monitoredItem into s.monitors before releasing the lock
(so concurrent callers for the same secret safely reuse the same informer),
then release the lock before WaitForCacheSync. This allows concurrent
AddSecretEventHandler calls for different secrets to perform their etcd
round-trips in parallel, reducing startup time from O(N × api_latency) to
O(api_latency).

On sync failure the map entry is cleaned up so a future caller can retry with
a fresh informer.

Testing

All existing unit tests in pkg/secret/... pass.

A concurrent registration test should be added (tracked separately).

Fixes: OCPBUGS-77056

When many routes use spec.tls.externalCertificate, the router calls
AddSecretEventHandler once per route at startup. Previously this held
the global write lock for the entire duration of each call, including
the WaitForCacheSync round-trip to the API server and etcd (~100-600ms
per secret). This serialized all N secret registrations, causing
startup time of O(N * api_latency):

  112 secrets x 587ms median = 65s  (healthy cluster)
 1738 secrets x 1414ms avg  = 40min (high etcd latency)

Fix: write the new monitoredItem into s.monitors before releasing the
lock (so concurrent callers for the same secret reuse the same
informer), then release the lock before WaitForCacheSync. This allows
concurrent AddSecretEventHandler calls for different secrets to perform
their etcd round-trips in parallel, reducing startup time from
O(N * api_latency) to O(api_latency).

On sync failure, the map entry is cleaned up so a future caller can
retry with a fresh informer.

Fixes: OCPBUGS-77056
Signed-off-by: Brett Tofel <btofel@redhat.com>
@openshift-ci-robot openshift-ci-robot added jira/severity-critical Referenced Jira bug's severity is critical for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Feb 24, 2026
@openshift-ci-robot
Copy link
Copy Markdown

@bentito: This pull request references Jira Issue OCPBUGS-77056, which is invalid:

  • expected the bug to target the "4.22.0" version, but no target version was set

Comment /jira refresh to re-evaluate validity if changes to the Jira bug are made, or edit the title of this pull request to link to a different bug.

The bug has been updated to refer to the pull request using the external bug tracker.

Details

In response to this:

Summary

Routes using spec.tls.externalCertificate trigger a call to
AddSecretEventHandler for each route at router startup. The previous
implementation held the global write mutex (s.lock) for the entire duration
of the call, including the WaitForCacheSync round-trip to the API server and
etcd (~100–600ms per secret). This caused all N secret registrations to run
strictly serially:

Scale etcd latency Startup time
112 secrets ~587ms/secret 65s
1,738 secrets ~1,414ms/secret 40.9 minutes

The customer reports ~2,000 external cert routes causing router pods to fail
their 120s startup probe repeatedly and enter CrashLoopBackOff.

Root Cause

In addSecretEventHandler, WaitForCacheSync was called inside
defer s.lock.Unlock(), meaning the global write lock was held while waiting
for each secret's informer to complete its initial LIST to etcd. No other
registration could begin until the previous one finished.

Fix

Write the new monitoredItem into s.monitors before releasing the lock
(so concurrent callers for the same secret safely reuse the same informer),
then release the lock before WaitForCacheSync. This allows concurrent
AddSecretEventHandler calls for different secrets to perform their etcd
round-trips in parallel, reducing startup time from O(N × api_latency) to
O(api_latency).

On sync failure the map entry is cleaned up so a future caller can retry with
a fresh informer.

Testing

All existing unit tests in pkg/secret/... pass.

A concurrent registration test should be added (tracked separately).

Fixes: OCPBUGS-77056

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci Bot requested review from Miciah and deads2k February 24, 2026 21:20
@bentito
Copy link
Copy Markdown
Author

bentito commented Feb 24, 2026

/jira refresh

@openshift-ci-robot openshift-ci-robot added jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. and removed jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Feb 24, 2026
@openshift-ci-robot
Copy link
Copy Markdown

@bentito: This pull request references Jira Issue OCPBUGS-77056, which is valid. The bug has been moved to the POST state.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (4.22.0) matches configured target version for branch (4.22.0)
  • bug is in the state ASSIGNED, which is one of the valid states (NEW, ASSIGNED, POST)

Requesting review from QA contact:
/cc @lihongan

Details

In response to this:

/jira refresh

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci Bot requested a review from lihongan February 24, 2026 23:20
@Miciah
Copy link
Copy Markdown
Contributor

Miciah commented Feb 25, 2026

/assign

@bentito
Copy link
Copy Markdown
Author

bentito commented Feb 25, 2026

/assign @everettraven

@bentito
Copy link
Copy Markdown
Author

bentito commented Feb 25, 2026

@everettraven Could you give this proposed fix a look from the standpoint of whether it makes logical sense or whether we might just be moving the problem to the API server? Thanks!

… cache sync

Signed-off-by: Brett Tofel <btofel@redhat.com>
@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci Bot commented Mar 3, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: bentito
Once this PR has been reviewed and has the lgtm label, please ask for approval from miciah. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@bentito
Copy link
Copy Markdown
Author

bentito commented Mar 27, 2026

/retest

Note: The ci/prow/unit failure appears to be in pkg/operator/staticpod/installerpod (TestCopyContent) which is a pre-existing test failure unrelated to this PR's changes. This PR only modifies pkg/secret/secret_monitor.go, pkg/secret/fake/fake_secret_monitor.go, and pkg/route/secretmanager/manager*.go. All tests in those packages pass locally with -race -count=5.

@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci Bot commented Mar 27, 2026

@bentito: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jira/severity-critical Referenced Jira bug's severity is critical for the branch this PR is targeting. jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants