Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Avoid deadlock when marking self-sent messages as read.
Browse files Browse the repository at this point in the history
Fetching the localNumber invokes another transaction, which can
result in deadlock if called during an existing transaction.

// FREEBIE
  • Loading branch information
michaelkirk committed Feb 8, 2017
1 parent 4d05575 commit e194989
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Messages/TSMessagesManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ - (TSIncomingMessage *)handleReceivedEnvelope:(OWSSignalServiceProtosEnvelope *)
__block TSIncomingMessage *_Nullable incomingMessage;
__block TSThread *thread;

// Do this outside of a transaction to avoid deadlock
OWSAssert([TSAccountManager isRegistered]);
NSString *localNumber = [TSAccountManager localNumber];

[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
if (groupId) {
NSMutableArray *uniqueMemberIds = [[[NSSet setWithArray:dataMessage.group.members] allObjects] mutableCopy];
Expand Down Expand Up @@ -634,8 +638,7 @@ - (TSIncomingMessage *)handleReceivedEnvelope:(OWSSignalServiceProtosEnvelope *)

// Any messages sent from the current user - from this device or another - should be
// automatically marked as read.
OWSAssert([TSAccountManager isRegistered]);
BOOL shouldMarkMessageAsRead = [envelope.source isEqualToString:[TSAccountManager localNumber]];
BOOL shouldMarkMessageAsRead = [envelope.source isEqualToString:localNumber];
if (shouldMarkMessageAsRead) {
[incomingMessage markAsReadLocallyWithTransaction:transaction];
}
Expand Down

0 comments on commit e194989

Please sign in to comment.