Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (cfg Configuration) Copy() Configuration {
}

newCfg.Options = cfg.Options.Copy()
newCfg.GUI = cfg.GUI.Copy()

// DeviceIDs are values
newCfg.IgnoredDevices = make([]protocol.DeviceID, len(cfg.IgnoredDevices))
Expand Down
4 changes: 4 additions & 0 deletions lib/config/guiconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ func (c GUIConfiguration) IsValidAPIKey(apiKey string) bool {
return false
}
}

func (c GUIConfiguration) Copy() GUIConfiguration {
return c
}
59 changes: 27 additions & 32 deletions lib/config/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (w *Wrapper) RawCopy() Configuration {
func (w *Wrapper) Replace(cfg Configuration) (Waiter, error) {
w.mut.Lock()
defer w.mut.Unlock()
return w.replaceLocked(cfg)
return w.replaceLocked(cfg.Copy())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this copy and the one in replaceLocked are redundant.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, as to is not copied when stored on w.cfg, so the called still can fiddle with original slices passed in wrapper.Replace(cfg)

}

func (w *Wrapper) replaceLocked(to Configuration) (Waiter, error) {
Expand All @@ -154,7 +154,7 @@ func (w *Wrapper) replaceLocked(to Configuration) (Waiter, error) {

for _, sub := range w.subs {
l.Debugln(sub, "verifying configuration")
if err := sub.VerifyConfiguration(from, to); err != nil {
if err := sub.VerifyConfiguration(from.Copy(), to.Copy()); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't verification be read only, thus copying unnecessary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"should". Better safe than sorry.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this actually causes the bug, as somewhere the verification builds some alternative representation of the folder slice to check some existance and screws something up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is just a single implementation of Committer.VerifyConfiguration that does not just return nil - the apiService, which calls to.GUI.Address(). Modifying the config in a verification function would simply be wrong, no gray area there. However there's not much to argue against "better safe than sorry",

l.Debugln(sub, "rejected config:", err)
return noopWaiter{}, err
}
Expand All @@ -164,15 +164,15 @@ func (w *Wrapper) replaceLocked(to Configuration) (Waiter, error) {
w.deviceMap = nil
w.folderMap = nil

return w.notifyListeners(from, to), nil
return w.notifyListeners(from.Copy(), to.Copy()), nil
}

func (w *Wrapper) notifyListeners(from, to Configuration) Waiter {
wg := sync.NewWaitGroup()
wg.Add(len(w.subs))
for _, sub := range w.subs {
go func(commiter Committer) {
w.notifyListener(commiter, from.Copy(), to.Copy())
w.notifyListener(commiter, from, to)
wg.Done()
}(sub)
}
Expand All @@ -187,15 +187,14 @@ func (w *Wrapper) notifyListener(sub Committer, from, to Configuration) {
}
}

// Devices returns a map of devices. Device structures should not be changed,
// other than for the purpose of updating via SetDevice().
// Devices returns a map of devices.
func (w *Wrapper) Devices() map[protocol.DeviceID]DeviceConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
if w.deviceMap == nil {
w.deviceMap = make(map[protocol.DeviceID]DeviceConfiguration, len(w.cfg.Devices))
for _, dev := range w.cfg.Devices {
w.deviceMap[dev.DeviceID] = dev
w.deviceMap[dev.DeviceID] = dev.Copy()
}
}
return w.deviceMap
Expand All @@ -213,13 +212,13 @@ func (w *Wrapper) SetDevices(devs []DeviceConfiguration) (Waiter, error) {
replaced = false
for newIndex := range newCfg.Devices {
if newCfg.Devices[newIndex].DeviceID == devs[oldIndex].DeviceID {
newCfg.Devices[newIndex] = devs[oldIndex]
newCfg.Devices[newIndex] = devs[oldIndex].Copy()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the point of generally being safe on struct/type members, but on function arguments it seems over-cautious? If it must not be modified, the caller should do the copying. And anyway, all these arguments originate from the wrapper (unless they are newly created, which is also fine), which now returns copies - so we should already be safe.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of agree, but the fact there is a bug somewhere here implies something's fucky, so I'd rather be overly cautious and make sure we do the right thing, in case the caller somehow manages to fiddle with this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think we needn't argue much, as we are actually on the same page - your just looking at it from the perspective of squashing a nasty bug, while I am in the easy situation of looking at it "academically" for review. I don't think this is a bottleneck even in setups that reach to the boundaries of scalability of Sycnthing.

replaced = true
break
}
}
if !replaced {
newCfg.Devices = append(newCfg.Devices, devs[oldIndex])
newCfg.Devices = append(newCfg.Devices, devs[oldIndex].Copy())
}
}

Expand All @@ -238,19 +237,14 @@ func (w *Wrapper) RemoveDevice(id protocol.DeviceID) (Waiter, error) {
defer w.mut.Unlock()

newCfg := w.cfg.Copy()
removed := false
for i := range newCfg.Devices {
if newCfg.Devices[i].DeviceID == id {
newCfg.Devices = append(newCfg.Devices[:i], newCfg.Devices[i+1:]...)
removed = true
break
return w.replaceLocked(newCfg)
}
}
if !removed {
return noopWaiter{}, nil
}

return w.replaceLocked(newCfg)
return noopWaiter{}, nil
}

// Folders returns a map of folders. Folder structures should not be changed,
Expand All @@ -261,7 +255,7 @@ func (w *Wrapper) Folders() map[string]FolderConfiguration {
if w.folderMap == nil {
w.folderMap = make(map[string]FolderConfiguration, len(w.cfg.Folders))
for _, fld := range w.cfg.Folders {
w.folderMap[fld.ID] = fld
w.folderMap[fld.ID] = fld.Copy()
}
}
return w.folderMap
Expand All @@ -271,7 +265,7 @@ func (w *Wrapper) Folders() map[string]FolderConfiguration {
func (w *Wrapper) FolderList() []FolderConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return append([]FolderConfiguration(nil), w.cfg.Folders...)
return w.cfg.Copy().Folders
}

// SetFolder adds a new folder to the configuration, or overwrites an existing
Expand All @@ -281,17 +275,15 @@ func (w *Wrapper) SetFolder(fld FolderConfiguration) (Waiter, error) {
defer w.mut.Unlock()

newCfg := w.cfg.Copy()
replaced := false

for i := range newCfg.Folders {
if newCfg.Folders[i].ID == fld.ID {
newCfg.Folders[i] = fld
replaced = true
break
return w.replaceLocked(newCfg)
}
}
if !replaced {
newCfg.Folders = append(w.cfg.Folders, fld)

@AudriusButkevicius AudriusButkevicius Jul 17, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So after a bunch of bisecting, this seems to be an issue, essentially appending old folders to a new copy, yet I don't understand why this could be an issue, as all of this happens under a lock.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just shooting from the hip here, but could there be an issue when two folders are added in rapid successions, such as from the same cluster config. That is, if the folder list is initially A, B and we append to A, B, C but this doesn't get propagated and the next folder added gets appended to A, B as A, B, D - The "C" folder is now mysteriously gone and bad things happen...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a concrete scenario, Both auto accepting happening in ClusterConfig and CommitConfiguration happen without lock on fmut. And CommitConfiguration is called in a goroutine, i.e. the wrapper lock won't be held while the model is running it. So in principle it might be possible for CommitConfiguration removing/adding folders at the same time as ClusterConfig does.

}

newCfg.Folders = append(newCfg.Folders, fld)

return w.replaceLocked(newCfg)
}
Expand All @@ -300,31 +292,31 @@ func (w *Wrapper) SetFolder(fld FolderConfiguration) (Waiter, error) {
func (w *Wrapper) Options() OptionsConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.Options
return w.cfg.Options.Copy()
}

// SetOptions replaces the current options configuration object.
func (w *Wrapper) SetOptions(opts OptionsConfiguration) (Waiter, error) {
w.mut.Lock()
defer w.mut.Unlock()
newCfg := w.cfg.Copy()
newCfg.Options = opts
newCfg.Options = opts.Copy()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about copying arguments.

return w.replaceLocked(newCfg)
}

// GUI returns the current GUI configuration object.
func (w *Wrapper) GUI() GUIConfiguration {
w.mut.Lock()
defer w.mut.Unlock()
return w.cfg.GUI
return w.cfg.GUI.Copy()
}

// SetGUI replaces the current GUI configuration object.
func (w *Wrapper) SetGUI(gui GUIConfiguration) (Waiter, error) {
w.mut.Lock()
defer w.mut.Unlock()
newCfg := w.cfg.Copy()
newCfg.GUI = gui
newCfg.GUI = gui.Copy()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same about copying arguments.

return w.replaceLocked(newCfg)
}

Expand Down Expand Up @@ -360,7 +352,7 @@ func (w *Wrapper) Device(id protocol.DeviceID) (DeviceConfiguration, bool) {
defer w.mut.Unlock()
for _, device := range w.cfg.Devices {
if device.DeviceID == id {
return device, true
return device.Copy(), true
}
}
return DeviceConfiguration{}, false
Expand All @@ -372,14 +364,17 @@ func (w *Wrapper) Folder(id string) (FolderConfiguration, bool) {
defer w.mut.Unlock()
for _, folder := range w.cfg.Folders {
if folder.ID == id {
return folder, true
return folder.Copy(), true
}
}
return FolderConfiguration{}, false
}

// Save writes the configuration to disk, and generates a ConfigSaved event.
func (w *Wrapper) Save() error {
w.mut.Lock()
defer w.mut.Unlock()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, shouldn't this already have been racy before?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should have, but just fixing what I found.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not complaining, just wondering.


fd, err := osutil.CreateAtomic(w.path)
if err != nil {
l.Debugln("CreateAtomic:", err)
Expand All @@ -403,7 +398,7 @@ func (w *Wrapper) Save() error {

func (w *Wrapper) GlobalDiscoveryServers() []string {
var servers []string
for _, srv := range w.cfg.Options.GlobalAnnServers {
for _, srv := range w.Options().GlobalAnnServers {
switch srv {
case "default":
servers = append(servers, DefaultDiscoveryServers...)
Expand All @@ -420,7 +415,7 @@ func (w *Wrapper) GlobalDiscoveryServers() []string {

func (w *Wrapper) ListenAddresses() []string {
var addresses []string
for _, addr := range w.cfg.Options.ListenAddresses {
for _, addr := range w.Options().ListenAddresses {
switch addr {
case "default":
addresses = append(addresses, DefaultListenAddresses...)
Expand Down
29 changes: 29 additions & 0 deletions lib/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,35 @@ func TestIssue4897(t *testing.T) {
}
}

func TestIssue5063(t *testing.T) {
wcfg, m := newState(defaultAutoAcceptCfg)

addAndVerify := func(wg *sync.WaitGroup) {
id := srand.String(8)
m.ClusterConfig(device1, protocol.ClusterConfig{
Folders: []protocol.Folder{
{
ID: id,
Label: id,
},
},
})
os.RemoveAll(filepath.Join("testdata", id))
wg.Done()
if fcfg, ok := wcfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("expected shared", id)
}
}

wg := &sync.WaitGroup{}
for i := 0; i <= 10; i++ {
wg.Add(1)
go addAndVerify(wg)
}

wg.Wait()
}

func TestAutoAcceptRejected(t *testing.T) {
// Nothing happens if AutoAcceptFolders not set
tcfg := defaultAutoAcceptCfg.Copy()
Expand Down