Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect entryCache with lock #262

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/yang/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,18 @@ func ToEntry(n Node) (e *Entry) {
}
}
ms := RootNode(n).Modules
if e := ms.entryCache[n]; e != nil {

// Protect the entryCache map from concurrent access.
ms.entryCacheMu.RLock()
e = ms.entryCache[n]
ms.entryCacheMu.RUnlock()
if e != nil {
return e
}
defer func() {
ms.entryCacheMu.Lock()
ms.entryCache[n] = e
ms.entryCacheMu.Unlock()
wenovus marked this conversation as resolved.
Show resolved Hide resolved
}()

// Copy in the extensions from our Node, if any.
Expand Down
17 changes: 10 additions & 7 deletions pkg/yang/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (
// Modules contains information about all the top level modules and
// submodules that are read into it via its Read method.
type Modules struct {
Modules map[string]*Module // All "module" nodes
SubModules map[string]*Module // All "submodule" nodes
includes map[*Module]bool // Modules we have already done include on
nsMu sync.Mutex // nsMu protects the byNS map.
byNS map[string]*Module // Cache of namespace lookup
typeDict *typeDictionary // Cache for type definitions.
Modules map[string]*Module // All "module" nodes
SubModules map[string]*Module // All "submodule" nodes
includes map[*Module]bool // Modules we have already done include on
nsMu sync.Mutex // nsMu protects the byNS map.
byNS map[string]*Module // Cache of namespace lookup
typeDict *typeDictionary // Cache for type definitions.
entryCacheMu sync.RWMutex // entryCacheMu protects the entryCache map.
// entryCache is used to prevent unnecessary recursion into previously
// converted nodes.
entryCache map[Node]*Entry
Expand Down Expand Up @@ -323,7 +324,7 @@ func (ms *Modules) Process() []error {
// Reset globals that may remain stale if multiple Process() calls are
// made by the same caller.
ms.mergedSubmodule = map[string]bool{}
ms.entryCache = map[Node]*Entry{}
ms.ClearEntryCache()

errs := ms.process()
if len(errs) > 0 {
Expand Down Expand Up @@ -446,5 +447,7 @@ func (ms *Modules) include(m *Module) error {
// ClearEntryCache clears the entryCache containing previously converted nodes
// used by the ToEntry function.
func (ms *Modules) ClearEntryCache() {
ms.entryCacheMu.Lock()
defer ms.entryCacheMu.Unlock()
ms.entryCache = map[Node]*Entry{}
}
Loading