Skip to content

Consider a less racy config system #5298

Open

Description

@calmh

We've had some bugs lately that are caused by races in config handling. As it looks today we have

  • a global config "wrapper" object that is created at startup and always holds the current version of the config
  • config change notifications that some things subscribe to and get old/new config values when it changes.

Some services use just the wrapper, some use just the configs from the notifications, some use both. Services can, depending on how they manage this, be looking at different versions of the config during a change event. For example, when a device is being removed it's possible that one service hasn't processed the change event yet and sees the device as existing, while another service looks directly at the wrapper and sees it as gone.

Looking at the current, global, config is inherently racy.

I think we should have something more transactional, where a config change goes through something like several phases of notifications:

  • ConfigWillChange(old, new) - services should consider whether they are affected and if so cease processing in preparation
  • ApplyConfigChange(old, new) - services act accordingly and apply the new config, starting up again

The guarantee we need here is that all affected services cease processing based on the old config before any service starts acting on the new. The config wrapper must ensure to order config changes properly if several happen concurrently. No-one should be looking at or retaining the global config object - its API should only support subscribing to changes.

(We already have these two phases, we just call them different names and don't have the guarantees above.)

Activity

  1. AudriusButkevicius commented on Jul 22, 2019

    @AudriusButkevicius
    Member

    There are other races.
    A lot of things rely on cfg.Raw(), which is not atomic and might tramp changes of others.
    We should have a generation number in the raw, which cfg.Replace() checks that it matches the existiing version, if it does, updates the config and increments, otherwise aborts returning an error.

    Alternative would be

    cfg.Mutate(func(raw) {
      do stuff
    })
    
  2. imsodin commented on Nov 18, 2019

    @imsodin
    Member

    While trying to harden the effect of config changes to the model along with connections, I quickly realized I was going into the direction of the problem stated in this issue. I now have a prototype of a mixture of the mechanisms described above and wanted to get some feedback before going ahead:

    Basically anyone that subscribes to the config wrapper gets a config.Changer object, which implements all the current config modification methods (SetFolder and so on). That object allows for exactly one change to the config - any further changes will fail. On CommitConfiguration besides the old and new config object the subscribers also get a new config.Changer object.

    And somewhat orthogonally to the above I also intend to remove all the "reading" methods on wrapper, i.e. subscribers need to store any config that they need access to.

    It does nothing about ceasing operation when a config changes is announced and about to happen. And it doesn't try to order concurrent changes. I initially tried to version configs and on concurrent changes try to determine whether the changes conflict - however that's nightmarish.

  3. calmh commented on Nov 18, 2019

    @calmh
    MemberAuthor

    So using an "old" Changer would return something like errConflict and the change would fail? That seems reasonable from one viewpoint, but sounds annoying for things like handling introductions that might fall afoul of that and then ... do what?

  4. AudriusButkevicius commented on Nov 18, 2019

    @AudriusButkevicius
    Member

    The thing we are missing is tracking a version in the config, and busy spinning the modificatiom when our change conflicts, no?

  5. imsodin commented on Nov 19, 2019

    @imsodin
    Member

    Exactly as Jakob said. And resolution strategy would be to wait for the conflicting config update and then go again (aka get the new config and run the function handling introductions again) - might be painful though I agree.

    For Audrius: What are conflicts? My early intentions revolved around that, i.e. version configs and then only let modifications through when concurrently modified versions don't conflict. I just didn't come up with a simple idea to check if config modifications conflict.

  6. AudriusButkevicius commented on Nov 19, 2019

    @AudriusButkevicius
    Member

    I can't imagine how that would work. You'd block services from operating while the transaction is in progress?

    I don't see any point of the config changer, as you can achieve the same thing by keeping the old api and just checking generation numbers.

    I can't imagine how introduction handling would work with cfg.Changer, and how many cycles it would take.

  7. imsodin commented on Nov 20, 2019

    @imsodin
    Member

    I just considered the changer mechanism simpler than passing around some kind of number in both directions (from wrapper to subscribers and from subscribers when passing a change to the wrapper).

    In most places I'd expect it to happen like this (or bail entirely), which is indeed somewhat ugly:

    	for {
    		folder := m.folderCfgs[id]
    		// Do something
    		w, err := m.cfg.SetFolders(folders)
    		if err != config.ErrConflict {
    			w.Wait() // Either waits for the conflicting change to go through
    			break
    		}
    	}

    The prototype interface diff looks like this:

    --- a/lib/config/wrapper.go
    +++ b/lib/config/wrapper.go
    @@ -7,6 +7,7 @@
     package config
     
     import (
    +	"errors"
     	"os"
     	"sync/atomic"
     	"time"
    @@ -19,6 +20,8 @@ import (
     	"github.com/syncthing/syncthing/lib/util"
     )
     
    +var ErrOutdatedConfig = errors.New("this version of the config has already been changed")
    +
     // The Committer interface is implemented by objects that need to know about
     // or have a say in configuration changes.
     //
    @@ -40,10 +43,22 @@ import (
     // capable of doing so.
     type Committer interface {
     	VerifyConfiguration(from, to Configuration) error
    -	CommitConfiguration(from, to Configuration) (handled bool)
    +	CommitConfiguration(from, to Configuration, c Changer) (handled bool)
     	String() string
     }
     
    +type Changer interface {
    +	Replace(cfg Configuration) (Waiter, error)
    +	SetGUI(gui GUIConfiguration) (Waiter, error)
    +	SetOptions(opts OptionsConfiguration) (Waiter, error)
    +	SetFolder(fld FolderConfiguration) (Waiter, error)
    +	RemoveDevice(id protocol.DeviceID) (Waiter, error)
    +	SetDevice(DeviceConfiguration) (Waiter, error)
    +	SetDevices([]DeviceConfiguration) (Waiter, error)
    +	AddOrUpdatePendingDevice(device protocol.DeviceID, name, address string)
    +	AddOrUpdatePendingFolder(id, label string, device protocol.DeviceID)
    +}
    +
     // Waiter allows to wait for the given config operation to complete.
     type Waiter interface {
     	Wait()
    @@ -60,30 +75,21 @@ type Wrapper interface {
     	ConfigPath() string
     
     	RawCopy() Configuration
    -	Replace(cfg Configuration) (Waiter, error)
     	RequiresRestart() bool
     	Save() error
     
     	GUI() GUIConfiguration
    -	SetGUI(gui GUIConfiguration) (Waiter, error)
     	LDAP() LDAPConfiguration
     
     	Options() OptionsConfiguration
    -	SetOptions(opts OptionsConfiguration) (Waiter, error)
     
     	Folder(id string) (FolderConfiguration, bool)
     	Folders() map[string]FolderConfiguration
     	FolderList() []FolderConfiguration
    -	SetFolder(fld FolderConfiguration) (Waiter, error)
     
     	Device(id protocol.DeviceID) (DeviceConfiguration, bool)
     	Devices() map[protocol.DeviceID]DeviceConfiguration
    -	RemoveDevice(id protocol.DeviceID) (Waiter, error)
    -	SetDevice(DeviceConfiguration) (Waiter, error)
    -	SetDevices([]DeviceConfiguration) (Waiter, error)
     
    -	AddOrUpdatePendingDevice(device protocol.DeviceID, name, address string)
    -	AddOrUpdatePendingFolder(id, label string, device protocol.DeviceID)
     	IgnoredDevice(id protocol.DeviceID) bool
     	IgnoredFolder(device protocol.DeviceID, folder string) bool
     
    @@ -91,7 +97,7 @@ type Wrapper interface {
     	GlobalDiscoveryServers() []string
     	StunServers() []string
     
    -	Subscribe(c Committer)
    +	Subscribe(c Committer) (Configuration, Changer)
     	Unsubscribe(c Committer)
     }

    (Most of the remaining methods on Wrapper will be removed too, or moved to be methods of Configuration, to prevent direct, racy access to the wrappers current config).

    Whatever the exact mechanism is, I don't see any solution that both prevents conflicting config changes and doesn't require whoever wants to change the config having to potentially deal with that change being rejected. Ideas as usual very welcome :)

  8. added
    enhancement
    on May 30, 2025
  9. added theissue type on May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions