Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(daemon): add reload guard and atomic config writes

yushen12dec9f12234
diff --git a/server/internal/cli/config.go b/server/internal/cli/config.go
index ba3225df76..ceaffb34d7 100644
--- a/server/internal/cli/config.go
+++ b/server/internal/cli/config.go
@@ -75,21 +75,43 @@ func LoadCLIConfig() (CLIConfig, error) {
return cfg, nil
}
-// SaveCLIConfig writes the CLI config to disk.
+// SaveCLIConfig writes the CLI config to disk atomically (write to temp, then rename).
func SaveCLIConfig(cfg CLIConfig) error {
path, err := CLIConfigPath()
if err != nil {
return err
}
- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ dir := filepath.Dir(path)
+ if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("create CLI config directory: %w", err)
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("encode CLI config: %w", err)
}
- if err := os.WriteFile(path, append(data, '\n'), 0o600); err != nil {
- return fmt.Errorf("write CLI config: %w", err)
+
+ // Write to a temp file in the same directory, then rename for atomicity.
+ tmp, err := os.CreateTemp(dir, ".config-*.json.tmp")
+ if err != nil {
+ return fmt.Errorf("create temp config file: %w", err)
+ }
+ tmpPath := tmp.Name()
+ if _, err := tmp.Write(append(data, '\n')); err != nil {
+ tmp.Close()
+ os.Remove(tmpPath)
+ return fmt.Errorf("write temp config file: %w", err)
+ }
+ if err := tmp.Close(); err != nil {
+ os.Remove(tmpPath)
+ return fmt.Errorf("close temp config file: %w", err)
+ }
+ if err := os.Chmod(tmpPath, 0o600); err != nil {
+ os.Remove(tmpPath)
+ return fmt.Errorf("chmod temp config file: %w", err)
+ }
+ if err := os.Rename(tmpPath, path); err != nil {
+ os.Remove(tmpPath)
+ return fmt.Errorf("rename config file: %w", err)
}
return nil
}
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.go
index 208a72129b..f6b51b1f66 100644
--- a/server/internal/daemon/daemon.go
+++ b/server/internal/daemon/daemon.go
@@ -28,6 +28,7 @@ type Daemon struct {
mu sync.Mutex
workspaces map[string]*workspaceState
+ reloading sync.Mutex // prevents concurrent reloadWorkspaces
}
// New creates a new Daemon instance.
@@ -196,6 +197,9 @@ func (d *Daemon) configWatchLoop(ctx context.Context) {
// reloadWorkspaces reconciles the active workspace set with the config file.
func (d *Daemon) reloadWorkspaces(ctx context.Context) {
+ d.reloading.Lock()
+ defer d.reloading.Unlock()
+
cfg, err := cli.LoadCLIConfig()
if err != nil {
d.logger.Warn("reload config failed", "error", err)