From bb38fce54e9eef4f1b4b5b009be282094fa5e35d Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 6 Apr 2017 19:23:55 -0400 Subject: [PATCH] Ensure that deleting sessions happens on session queue // FREEBIE --- ...SInvalidIdentityKeyReceivingErrorMessage.m | 38 +++++++++++-------- .../TSInvalidIdentityKeySendingErrorMessage.m | 10 +++-- src/Messages/TSMessagesManager.m | 4 +- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeyReceivingErrorMessage.m b/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeyReceivingErrorMessage.m index a2354377a..511761736 100644 --- a/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeyReceivingErrorMessage.m +++ b/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeyReceivingErrorMessage.m @@ -1,5 +1,6 @@ -// Created by Frederic Jacobs on 31/12/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// #import "TSInvalidIdentityKeyReceivingErrorMessage.h" #import "OWSFingerprint.h" @@ -78,20 +79,25 @@ NS_ASSUME_NONNULL_BEGIN return; } - [[TSStorageManager sharedManager] saveRemoteIdentity:newKey recipientId:self.envelope.source]; - - // Decrypt this and any old messages for the newly accepted key - NSArray *messagesToDecrypt = - [self.thread receivedMessagesForInvalidKey:newKey]; - - for (TSInvalidIdentityKeyReceivingErrorMessage *errorMessage in messagesToDecrypt) { - [[TSMessagesManager sharedManager] handleReceivedEnvelope:errorMessage.envelope]; - - // Here we remove the existing error message because handleReceivedEnvelope will either - // 1.) succeed and create a new successful message in the thread or... - // 2.) fail and create a new identical error message in the thread. - [errorMessage remove]; - } + // Saving a new identity mutates the session store so it must happen on the sessionStoreQueue + dispatch_async([OWSDispatch sessionStoreQueue], ^{ + [[TSStorageManager sharedManager] saveRemoteIdentity:newKey recipientId:self.envelope.source]; + + dispatch_async(dispatch_get_main_queue(), ^{ + // Decrypt this and any old messages for the newly accepted key + NSArray *messagesToDecrypt = + [self.thread receivedMessagesForInvalidKey:newKey]; + + for (TSInvalidIdentityKeyReceivingErrorMessage *errorMessage in messagesToDecrypt) { + [[TSMessagesManager sharedManager] handleReceivedEnvelope:errorMessage.envelope]; + + // Here we remove the existing error message because handleReceivedEnvelope will either + // 1.) succeed and create a new successful message in the thread or... + // 2.) fail and create a new identical error message in the thread. + [errorMessage remove]; + } + }); + }); } - (NSData *)newIdentityKey diff --git a/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.m b/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.m index 26629baba..10d3ed7b1 100644 --- a/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.m +++ b/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.m @@ -1,5 +1,6 @@ -// Created by Frederic Jacobs on 15/02/15. -// Copyright (c) 2015 Open Whisper Systems. All rights reserved. +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// #import "TSInvalidIdentityKeySendingErrorMessage.h" #import "OWSFingerprint.h" @@ -56,7 +57,10 @@ NSString *TSInvalidRecipientKey = @"TSInvalidRecipientKey"; - (void)acceptNewIdentityKey { - [[TSStorageManager sharedManager] saveRemoteIdentity:self.newIdentityKey recipientId:self.recipientId]; + // Saving a new identity mutates the session store so it must happen on the sessionStoreQueue + dispatch_async([OWSDispatch sessionStoreQueue], ^{ + [[TSStorageManager sharedManager] saveRemoteIdentity:self.newIdentityKey recipientId:self.recipientId]; + }); } - (NSData *)newIdentityKey diff --git a/src/Messages/TSMessagesManager.m b/src/Messages/TSMessagesManager.m index e08946a40..c9681bf29 100644 --- a/src/Messages/TSMessagesManager.m +++ b/src/Messages/TSMessagesManager.m @@ -581,7 +581,9 @@ NS_ASSUME_NONNULL_BEGIN } }]; - [[TSStorageManager sharedManager] deleteAllSessionsForContact:endSessionEnvelope.source]; + dispatch_async([OWSDispatch sessionStoreQueue], ^{ + [[TSStorageManager sharedManager] deleteAllSessionsForContact:endSessionEnvelope.source]; + }); } - (void)handleExpirationTimerUpdateMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope