forked from tangled.org/core
this repo has no description

appview/pages: fix concurrent write in dev mode

When in dev mode. Concurrent writes to the template map will cause a
concurrent write panic and crash appview. Quickly moving between pages
can trigger this crash. This is addressed by adding a RWMutex on the
templates. RWMutex allows multiple readers, but only one writer.

Signed-off-by: Ciel <jamie@longest.voyage>

Changed files
+11 -1
appview
pages
+11 -1
appview/pages/pages.go
···
"os"
"path/filepath"
"strings"
"tangled.sh/tangled.sh/core/appview/commitverify"
"tangled.sh/tangled.sh/core/appview/config"
···
var Files embed.FS
type Pages struct {
-
t map[string]*template.Template
avatar config.AvatarConfig
dev bool
embedFS embed.FS
···
}
p := &Pages{
t: make(map[string]*template.Template),
dev: config.Core.Dev,
avatar: config.Avatar,
···
}
log.Printf("total templates loaded: %d", len(templates))
p.t = templates
}
···
}
// Update the template in the map
p.t[name] = tmpl
log.Printf("template reloaded from disk: %s", name)
return nil
···
}
}
tmpl, exists := p.t[templateName]
if !exists {
return fmt.Errorf("template not found: %s", templateName)
···
"os"
"path/filepath"
"strings"
+
"sync"
"tangled.sh/tangled.sh/core/appview/commitverify"
"tangled.sh/tangled.sh/core/appview/config"
···
var Files embed.FS
type Pages struct {
+
mu sync.RWMutex
+
t map[string]*template.Template
+
avatar config.AvatarConfig
dev bool
embedFS embed.FS
···
}
p := &Pages{
+
mu: sync.RWMutex{},
t: make(map[string]*template.Template),
dev: config.Core.Dev,
avatar: config.Avatar,
···
}
log.Printf("total templates loaded: %d", len(templates))
+
p.mu.Lock()
+
defer p.mu.Unlock()
p.t = templates
}
···
}
// Update the template in the map
+
p.mu.Lock()
+
defer p.mu.Unlock()
p.t[name] = tmpl
log.Printf("template reloaded from disk: %s", name)
return nil
···
}
}
+
p.mu.RLock()
+
defer p.mu.RUnlock()
tmpl, exists := p.t[templateName]
if !exists {
return fmt.Errorf("template not found: %s", templateName)