Repository navigation
fix(package): [concurrency] hold the map lock while taking the try lock - #417
Merged
Merged
Conversation
duke-git
approved these changes
Nov 9, 2026
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.
TryKeyedLocker.TryLocklooks up or creates the key'scasMutexwhile holdingl.mu, releasesl.mu, and only then callslock.TryLock().Unlockremoves the map entry when the lock is free. IfUnlockruns between the lookup and the CAS, the goroutine inTryLocktakes a lock that is no longer in the map, and a laterTryLockfor the same key creates a new entry and also succeeds, so two goroutines hold the same key. The race detector also reports the unsynchronized read of the lock word inUnlockagainst the CAS outside the mutex.TryLocknow keepsl.muheld (withdefer) throughlock.TryLock(). The CAS does not block, so no waiting is added, and a failed try leaves the state unchanged.Unlockis unchanged.Test:
TestTryKeyedLocker_MutualExclusionUnderContentionruns 8 goroutines x 20000 iterations ofTryLock, count holders,Unlockon one key and fails if a second holder is seen. It is a stress test, so a pass is not proof that the race is absent. On go1.26.6 Linux, on the base commit the test failed in 5 of 5 runs and the race detector flagged the base as well. With the change, 5 of 5 runs and-count=30pass,go test -race ./concurrencypasses, and vet and a 386 build and run pass. The change makes the critical section one CAS longer and I did not benchmark it. The fullgo test ./...still fails innetutil(TestTelnetConnected,ExampleIsTelnetConnected) the same way on the base commit in my environment, which has no external network access. I did not investigate the cause.Not changed:
KeyedLockerandRWKeyedLocker. In particularKeyedLocker.Dostill waits forfnto finish in its deferred release when the context has already expired and no other caller holds the key.