Repository navigation
fix: clear read caches when setting config values - #229
Merged
Merged
Conversation
inhere
added a commit
that referenced
this pull request
Oct 24, 2026
The load paths wrote c.data without any lock, and fired the load.data/reload.data hooks outside the lock, so a hook that reads the whole data set raced with other writers and with the readers. The read caches had the same problem: they were read and written without a lock, and only Set/SetData cleared them(see #229). - add Config.writeAndHook(): lock order hookMu -> lock, the data is changed under the write lock, the read caches are cleared, and then the hook is fired after the data lock is released. all the write APIs go through it: Set, SetData, ClearData, LoadData, LoadSMap, LoadSources, LoadStrings, LoadFiles, LoadExists, LoadFilesByFilter, LoadRemote, LoadFilesByFormat, LoadExistsByFormat, LoadFromDir. ReloadFiles keeps its own variant, which does not fire OnReloadData on an error. - the file and remote reading runs before the lock is taken, and the user FileFilterFn of LoadFilesByFilter is called without the lock. - one OnLoadData event per public load call, the internal loadFile/parseSourceCode/ loadDataMap/loadSMap no longer fire hooks. - LoadOSEnv, LoadOSEnvs, LoadOSEnvByFilter and LoadFlags still load through Set, and fire OnLoadData via the new fireHookScoped(). LoadOSEnvByFilter did not fire OnLoadData at all, it does now. - the read caches are guarded by cacheMu with a cache generation check: a value read before a data change can not be stored into the cache after it. ClearCaches() is unconditional now, so a stale cache can not survive a runtime option change. - ClearData() fires OnCleanData after the data is cleared, it was fired before. Tests: the load hook sees the new value, the load hook keeps the data stable, the cache is invalidated by LoadData/LoadStrings/ClearData, and two -race concurrency tests(String vs Set with EnableCache, String vs LoadData).
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.
When
EnableCacheis enabled,SetandSetDataleave cached strings, slices and maps unchanged. Reading a value before updating it can therefore make later reads return the old value, including from change hooks.Clear the existing read caches after updating the data and before calling hooks. Regression tests cover scalar and collection values, parent/child paths, replacing or clearing data, and hook reads.
Validation:
go test -cover ./...,go build ./..., and the YAML example pass on Windows amd64 with Go 1.27.1. Staticcheck and Revive report the same findings as the upstream baseline.