Don't resurrect zombies.

pull/1/head
Matthew Chen 8 years ago
parent 8f3304ff9d
commit fce52841f9

@ -173,7 +173,8 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
- (void)updateWithMessageState:(TSOutgoingMessageState)messageState
transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithSendingError:(NSError *)error;
- (void)updateWithHasSyncedTranscript:(BOOL)hasSyncedTranscript;
- (void)updateWithHasSyncedTranscript:(BOOL)hasSyncedTranscript
transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithCustomMessage:(NSString *)customMessage transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithCustomMessage:(NSString *)customMessage;
// deliveryTimestamp is an optional parameter, since legacy
@ -196,7 +197,6 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
- (NSUInteger)sentRecipientsCount;
- (BOOL)wasSentToRecipient:(NSString *)contactId;
- (void)updateWithSentRecipient:(NSString *)contactId transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateWithSentRecipient:(NSString *)contactId;
@end

@ -292,13 +292,12 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
}
- (void)updateWithHasSyncedTranscript:(BOOL)hasSyncedTranscript
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestOutgoingMessage:transaction
changeBlock:^(TSOutgoingMessage *message) {
[message setHasSyncedTranscript:hasSyncedTranscript];
}];
}];
[self applyChangeToSelfAndLatestOutgoingMessage:transaction
changeBlock:^(TSOutgoingMessage *message) {
[message setHasSyncedTranscript:hasSyncedTranscript];
}];
}
- (void)updateWithCustomMessage:(NSString *)customMessage transaction:(YapDatabaseReadWriteTransaction *)transaction
@ -421,13 +420,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
}];
}
- (void)updateWithSentRecipient:(NSString *)contactId
{
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self updateWithSentRecipient:contactId transaction:transaction];
}];
}
- (void)updateWithReadRecipientId:(NSString *)recipientId
readTimestamp:(uint64_t)readTimestamp
transaction:(YapDatabaseReadWriteTransaction *)transaction

@ -190,7 +190,12 @@ NSUInteger const OWSSendMessageOperationMaxRetries = 4;
return;
}
[message updateWithMessageState:TSOutgoingMessageStateSentToService];
// Update the message unless it has been deleted.
if ([TSOutgoingMessage fetchObjectWithUniqueID:message.uniqueId]) {
[message updateWithMessageState:TSOutgoingMessageStateSentToService];
} else {
DDLogInfo(@"%@ not marking message sent; message deleted.", strongSelf.logTag);
}
aSuccessHandler();
@ -204,7 +209,12 @@ NSUInteger const OWSSendMessageOperationMaxRetries = 4;
return;
}
[strongSelf.message updateWithSendingError:error];
// Update the message unless it has been deleted.
if ([TSOutgoingMessage fetchObjectWithUniqueID:message.uniqueId]) {
[strongSelf.message updateWithSendingError:error];
} else {
DDLogInfo(@"%@ not marking message failed; message deleted.", strongSelf.logTag);
}
DDLogDebug(@"%@ failed with error: %@", strongSelf.logTag, error);
aFailureHandler(error);
@ -280,6 +290,15 @@ NSUInteger const OWSSendMessageOperationMaxRetries = 4;
- (void)tryWithRemainingRetries:(NSUInteger)remainingRetries
{
// If the message has been deleted, abort send.
if (![TSOutgoingMessage fetchObjectWithUniqueID:self.message.uniqueId]) {
DDLogInfo(@"%@ aborting message send; message deleted.", self.logTag);
NSError *error = OWSErrorWithCodeDescription(
OWSErrorCodeMessageDeletedBeforeSent, @"Message was deleted before it could be sent.");
self.failureHandler(error);
return;
}
// Use this flag to ensure a given operation only succeeds or fails once.
__block BOOL onceFlag = NO;
RetryableFailureHandler retryableFailureHandler = ^(NSError *_Nonnull error) {
@ -429,6 +448,22 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
//
// So we're using YDB behavior to ensure this invariant, which is a bit
// unorthodox.
// Update the message unless it has been deleted.
// __block BOOL isMessageDeleted = NO;
// [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
// if ([TSOutgoingMessage fetchObjectWithUniqueID:message.uniqueId
// transaction:transaction]) {
// [message updateWithMessageState:TSOutgoingMessageStateAttemptingOut transaction:transaction];
// } else {
// DDLogInfo(@"%@ not marking message as sending; message deleted.", self.logTag);
// isMessageDeleted = YES;
// }
// }];
// if (isMessageDeleted) {
// NSError *error = OWSErrorWithCodeDescription(OWSErrorCodeMessageDeletedBeforeSent, @"Message was
// deleted before it could be sent."); failureHandler(error); return;
// }
DDLogError(@"------ TSOutgoingMessageStateAttemptingOut: %@ %@", message.debugDescription, message.description);
[message updateWithMessageState:TSOutgoingMessageStateAttemptingOut];
OWSSendMessageOperation *sendMessageOperation =
@ -704,7 +739,14 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
attempts:OWSMessageSenderRetryAttempts
success:^{
DDLogInfo(@"%@ Marking group message as sent to recipient: %@", self.logTag, recipient.uniqueId);
[message updateWithSentRecipient:recipient.uniqueId];
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
// Update the message unless it has been deleted.
if ([TSOutgoingMessage fetchObjectWithUniqueID:message.uniqueId]) {
[message updateWithSentRecipient:recipient.uniqueId transaction:transaction];
} else {
DDLogInfo(@"%@ not marking message as sent to recipient; message deleted.", self.logTag);
}
}];
[futureSource trySetResult:@1];
}
failure:^(NSError *error) {
@ -1146,7 +1188,14 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
if (message.shouldSyncTranscript) {
// TODO: I suspect we shouldn't optimistically set hasSyncedTranscript.
// We could set this in a success handler for [sendSyncTranscriptForMessage:].
[message updateWithHasSyncedTranscript:YES];
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
// Update the message unless it has been deleted.
if ([TSOutgoingMessage fetchObjectWithUniqueID:message.uniqueId]) {
[message updateWithHasSyncedTranscript:YES transaction:transaction];
} else {
DDLogInfo(@"%@ not marking message as having synced transcript; message deleted.", self.logTag);
}
}];
[self sendSyncTranscriptForMessage:message];
}

@ -26,6 +26,7 @@ typedef NS_ENUM(NSInteger, OWSErrorCode) {
OWSErrorCodeMessageSendNoValidRecipients = 777407,
OWSErrorCodeContactsUpdaterRateLimit = 777408,
OWSErrorCodeCouldNotWriteAttachmentData = 777409,
OWSErrorCodeMessageDeletedBeforeSent = 777410,
};
extern NSError *OWSErrorWithCodeDescription(OWSErrorCode code, NSString *description);

Loading…
Cancel
Save