Fix possible deadlock in CurrentChatUser
functions
#2074
Merged
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.
馃敆 Issue Links
https://getstream.slack.com/archives/CRB9P6L03/p1654667559058219
馃幆 Goal
A customer has reported that calling
addDevice
causes a deadlock馃摑 Summary
Calling
addDevice
twice, once from background queue and once from main queue causes deadlock:background queue -> acquires the lock (semaphore), waits on main queue because of performAndWait
main queue -> waits on the lock, cannot perform any work
馃洜 Implementation
EntityDatabaseObserver
usesviewContext
for its DB operations, both in tests and production code. This is because it needs to be performant.When you try to access
EntityDatabaseObserver.item
from background queue, since it's anAtomic
, it acquires the lock & callsviewContext.performAndWait
(to convert DTO to Model) which waits for main queue to complete some workIf you try to access
EntityDatabaseObserver.item
from main queue, it'll wait on the lock... which is held by background queue, which is waiting on main queue (because ofperformAndWait
), which is waiting on background queue to release the lock, which is waiting on main queue to complete the work, which is waiting on background queue to release the lock......We cannot "fix" the bug in
EntityDatabaseObserver
right away, it's complicated. But the bug happens because we accesscurrentUser
, which iscurrentUserObserver.item
in theCurrentUserController
. This causes DB operations, which is not required, we already have O(1) access tocurrentUserId
, which is all we need.The patch in this PR will mitigate this issue for now.
馃И Manual Testing Notes
Paste this in
EntityDatabaseObserver_Tests
鈽戯笍 Contributor Checklist