fix(config): prevent late option registration from losing saved values

Config options registered after loadConfig() runs were silently
discarded — ReplaceConfig() only applies disk values to already-known
options, so any module registering in Start() rather than New() would
always reset to its DefaultValue on every restart.

Changes:
- base/notifications: move prep()/registerConfig() from Start() to
  New(), ensuring all options exist before loadConfig() runs
- base/config: introduce cfgInitialized flag, set unconditionally in
  start() before loadConfig()
- base/config: add guard in Register() that rejects registrations
  after config is initialized, making the violation explicit

Fixes: core/useSystemNotifications always restoring to true despite
being saved as false in config.json

https://github.com/safing/portmaster/issues/2020
This commit is contained in:
Alexandr Stelnykovych
2026-03-12 16:57:42 +02:00
parent 39a0035c2a
commit 8f9dcd5924
3 changed files with 15 additions and 4 deletions
+7
View File
@@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"sort"
"sync/atomic"
"github.com/safing/portmaster/base/utils/debug"
"github.com/safing/portmaster/service/mgr"
@@ -32,6 +33,9 @@ func prep() error {
return registerBasicOptions()
}
// true - when config is initialized (from disk). So no new options can be registered anymore.
var cfgInitialized atomic.Bool
func start() error {
configFilePath = filepath.Join(module.instance.DataDir(), "config.json")
@@ -46,6 +50,9 @@ func start() error {
return err
}
// mark config as loaded, so that options can react on this in their registration and prevent registration of options after loading.
cfgInitialized.Store(true)
err = loadConfig(false)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to load config file: %w", err)
+4
View File
@@ -62,6 +62,7 @@ func GetOption(name string) (*Option, error) {
}
// Register registers a new configuration option.
// Note: Option must be registered before initial loading config from disk.
func Register(option *Option) error {
if option.Name == "" {
return fmt.Errorf("failed to register option: please set option.Name")
@@ -75,6 +76,9 @@ func Register(option *Option) error {
if option.OptType == 0 {
return fmt.Errorf("failed to register option: please set option.OptType")
}
if cfgInitialized.Load() {
return fmt.Errorf("cannot register option '%s': config already initialized", option.Key)
}
if option.ValidationRegex == "" && option.PossibleValues != nil {
values := make([]string, len(option.PossibleValues))
+4 -4
View File
@@ -25,10 +25,6 @@ func (n *Notifications) States() *mgr.StateMgr {
}
func (n *Notifications) Start() error {
if err := prep(); err != nil {
return err
}
return start()
}
@@ -146,6 +142,10 @@ func New(instance instance) (*Notifications, error) {
states: mgr.NewStateMgr(m),
}
if err := prep(); err != nil {
return nil, err
}
return module, nil
}