From d6d48f2866c2b307d439f5edfbfaacf6b3a45929 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 1 Aug 2024 12:25:10 +0200 Subject: [PATCH] Make common.UserConfig an atomic.Pointer for safe concurrent access Currently, userConfig is only read once at startup and then never changes. Later in this branch, we will add the possibility to reload the user config; this can happen either when switching repositories, or when the user has edited the config file. In both cases, reloading happens on the main thread, but the user config could be accessed concurrently from background threads, so we need to make this safe. --- pkg/common/common.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/common/common.go b/pkg/common/common.go index 2a18cf0e1..837af7bea 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -1,6 +1,8 @@ package common import ( + "sync/atomic" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/sirupsen/logrus" @@ -11,7 +13,7 @@ import ( type Common struct { Log *logrus.Entry Tr *i18n.TranslationSet - userConfig *config.UserConfig + userConfig atomic.Pointer[config.UserConfig] AppState *config.AppState Debug bool // for interacting with the filesystem. We use afero rather than the default @@ -20,9 +22,9 @@ type Common struct { } func (c *Common) UserConfig() *config.UserConfig { - return c.userConfig + return c.userConfig.Load() } func (c *Common) SetUserConfig(userConfig *config.UserConfig) { - c.userConfig = userConfig + c.userConfig.Store(userConfig) }