Skip to content

lib/config: Copy inputs and copy config outputs - #5069

Merged
calmh merged 4 commits into
syncthing:masterfrom
AudriusButkevicius:copy-copy
Jul 26, 2018
Merged

calmh merged 4 commits into
syncthing:masterfrom
AudriusButkevicius:copy-copy

Conversation

@AudriusButkevicius

@AudriusButkevicius AudriusButkevicius commented Jul 16, 2018 •

Copy link
Copy Markdown
Member

This is a guess as to what could be the cause for #5063.

In the last debug logs the relevant part was:

[GXVUP] 22:26:43 INFO: Existing folder default false
[GXVUP] 22:26:43 INFO: Existing folder gdrxc-frazc false
[GXVUP] 22:26:43 INFO: Existing folder jjnhx-nhqzq false
[GXVUP] 22:26:43 INFO: Missing folder ewa2m-zcpje

// CommitConfiguration in the model coming in:

[GXVUP] 22:26:43 INFO: from folder map[
default:...
ewa2m-zcpje:...
gdrxc-frazc:...


[GXVUP] 22:26:43 INFO: to folder map[
ewa2m-zcpje:...
gdrxc-frazc:...
jjnhx-nhqzq:...
default:...

[GXVUP] 22:26:43 INFO: Adding folder "test1" (jjnhx-nhqzq)

So ewa2m-zcpje was introduced, but it somehow overwrote an existing folders spot in the config.

@imsodin

imsodin commented Jul 17, 2018 •

Copy link
Copy Markdown
Member

Do you know/have an idea where the problem occurs specifically? The diff looks a bit like a shotgun approach at first glance. Not so much after the second glance, see review.

Comment thread lib/config/wrapper.go
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",

Comment thread lib/config/wrapper.go
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.

Comment thread lib/config/wrapper.go Outdated
w.mut.Lock()
defer w.mut.Unlock()
return append([]FolderConfiguration(nil), w.cfg.Folders...)
return append([]FolderConfiguration(nil), w.cfg.Copy().Folders...)

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.

Appending to a nil slice already ensures we return a copy.

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 does not ensure the slices within the folder structs are copied correctly.

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.

Oh right, good one. My money is on this as error source :)

Comment thread lib/config/wrapper.go Outdated
newCfg.Folders[i] = fld
replaced = true
break
newCfg.Folders[i] = fld.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.

Comment thread lib/config/wrapper.go
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.

Comment thread lib/config/wrapper.go
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.

Comment thread lib/config/wrapper.go
// 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.

Comment thread lib/config/wrapper.go
}
}
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.

@calmh

calmh commented Jul 20, 2018

Copy link
Copy Markdown
Member

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...

@AudriusButkevicius

Copy link
Copy Markdown
Member Author

So the only thing is perhaps sensible to revert is copying inputs.
Other than that I think other changes make sense.

@AudriusButkevicius

AudriusButkevicius commented Jul 21, 2018 •

Copy link
Copy Markdown
Member Author

Can we quantify what we want undone?

@imsodin

imsodin commented Jul 21, 2018

Copy link
Copy Markdown
Member

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 ;)

@AudriusButkevicius

AudriusButkevicius commented Jul 21, 2018 •

Copy link
Copy Markdown
Member Author

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.

@imsodin

imsodin commented Jul 21, 2018 •

Copy link
Copy Markdown
Member

So I know that there is only a single line that fixes the issue

I missed that one, would you mind pointing it out explicitly (again?) to me?

EDIT:
This: #5069 (comment)

@AudriusButkevicius

Copy link
Copy Markdown
Member Author

See last comments.

@calmh

calmh commented Jul 22, 2018 •

Copy link
Copy Markdown
Member

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:

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())
wg.Done()
}(sub)
}
return wg
}

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...

@AudriusButkevicius

Copy link
Copy Markdown
Member Author

So I did reproduce it. I'll try running the tests with race.

@calmh

calmh commented Jul 22, 2018 •

Copy link
Copy Markdown
Member

No, here's the problem:

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

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.

@calmh

calmh commented Jul 22, 2018

Copy link
Copy Markdown
Member

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 fld.Copy() that you also added. So I agree - that line is the problem, but no need to copy the folderconfig once more.

@AudriusButkevicius

Copy link
Copy Markdown
Member Author

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.

@calmh

calmh commented Jul 22, 2018

Copy link
Copy Markdown
Member

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.

@imsodin

imsodin commented Jul 22, 2018

Copy link
Copy Markdown
Member

[...] 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 [...]

Wouldn't that include quite a few of Audrius added copies, like https://github.com/syncthing/syncthing/pull/5069/files#diff-5d899bb376d9455c8659d63b8b210d53R191 ?

@AudriusButkevicius

Copy link
Copy Markdown
Member Author

So what do we do with this.
I look at most changes, and to me they make sense, such as Folders() returning copies, etc, etc.
I kind of agree with undoing copying inputs I guess.

@calmh

calmh commented Jul 22, 2018

Copy link
Copy Markdown
Member

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.

@AudriusButkevicius

Copy link
Copy Markdown
Member Author

Anything else left here?

@imsodin imsodin left a comment

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.

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.

Comment thread lib/config/wrapper.go
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)

@calmh

calmh commented Jul 26, 2018

Copy link
Copy Markdown
Member

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.

@calmh
calmh merged commit 5161f03 into syncthing:master Jul 26, 2018
@AudriusButkevicius
AudriusButkevicius deleted the copy-copy branch July 26, 2018 21:50
@calmh calmh added this to the v0.14.50 milestone Jul 27, 2018
@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Jul 27, 2019
@syncthing syncthing locked and limited conversation to collaborators Jul 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants