Repository navigation
lib/config: Copy inputs and copy config outputs #5069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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()) | ||
| } | ||
|
|
||
| func (w *Wrapper) replaceLocked(to Configuration) (Waiter, error) { | ||
|
|
@@ -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 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't verification be read only, thus copying unnecessary?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "should". Better safe than sorry.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is just a single implementation of |
||
| l.Debugln(sub, "rejected config:", err) | ||
| return noopWaiter{}, err | ||
| } | ||
|
|
@@ -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) | ||
| } | ||
|
|
@@ -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 | ||
|
|
@@ -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() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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, | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a concrete scenario, Both auto accepting happening in |
||
| } | ||
|
|
||
| newCfg.Folders = append(newCfg.Folders, fld) | ||
|
|
||
| return w.replaceLocked(newCfg) | ||
| } | ||
|
|
@@ -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() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same about copying arguments. |
||
| return w.replaceLocked(newCfg) | ||
| } | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, shouldn't this already have been racy before?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should have, but just fixing what I found.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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...) | ||
|
|
@@ -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...) | ||
|
|
||
There was a problem hiding this comment.
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
replaceLockedare redundant.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, as
tois not copied when stored onw.cfg, so the called still can fiddle with original slices passed inwrapper.Replace(cfg)