Repository navigation
Description
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 preparationApplyConfigChange(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
AudriusButkevicius commentedon Jul 22, 2019
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
imsodin commentedon Nov 18, 2019
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.Changerobject, which implements all the current config modification methods (SetFolderand so on). That object allows for exactly one change to the config - any further changes will fail. OnCommitConfigurationbesides the old and new config object the subscribers also get a newconfig.Changerobject.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.
calmh commentedon Nov 18, 2019
So using an "old"
Changerwould return something likeerrConflictand 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?AudriusButkevicius commentedon Nov 18, 2019
The thing we are missing is tracking a version in the config, and busy spinning the modificatiom when our change conflicts, no?
imsodin commentedon Nov 19, 2019
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.
AudriusButkevicius commentedon Nov 19, 2019
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.
imsodin commentedon Nov 20, 2019
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:
The prototype interface diff looks like this:
(Most of the remaining methods on
Wrapperwill be removed too, or moved to be methods ofConfiguration, 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 :)
all: Remove config reading through wrapper (ref syncthing#5298)
lib: Apply config changes sequentially (ref syncthing#5298)
lib: Apply config changes sequentially (ref syncthing#5298)
lib: Apply config changes sequentially (ref syncthing#5298)
lib: Apply config changes sequentially (ref syncthing#5298)
lib: Apply config changes sequentially (ref syncthing#5298)
lib: Apply config changes sequentially (ref #5298) (#7188)