Move socket clean inside operation queue

Summary: Set delegate and close inside the operation's queue as to make it safer i.e. all socket related operations are done inside the queue.

Reviewed By: ivanmisuno

Differential Revision: D47124235

fbshipit-source-id: 48b53db1cd47d017a26186a156046ba68fe358b7
This commit is contained in:
Lorenzo Blasa
2023-06-29 12:40:09 -07:00
committed by Facebook GitHub Bot
parent 14068f1ea8
commit 4ac755370d
2 changed files with 14 additions and 13 deletions

View File

@@ -171,16 +171,21 @@ static constexpr int connectionKeepaliveSeconds = 10;
}
_keepAlive = nil;
if (_socket) {
// Clear the socket delegate before close. Ensures that we won't get
// any messages after the disconnect takes place.
[_socket setDelegate:nil];
[_socket close];
_socket = nil;
};
__weak auto weakSelf = self;
NSBlockOperation* disconnectOperation =
[NSBlockOperation blockOperationWithBlock:^{
__strong auto strongSelf = weakSelf;
// Clear the socket delegate before close. Ensures that we won't get
// any messages after the disconnect takes place.
if (strongSelf->_socket) {
[strongSelf->_socket setDelegate:nil];
[strongSelf->_socket close];
[_dispatchQueue cancelAllOperations];
[_dispatchQueue waitUntilAllOperationsAreFinished];
strongSelf->_socket = nil;
}
}];
[_dispatchQueue addOperations:@[ disconnectOperation ] waitUntilFinished:YES];
}
- (void)send:(NSString*)message