Repository navigation
Conversation
✅ Deploy Preview for polite-licorice-3db33c canceled.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe Kubernetes sync now registers API types during package initialization. ChangesKubernetes scheme registration
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This localized change moves scheme registration to package initialization and is merge-ready after normal checks; no actionable merge-blocking risk remains. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
226de56 to
bbbe823
Compare
toddbaert
left a comment
There was a problem hiding this comment.
Nice catch, thanks! @yuchou87
Same addition as #2022 (review).
bbbe823 to
8112b41
Compare
kubernetes.Sync.Init called v1beta1.AddToScheme on the global client-go scheme. runtime.Scheme holds several unguarded maps and apimachinery documents it as "only threadsafe after registration is complete", so mutating it at arbitrary runtime moments violates that contract. flagd is unaffected because it calls Init serially at startup, but flagd-proxy calls it from one goroutine per sync target, so two targets registering close together race on those maps and the Go runtime kills the process with "fatal error: concurrent map read and map write". Moving the registration to package init keeps it single-goroutine and happens before anything can use the scheme concurrently. The work was already idempotent, so doing it once instead of per-Init is strictly less work. Signed-off-by: Yu Chou <[email protected]>
Signed-off-by: Todd Baert <[email protected]>
8112b41 to
1f472ab
Compare
|
🤖 I have created a release *beep* *boop* --- <details><summary>flagd: 0.16.2</summary> ## [0.16.2](flagd/v0.16.1...flagd/v0.16.2) (2026-08-14) ### 🐛 Bug Fixes * **eventing:** prevent send on closed channel in EmitToAll ([#1993](#1993)) ([8d7d41f](8d7d41f)) * possible race on eventstream connection ([#2025](#2025)) ([e38ecb7](e38ecb7)) * record OFREP evaluation metrics ([#2016](#2016)) ([8dd34dc](8dd34dc)) </details> <details><summary>flagd-proxy: 0.9.8</summary> ## [0.9.8](flagd-proxy/v0.9.7...flagd-proxy/v0.9.8) (2026-08-14) ### 🐛 Bug Fixes * **flagd-proxy:** set gRPC keepalive enforcement policy on the sync server ([#2022](#2022)) ([5591fa8](5591fa8)) ### 🧹 Chore * resolve open dependabot security alerts ([#2008](#2008)) ([bbb05d4](bbb05d4)) </details> <details><summary>core: 0.16.2</summary> ## [0.16.2](core/v0.16.1...core/v0.16.2) (2026-08-14) ### 🐛 Bug Fixes * **core:** register the OFO scheme at package init, not inside Sync.Init ([#2024](#2024)) ([cf2db36](cf2db36)) * **flagd-proxy:** set gRPC keepalive enforcement policy on the sync server ([#2022](#2022)) ([5591fa8](5591fa8)) * possible race on eventstream connection ([#2025](#2025)) ([e38ecb7](e38ecb7)) </details> --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Signed-off-by: OpenFeature Bot <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>



What
Moves
v1beta1.AddToScheme(scheme.Scheme)out ofkubernetes.Sync.Initand intothe package's
init(), so the global client-go scheme is mutated once, on a singlegoroutine, before anything can use it concurrently.
Fixes #2023.
Why
runtime.Schemeholds plain maps and carries no lock. apimachinery states thecontract on the type itself:
Sync.Initviolates that by writing to the globalscheme.Schemeat runtime.That is harmless in flagd, which calls
Initin a serial startup loop(
flagd/pkg/runtime/runtime.go:60), but flagd-proxy calls it from one goroutineper sync target at arbitrary times (
flagd-proxy/pkg/service/subscriptions/manager.go:128→
:201), so two targets registering close together race on those maps and the Goruntime kills the process:
We see this roughly once an hour in a test cluster with four sync targets. Two
distinct targets registering close in time are enough — a second subscriber to an
existing target reuses the multiplexer and never calls
Init. In practice it isself-reinforcing: the crash makes every client reconnect at once, which is exactly
the condition that triggers it.
Registering at package init matches what the type documents and costs nothing: the
work was already idempotent, so doing it once instead of per-
Initis strictlyless work.
utilruntime.Mustis the conventional Kubernetes idiom here — the callonly touches in-memory maps, so a failure would mean conflicting type definitions,
which is worth failing at startup for.
Change
All of
core/pkg/sync/kubernetes/kubernetes_sync.go:Sync.Init's signature is unchanged, and neither caller distinguished that errorfrom the URI-parsing one, so this is not a breaking change.
No new dependency —
utilruntimeis already part ofk8s.io/apimachinery. Therest of the diff is the two tests described below.
Testing
Two regression tests in
kubernetes_sync_test.go:TestConcurrentInit— reproduces the reported crash directly: 16 goroutinescalling
Sync.Initconcurrently. With the fix reverted this reportsDATA RACEon 5 runs out of 5 (65–541 reports per run), pointing at
AddToScheme←Sync.Init; with the fix it passes.make testalready runs-race, so this iscaught in CI.
TestSchemeRegisteredAtPackageInit— asserts the registration happened bypackage-init time, which holds without
-racetoo. It captures the state from aninit()rather than reading it in the test body, becauseSync.Initregistersthe types as a side effect and the pre-existing
TestInitcalls it — read from atest function, the check passes whether or not the fix is present. I verified that
false negative before settling on this shape.
Verified against
main(5c74b83): all three modules build and vet clean;go test -racepasses for./core/...(19 packages), and for the touched packageunder
-count=3and three rounds of-shuffle=on;golangci-lintreports 0 issuesfor it.
Also removed a now-redundant
v1beta1.AddToSchemefromTestNotify— package initcovers it, and it was a second global mutation from a test.
One thing worth your call
While checking this I found that nothing in-tree appears to consume the global
registration at all. With the
init()and thek8s.io/client-go/kubernetes/schemeimport deleted outright, all three modules build and the whole
kubernetespackagetest suite passes (only my new guard test fails, by construction). The real
dynamicclient and
dynamicinformernever touchscheme.Scheme;toFFCfgusesruntime.DefaultUnstructuredConverter; onlydynamic/fakeconsumes a scheme, andthe tests can pass their own.
So deleting the global mutation may be the better fix. I did not propose that here
because
coreis an importable module and dropping a global registration is anobservable change for downstream importers, which felt like your call rather than
mine. Happy to switch this PR to the deletion if you prefer it.