Skip to content

fix: clear read caches when setting config values - #229

Merged
inhere merged 2 commits into
gookit:masterfrom
Ekko82:fix/invalidate-cache-on-write
Oct 24, 2026
Merged

inhere merged 2 commits into
gookit:masterfrom
Ekko82:fix/invalidate-cache-on-write

Conversation

@Ekko82

@Ekko82 Ekko82 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

When EnableCache is enabled, Set and SetData leave 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.

@inhere
inhere merged commit c4d38e6 into gookit:master Oct 24, 2026
11 checks passed
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants