Skip to content

fix: guard ContainerManager.active map with a mutex - #10049

Open
alliasgher wants to merge 1 commit into
GoogleContainerTools:mainfrom
alliasgher:fix-container-manager-race
Open

fix: guard ContainerManager.active map with a mutex#10049
alliasgher wants to merge 1 commit into
GoogleContainerTools:mainfrom
alliasgher:fix-container-manager-race

Conversation

@alliasgher

Copy link
Copy Markdown

Fixes #9081. ContainerManager.active is a plain map accessed from the goroutine in Start (via checkPod) without synchronisation, causing a fatal "concurrent map read and map write" crash. Add a sync.Mutex and lock around the map access.

@alliasgher
alliasgher requested a review from a team as a code owner April 14, 2026 09:35

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request introduces a mutex to the ContainerManager to synchronize access to the active map within the checkPod method. While this addresses potential concurrency issues, the reviewer pointed out that holding the lock while calling external notification functions could lead to contention or blocking, suggesting a refactor to minimize the lock's scope.

Comment on lines +122 to +123
d.mu.Lock()
defer d.mu.Unlock()

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.

medium

Holding the mutex while calling external notification functions (such as notifyDebuggingContainerStarted and debuggingContainerStartedV2) is generally discouraged. These functions eventually send events over channels, which could block if the channel is full or the consumer is slow. Holding the lock during these operations can lead to increased contention and potentially stall the pod watcher goroutine.

Consider refactoring the loop to collect the necessary information for notifications into a local slice, then dispatching the notifications after releasing the lock.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That makes sense. Alternatively, the mutex could be locked and unlocked inside the loop just for the duration of the map access, but then defer can't be used as easily and the mutex may get acquired and released many times, which may impact performance negatively.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

So is current implementation fine?

Comment thread pkg/skaffold/kubernetes/debugging/container_manager.go Outdated
ContainerManager.active is a plain map accessed from the goroutine in
Start (via checkPod) without synchronisation. Under concurrent pod
events this causes a fatal "concurrent map read and map write" crash.

Add a sync.Mutex and lock it around the map access in checkPod.

Fixes GoogleContainerTools#9081

Signed-off-by: alliasgher <alliasgher123@gmail.com>
@alliasgher
alliasgher force-pushed the fix-container-manager-race branch from 9ecee1d to 77c556b Compare April 14, 2026 09:46
@alliasgher
alliasgher requested a review from mrwonko April 14, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"concurrent map read and map write" crash in kubernetes ContainerManager.active

2 participants