Repository navigation
lib/config: Copy inputs and copy config outputs - #5069
Conversation
|
Do you know/have an idea where the problem occurs specifically? |
| 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 { |
There was a problem hiding this comment.
Shouldn't verification be read only, thus copying unnecessary?
There was a problem hiding this comment.
"should". Better safe than sorry.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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",
| for newIndex := range newCfg.Devices { | ||
| if newCfg.Devices[newIndex].DeviceID == devs[oldIndex].DeviceID { | ||
| newCfg.Devices[newIndex] = devs[oldIndex] | ||
| newCfg.Devices[newIndex] = devs[oldIndex].Copy() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| w.mut.Lock() | ||
| defer w.mut.Unlock() | ||
| return append([]FolderConfiguration(nil), w.cfg.Folders...) | ||
| return append([]FolderConfiguration(nil), w.cfg.Copy().Folders...) |
There was a problem hiding this comment.
Appending to a nil slice already ensures we return a copy.
There was a problem hiding this comment.
It does not ensure the slices within the folder structs are copied correctly.
There was a problem hiding this comment.
Oh right, good one. My money is on this as error source :)
| newCfg.Folders[i] = fld | ||
| replaced = true | ||
| break | ||
| newCfg.Folders[i] = fld.Copy() |
| defer w.mut.Unlock() | ||
| newCfg := w.cfg.Copy() | ||
| newCfg.Options = opts | ||
| newCfg.Options = opts.Copy() |
| defer w.mut.Unlock() | ||
| newCfg := w.cfg.Copy() | ||
| newCfg.GUI = gui | ||
| newCfg.GUI = gui.Copy() |
| // Save writes the configuration to disk, and generates a ConfigSaved event. | ||
| func (w *Wrapper) Save() error { | ||
| w.mut.Lock() | ||
| defer w.mut.Unlock() |
There was a problem hiding this comment.
Wait, shouldn't this already have been racy before?
There was a problem hiding this comment.
It should have, but just fixing what I found.
There was a problem hiding this comment.
Not complaining, just wondering.
| } | ||
| } | ||
| if !replaced { | ||
| newCfg.Folders = append(w.cfg.Folders, fld) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
|
Yeah I'm not super happy about adding a bunch of copying everywhere because we're not sure if or when the config is changed. That seems like a problem we should solve at the root... |
|
So the only thing is perhaps sensible to revert is copying inputs. |
|
Can we quantify what we want undone? |
|
Personally, I'd temporarily undo everything except the "fixes" and improvements and copying when returning config elements, then ask @pitlochry again for a test. But obviously I don't know what @calmh has in mind ;) |
|
So I know that there is only a single line that fixes the issue, yet I think copying in most cases is the sensible thing todo, because FolderMap() should never return references to the orignal folders, nor should we risk folks modifying things in VerifyConfiguration, nor should we rely on folks not modifying slices after they did SetFolder etc. With copies the behaviour is more resilient. |
I missed that one, would you mind pointing it out explicitly (again?) to me? EDIT: |
|
See last comments. |
|
I don't understand how the proposed diff could possibly fix the problem, other than as a voodoo side effect by affecting timing or something. In model.CommitConfiguration it's looking at "from" and "to" and drawing the conclusion that "from" lacks a folder that's clearly present from before, at device startup even (in the last panic log on the issue, which must have auto accept enabled despite the commentary). This means that the Folders slice in "from" is broken, somehow. I don't see anywhere we modify a Folders slice without copying it first. The one thing that worries me a little is notifyListeners: syncthing/lib/config/wrapper.go Lines 170 to 180 in b83c5b3 The copying of "from" and "to" happens outside of the lock, because it's concurrent in a newly spawned goroutine that runs in the future. Yet, I don't see how "from" or the Folders slice inside it could ever be modified, even if we return from replaceLocked and those copies happen later... It would be nice if this could be reproduced with a race detector build though to see if it has anything to say. I can't reproduce this myself so far... |
|
So I did reproduce it. I'll try running the tests with race. |
|
No, here's the problem: syncthing/lib/config/wrapper.go Line 293 in b83c5b3 That appends to the original slice and not the copy, making the slice not a copy anymore and forgetting previous changes to the copy, etc. |
|
So I see now that you already found and fixed that, but I didn't see that part of the diff and thought you were talking about the |
|
So I think other changes don't do damage? We should also probably copy from and to prior to passing things to the new routines. |
|
I think the other changes are voodoo and muddy the waters. The point of the copies is not to prevent modifications by the caller, but to give them something that doesn't change under their feet if there's a second config commit while they are looking at the first. I agree we should copy before firing the routines though. |
Wouldn't that include quite a few of Audrius added copies, like https://github.com/syncthing/syncthing/pull/5069/files#diff-5d899bb376d9455c8659d63b8b210d53R191 ? |
|
So what do we do with this. |
|
I started marking up all the changes with yes/no IMHO, but it boils down to "yes, copy when we're returning a slice or map of something that the caller can reasonably expect to be the owner of" and no in the other cases. |
|
Anything else left here? |
imsodin
left a comment
There was a problem hiding this comment.
All the "set"-functions still do copy the argument, except for SetFolder. I think the last consensus was to not copy arguments, just return values.
| w.mut.Lock() | ||
| defer w.mut.Unlock() | ||
| return w.replaceLocked(cfg) | ||
| return w.replaceLocked(cfg.Copy()) |
There was a problem hiding this comment.
I think this copy and the one in replaceLocked are redundant.
There was a problem hiding this comment.
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)
|
Practically this is all fine, philosophically I think it's dirty to add copies everywhere to protect against some theoretical fiddling coder boogeyman. It's not like we usually worry about this whenever we pass a slice of something to somewhere. But it solves the bug (which is not due to too few calls to Copy()...) and I'll leave the potential cleanup as a battle for another day. |
This is a guess as to what could be the cause for #5063.
In the last debug logs the relevant part was:
So
ewa2m-zcpjewas introduced, but it somehow overwrote an existing folders spot in the config.