Fix unsynchronized map write in plugin Manager.RemoveUser#1005
Open
Osamaali313 wants to merge 1 commit into
Open
Fix unsynchronized map write in plugin Manager.RemoveUser#1005Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
`Manager.RemoveUser` deletes from `m.instances` without holding `m.mutex`:
delete(m.instances, pluginConf.ID)
Every other access to `m.instances` is synchronized — `Instance` reads it
under `m.mutex.RLock()`, and the writes in `InitializeForUserID` /
`initializeSingleUserPlugin` happen under `m.mutex.Lock()`. The adjacent
`inst.Disable()` call in this same loop is even wrapped in Lock/Unlock, so
only the map delete is left unguarded.
`RemoveUser` is registered as the `OnUserDeleted` callback (router.go), fired
when an admin deletes a user, while any authenticated request to the plugin
API (`GET /plugin`, `/plugin/:id/*`) concurrently reads `m.instances` via
`Instance`. A `RLock` reader is not protected against a writer that never
takes the mutex, so this races and triggers Go's runtime-fatal
"concurrent map read and map write", crashing the whole server (a fatal that
gin.Recovery cannot recover). Take the write lock around the delete, matching
every other access.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1005 +/- ##
==========================================
+ Coverage 74.54% 74.56% +0.01%
==========================================
Files 66 66
Lines 3485 3487 +2
==========================================
+ Hits 2598 2600 +2
Misses 688 688
Partials 199 199 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Problem
Manager.RemoveUser(plugin/manager.go) deletes from them.instancesmap without holdingm.mutex:Every other access to
m.instancesis synchronized:Instance()reads it underm.mutex.RLock().InitializeForUserID/initializeSingleUserPluginhappen underm.mutex.Lock().The author even wraps the adjacent
inst.Disable()inLock/Unlock— only this map delete is left unguarded.Impact
RemoveUseris registered as theOnUserDeletedcallback (router.go:userChangeNotifier.OnUserDeleted(pluginManager.RemoveUser)), fired when an admin deletes a user (DELETE /user/:id). Concurrently, any authenticated request to the plugin API (GET /plugin,GET/POST /plugin/:id/*) readsm.instancesviaInstance()underRLock. AnRLockreader is not protected against a writer that never takes the mutex, so an admin deleting a user (who has plugin configs) while a client polls plugins races the map.That triggers Go's runtime-fatal
concurrent map read and map write— a fatal error thatgin.Recovery()cannot recover, crashing the whole server process and dropping all WebSocket streams and in-flight requests.Reproduction
A minimal program mirroring the exact pattern (an
RLock-guarded read in one goroutine vs. an unguardeddeletein another) reliably fatals:Fix
Take the write lock around the delete, matching every other
m.instancesaccess: