From 61fa281894ed2ef9c5384116211d4983e5b6791d Mon Sep 17 00:00:00 2001 From: "L. Yeung" Date: Sat, 20 May 2023 10:46:50 +0800 Subject: [PATCH] fix(platform): avoid duplicate creations of template cache --- src/platform/shell.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/platform/shell.go b/src/platform/shell.go index 18ab923b..bda7534d 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -190,6 +190,7 @@ type TemplateCache struct { SHLVL int Segments SegmentsCache + initialized bool sync.RWMutex } @@ -312,6 +313,7 @@ func (env *Shell) Init() { env.cmdCache = &commandCache{ commands: NewConcurrentMap(), } + env.tmplCache = &TemplateCache{} env.SetPromptCount() } @@ -776,6 +778,7 @@ func (env *Shell) LoadTemplateCache() { env.Error(err) return } + templateCache.initialized = true env.tmplCache = &templateCache } @@ -785,19 +788,21 @@ func (env *Shell) Logs() string { func (env *Shell) TemplateCache() *TemplateCache { defer env.Trace(time.Now()) - if env.tmplCache != nil { - return env.tmplCache + tmplCache := env.tmplCache + tmplCache.Lock() + defer tmplCache.Unlock() + + if tmplCache.initialized { + return tmplCache } - tmplCache := &TemplateCache{ - Root: env.Root(), - Shell: env.Shell(), - ShellVersion: env.CmdFlags.ShellVersion, - Code: env.ErrorCode(), - WSL: env.IsWsl(), - Segments: make(map[string]interface{}), - PromptCount: env.CmdFlags.PromptCount, - } + tmplCache.Root = env.Root() + tmplCache.Shell = env.Shell() + tmplCache.ShellVersion = env.CmdFlags.ShellVersion + tmplCache.Code = env.ErrorCode() + tmplCache.WSL = env.IsWsl() + tmplCache.Segments = make(map[string]interface{}) + tmplCache.PromptCount = env.CmdFlags.PromptCount tmplCache.Env = make(map[string]string) tmplCache.Var = make(map[string]interface{}) @@ -838,7 +843,7 @@ func (env *Shell) TemplateCache() *TemplateCache { tmplCache.SHLVL = shlvl } - env.tmplCache = tmplCache + tmplCache.initialized = true return tmplCache }