fix(state): guard StateEventChans iteration with StateEventChansLock#2775
Open
Aamod007 wants to merge 1 commit into
Open
fix(state): guard StateEventChans iteration with StateEventChansLock#2775Aamod007 wants to merge 1 commit into
Aamod007 wants to merge 1 commit into
Conversation
PushContainerEvent, PushNodeEvent and PushNamespaceEvent iterate sa.StateEventChans without holding StateEventChansLock, while addStateEventChan/removeStateEventChan mutate the map and close the channels under the lock on every WatchState client connect/disconnect. Since the runtime handlers push events from separate goroutines (go dm.StateAgent.PushContainerEvent(...)), a client disconnecting mid-broadcast triggers either a concurrent map iteration/write fatal error or a send on closed channel panic, crashing the whole daemon. Take a read lock around the broadcast loops so channels cannot be removed or closed while an event is being fanned out. Add a regression test that fails under -race (and panics) without this fix. Signed-off-by: Aamod007 <aamodkumar2006@gmail.com>
4fa3057 to
c6b6370
Compare
vee1e
reviewed
Jul 15, 2026
| default: | ||
| } | ||
| sa.removeStateEventChan(uid) | ||
| _ = i |
There was a problem hiding this comment.
here i is already used in the for loop, so the compiler doesn't consider it unused, making the assignment pointless. It neither suppresses a compiler error nor does anything at runtime. i know its pedantic but still worth pointing out dead code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose of PR?:
Fixes #2774
PushContainerEvent,PushNodeEventandPushNamespaceEventinKubeArmor/state/data.goiteratesa.StateEventChanswithout holdingStateEventChansLock, whileaddStateEventChan/removeStateEventChanmutate the map and close the channels under that lock on everyWatchStateclient connect/disconnect. Since the runtime handlers push events from separate goroutines (go dm.StateAgent.PushContainerEvent(...)), a client disconnecting mid-broadcast crashes the daemon withfatal error: concurrent map iteration and map writeorpanic: send on closed channel.This PR takes
StateEventChansLock.RLock()around the broadcast loop in all three functions. The sends are already non-blocking (select/default), so holding the read lock during fan-out is cheap, and becauseremoveStateEventChancloses channels under the write lock, the read lock also rules out the send-on-closed-channel panic.Does this PR introduce a breaking change?
No.
If the changes in this PR are manually verified, list down the scenarios covered::
TestPushContainerEventRacewhich simulatesWatchStateclients connecting/disconnecting concurrently with container event pushes. On currentmainit fails undergo test -race(data race betweenPushContainerEventandremoveStateEventChan) and also panics withsend on closed channel. With this fix,go test -race -count=1 -run TestPushContainerEventRace ./state/passes.go vet ./state/clean.Additional information for reviewer? :
Failure output on
mainwithout the fix:Checklist:
<type>(<scope>): <subject>