collector: fix potential forgetting unlock in Persister.Inject #386
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This patch fixes a potential forgetting unlock bug in
Persister.Inject
, which may lead to a deadlock.In src/mongoshake/collector/persister.go,
Function
Inject
callsp.diskQueueMutex.Lock()
but forgetsp.diskQueueMutex.Unlock()
on a specific branch.MongoShake/src/mongoshake/collector/persister.go
Lines 157 to 168 in 313fc4e
Inject
is called bynext
in syncer.go, andnext
is called multiple times bypoll
.So it is possible that
p.disQueueMutex.Lock()
is acquired twice, leading to a deadlock.MongoShake/src/mongoshake/collector/syncer.go
Line 486 in 313fc4e
MongoShake/src/mongoshake/collector/syncer.go
Lines 443 to 451 in 313fc4e
Fix
The fix is to add a
defer p.diskQueueMutex.Unlock()
right afterp.diskQueueMutex.Lock()
. This wayUnlock
is always called as long asLock
is called, even if there is a panic (LOG.Crashf).Suggestions
One interesting thing is that there is a unit testing
TestInject
in src/mongoshake/collector/persister_test.go.But it fails to detect this potential deadlock because of the limited configurations.
e.g.
MongoShake/src/mongoshake/collector/persister_test.go
Line 36 in 313fc4e
So
p.enableDiskPersist
will always be false and theLock
branch will never be taken.MongoShake/src/mongoshake/collector/persister.go
Lines 52 to 53 in 313fc4e
I suggest changing the configurations to cover the
Lock
branch.Though unlikely, if leaving out
Unlock
is the intention of the developers (e.g., for misconfiguration detection), I suggest adding error messages and doc to better explain this.