Repository navigation
Release the write lock before invoking set.value hooks - #225
Merged
Merged
Conversation
inhere
added a commit
that referenced
this pull request
Oct 23, 2026
The hook is fired after the data lock is released (#225), so a hook that reads the whole data set - eg: SaveFileOnSet() calls DumpToFile - races with another goroutine calling Set: go test -race reports a data race on c.data, and Go may even abort with 'concurrent map read and map write'. - add an outer hookMu that serializes the data change with its hook. the hook still runs after the data lock is released, so the getter methods do not deadlock, but no other Set/SetData/ClearData can change the data while the hook is running. - the lock order is hookMu -> lock, and ClearData now swaps the data map under the data lock. - extract the locked set logic to Config.setValue(). - document the hook contract on HookFunc: use the getters, do not call Set/SetData/ClearData in a hook, the data is stable while the hook runs. Tests: TestSetHookKeepsDataStable (another writer must wait for a running hook) and TestSetHookCanDumpAllData (hook dumps the data while two goroutines Set).
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.
A
set.valuehook that callscfg.String(key)deadlocks: Set invokes the hook while still holding the write lock, and the getter waits for a read lock.Register the hook before the deferred unlock so the lock is released before the callback runs. A bounded regression checks hooks reading updated top-level, nested, and literal dotted keys.
Validation:
go test ./...,go vet ./..., andgo test -race . -run TestSetHookCanReadUpdatedValue -count=5pass on macOS/arm64 with Go 1.26.5. All three new cases time out before the fix.AI assistance: Codex helped prepare the code and tests