Skip to content

lib/model: Hold locks as shortly as possible - #5975

Closed
imsodin wants to merge 2 commits into
syncthing:masterfrom
imsodin:model/allTheLocksMaybe
Closed

imsodin wants to merge 2 commits into
syncthing:masterfrom
imsodin:model/allTheLocksMaybe

Conversation

@imsodin

@imsodin imsodin commented Aug 22, 2019

Copy link
Copy Markdown
Member

After Moisie reported some deadlock false positives (https://forum.syncthing.net/t/web-based-syncthing-not-showing-info/13529/37) I had another look at all the fmut/pmut locks in model. I found some more cases where a lock could be replaced by a read-lock and calling functions could be moved outside of the lock. The bigger change was getting rid of the add-/start-/tearDownFolderLocked functions respectively move the locking inside those functions. We already did release the lock inside two of those functions, so there's arguably no point in doing so and now I limited the locking even further.

Unit and integration tests passed.

Comment thread lib/model/model.go
defer m.fmut.Unlock()

if sr, ok := m.deviceStatRefs[deviceID]; ok {
m.fmut.RLock()

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.

This is wrong as concurrent calls might both conclude there is no ref and create a new one, clobbering each other. The original is correct.

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.

Right. My beaf with this is that an informational call can block everything else due to taking that lock. In df1ffe6 I moved the creation of deviceStatRefs into CommitConfiguration such that we only require read-locks here.

Comment thread lib/model/model.go

m.pmut.Lock()
if oldConn, ok := m.conn[deviceID]; ok {
m.pmut.RLock()

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 both the new and old code is incorrect here. If there is an old connection we will attempt to close it. But since we don't hold the lock over the close, and don't remove the connection from m.conn we can have two concurrent calls both seeing the old conn and closing it, then setting the new conn with the other call clobbering it...

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.

In principle yes, in practice this should be prevented by AddConnection being called from a single loop in lib/service, thus this cannot be called concurrently.

Comment thread lib/model/model.go
tokens := m.folderRunnerTokens[cfg.ID]

m.fmut.Unlock()
m.fmut.RUnlock()

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.

Here also I think both old and new is broken. Some rethinking required. :/

@imsodin

imsodin commented Aug 30, 2019

Copy link
Copy Markdown
Member Author

I should have mentioned this in the PR description: I considered fmut/pmut solely as protections, not as gate-keepers for correct execution order/prevent unwanted parallelism and tried to figure out whether or not such gate-keeping becomes necessary by my changes.
E.g. tearDownFolder is only called from CommitConfiguration, which I assumed to not be called concurrently, but that's not the case currently (#5298). However I think the right thing to do then is to protect that from being called concurrently, because that's definitely bad (we want the latest changes to take effect). Simplest thing would be just a commitConfiguration mutex (thought that wouldn't guarantee ordering).

@calmh

calmh commented Sep 9, 2019

Copy link
Copy Markdown
Member

I don't trust this PR enough to merge it. Some things are clearly fine, but other things aren't (clearly fine) and we have no testing in place to verify in either direction. We can assume that some things aren't called concurrently but that's something that will come back and bite us in the ass. I'd rather assume that things are called concurrently and have the current locking in place to assure correctness. At least that's the intention, although as we can see in some places it still isn't fully correct today...

@calmh

calmh commented Oct 16, 2019

Copy link
Copy Markdown
Member

I think this is dead as-is and has been superseded in the uncontroversial parts. Hence closing, yell if I'm wrong.

@calmh calmh closed this Oct 16, 2019
@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 Oct 16, 2020
@syncthing syncthing locked and limited conversation to collaborators Oct 16, 2020
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.

3 participants