Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct crashing issue caused by locking on a object which is replaced inside the lock #220

Merged
merged 1 commit into from
Oct 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions MQTTClient/MQTTClient/MQTTSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ @interface MQTTSessionManager()

@property (strong, nonatomic) NSDictionary<NSString *, NSNumber *> *internalSubscriptions;
@property (strong, nonatomic) NSDictionary<NSString *, NSNumber *> *effectiveSubscriptions;
@property (strong, nonatomic) NSLock *subscriptionLock;

@end

Expand Down Expand Up @@ -110,6 +111,7 @@ - (id)init {
name:UIApplicationDidBecomeActiveNotification
object:nil];
#endif
self.subscriptionLock = [[NSLock alloc] init];
return self;
}

Expand Down Expand Up @@ -422,21 +424,21 @@ - (void)newMessage:(MQTTSession *)session data:(NSData *)data onTopic:(NSString
- (void)connected:(MQTTSession *)session sessionPresent:(BOOL)sessionPresent {
if (self.clean || !self.reconnectFlag || !sessionPresent) {
NSDictionary *subscriptions = [self.internalSubscriptions copy];
@synchronized(self.effectiveSubscriptions) {
self.effectiveSubscriptions = [[NSMutableDictionary alloc] init];
}
[self.subscriptionLock lock];
self.effectiveSubscriptions = [[NSMutableDictionary alloc] init];
[self.subscriptionLock unlock];
if (subscriptions.count) {
[self.session subscribeToTopics:subscriptions subscribeHandler:^(NSError *error, NSArray<NSNumber *> *gQoss) {
if (!error) {
NSArray<NSString *> *allTopics = subscriptions.allKeys;
for (int i = 0; i < allTopics.count; i++) {
NSString *topic = allTopics[i];
NSNumber *gQos = gQoss[i];
@synchronized(self.effectiveSubscriptions) {
NSMutableDictionary *newEffectiveSubscriptions = [self.subscriptions mutableCopy];
[newEffectiveSubscriptions setObject:gQos forKey:topic];
self.effectiveSubscriptions = newEffectiveSubscriptions;
}
[self.subscriptionLock lock];
NSMutableDictionary *newEffectiveSubscriptions = [self.subscriptions mutableCopy];
[newEffectiveSubscriptions setObject:gQos forKey:topic];
self.effectiveSubscriptions = newEffectiveSubscriptions;
[self.subscriptionLock unlock];
}
}
}];
Expand Down Expand Up @@ -497,11 +499,11 @@ - (void)setSubscriptions:(NSDictionary<NSString *, NSNumber *> *)newSubscription
if (![newSubscriptions objectForKey:topicFilter]) {
[self.session unsubscribeTopic:topicFilter unsubscribeHandler:^(NSError *error) {
if (!error) {
@synchronized(self.effectiveSubscriptions) {
NSMutableDictionary *newEffectiveSubscriptions = [self.subscriptions mutableCopy];
[newEffectiveSubscriptions removeObjectForKey:topicFilter];
self.effectiveSubscriptions = newEffectiveSubscriptions;
}
[self.subscriptionLock lock];
NSMutableDictionary *newEffectiveSubscriptions = [self.subscriptions mutableCopy];
[newEffectiveSubscriptions removeObjectForKey:topicFilter];
self.effectiveSubscriptions = newEffectiveSubscriptions;
[self.subscriptionLock unlock];
}
}];
}
Expand All @@ -514,11 +516,11 @@ - (void)setSubscriptions:(NSDictionary<NSString *, NSNumber *> *)newSubscription
[self.session subscribeToTopic:topicFilter atLevel:qos subscribeHandler:^(NSError *error, NSArray<NSNumber *> *gQoss) {
if (!error) {
NSNumber *gQos = gQoss[0];
@synchronized(self.effectiveSubscriptions) {
NSMutableDictionary *newEffectiveSubscriptions = [self.subscriptions mutableCopy];
[newEffectiveSubscriptions setObject:gQos forKey:topicFilter];
self.effectiveSubscriptions = newEffectiveSubscriptions;
}
[self.subscriptionLock lock];
NSMutableDictionary *newEffectiveSubscriptions = [self.subscriptions mutableCopy];
[newEffectiveSubscriptions setObject:gQos forKey:topicFilter];
self.effectiveSubscriptions = newEffectiveSubscriptions;
[self.subscriptionLock unlock];
}
}];
}
Expand Down