fix(lxdclient): fix context race condition in ProtocolLXD WithContext - #77
Merged
Merged
Conversation
WithContext was called before APICallMutex.Lock(), causing concurrent goroutines to overwrite each other's context on the shared client. Additionally, context was not reset on error paths, leaving the client with a canceled context for all subsequent calls. Changes: - Move WithContext after Mutex acquisition in all call sites - Add defer reset to context.Background() on all paths - Add Mutex to setLXDHostResourceCache (was missing) - Remove xsemaphore from GetResourceFromLXDWithClient (callers now hold the Mutex and set context) - Add regression tests for Mutex-based concurrency control
3 tasks
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.
Summary
ProtocolLXD.WithContext()was called beforeAPICallMutex.Lock(), causing concurrent goroutines to overwrite each other's context on the shared clientsetLXDHostResourceCachemodifying client context without holding the MutexChanges
server/pkg/api/server_host.go: MoveWithContextafterAPICallMutex.Lock(), adddeferreset on all pathsserver/pkg/lxdclient/resource.go:GetResourceFromLXD: MoveWithContextafter Mutex acquisition +deferresetGetResourceFromLXDWithClient: Removexsemaphoreand internalWithContext/reset (callers now own this responsibility)server/pkg/metric/scrape_lxd.go: AddWithContext+deferreset afterTryLockserver/pkg/resourcecache/resource_cache.go: AddAPICallMutex.Lock()+WithContext+deferresetserver/go.mod: Removexsemaphoredependencyserver/pkg/lxdclient/connect_test.go: Add regression tests for Mutex-based concurrency controlTest Plan
go test ./...— all tests passgo vet ./...— no issuesgo build ./...— build succeedsNotes
context cancelederrors causedno available instancefailures despite available capacity