From 2cd5aa8c0a559503812f0d30d1bf0f4a9e56a664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szyszkowski?= Date: Thu, 4 Nov 2021 18:09:22 +0100 Subject: [PATCH 1/4] Recreate ARTDataEncoder on ARTChannel options update --- Source/ARTChannel.m | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Source/ARTChannel.m b/Source/ARTChannel.m index 924d12bc8..8fdff9282 100644 --- a/Source/ARTChannel.m +++ b/Source/ARTChannel.m @@ -46,6 +46,7 @@ - (ARTChannelOptions *)options_nosync { - (void)setOptions:(ARTChannelOptions *)options { dispatch_sync(_queue, ^{ [self setOptions_nosync:options]; + [self recreateDataEncoderWith:options.cipher]; }); } @@ -53,6 +54,16 @@ - (void)setOptions_nosync:(ARTChannelOptions *)options { _options = options; } +- (void)recreateDataEncoderWith:(ARTCipherParams*)cipher { + NSError *error = nil; + _dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:cipher error:&error]; + + if (error != nil) { + [_logger warn:@"creating ARTDataEncoder: %@", error]; + _dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:nil error:nil]; + } +} + - (void)publish:(NSString *)name data:(id)data { [self publish:name data:data callback:nil]; } From d370c106619089cc78298c51fef8bc265c2bb58b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szyszkowski?= Date: Thu, 4 Nov 2021 18:51:55 +0100 Subject: [PATCH 2/4] Move `recreateDataEncoderWith` method call to `setOptions_nosync` body `setOptions_nosync` is called from other classes that inherit `ARTChannel` class --- Source/ARTChannel.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/ARTChannel.m b/Source/ARTChannel.m index 8fdff9282..a3e3f3957 100644 --- a/Source/ARTChannel.m +++ b/Source/ARTChannel.m @@ -46,12 +46,12 @@ - (ARTChannelOptions *)options_nosync { - (void)setOptions:(ARTChannelOptions *)options { dispatch_sync(_queue, ^{ [self setOptions_nosync:options]; - [self recreateDataEncoderWith:options.cipher]; }); } - (void)setOptions_nosync:(ARTChannelOptions *)options { _options = options; + [self recreateDataEncoderWith:options.cipher]; } - (void)recreateDataEncoderWith:(ARTCipherParams*)cipher { From 0a7fd0c8fa7cf40897a6b671ac27c7dad43c2c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szyszkowski?= Date: Fri, 5 Nov 2021 08:48:56 +0100 Subject: [PATCH 3/4] Wrap encoder recreation in `barrier` block to avoid race condition --- Source/ARTChannel.m | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Source/ARTChannel.m b/Source/ARTChannel.m index a3e3f3957..f3f0b244f 100644 --- a/Source/ARTChannel.m +++ b/Source/ARTChannel.m @@ -55,13 +55,15 @@ - (void)setOptions_nosync:(ARTChannelOptions *)options { } - (void)recreateDataEncoderWith:(ARTCipherParams*)cipher { - NSError *error = nil; - _dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:cipher error:&error]; - - if (error != nil) { - [_logger warn:@"creating ARTDataEncoder: %@", error]; - _dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:nil error:nil]; - } + dispatch_barrier_async(_queue, ^{ + NSError *error = nil; + self->_dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:cipher error:&error]; + + if (error != nil) { + [self->_logger warn:@"creating ARTDataEncoder: %@", error]; + self->_dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:nil error:nil]; + } + }); } - (void)publish:(NSString *)name data:(id)data { From 0b507e371ef454c8b5e6290d1e87afd3db047614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szyszkowski?= Date: Fri, 5 Nov 2021 19:25:25 +0100 Subject: [PATCH 4/4] Remove `dispatch_barrier` that's not necessary on serial queues --- Source/ARTChannel.m | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Source/ARTChannel.m b/Source/ARTChannel.m index f3f0b244f..a3e3f3957 100644 --- a/Source/ARTChannel.m +++ b/Source/ARTChannel.m @@ -55,15 +55,13 @@ - (void)setOptions_nosync:(ARTChannelOptions *)options { } - (void)recreateDataEncoderWith:(ARTCipherParams*)cipher { - dispatch_barrier_async(_queue, ^{ - NSError *error = nil; - self->_dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:cipher error:&error]; - - if (error != nil) { - [self->_logger warn:@"creating ARTDataEncoder: %@", error]; - self->_dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:nil error:nil]; - } - }); + NSError *error = nil; + _dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:cipher error:&error]; + + if (error != nil) { + [_logger warn:@"creating ARTDataEncoder: %@", error]; + _dataEncoder = [[ARTDataEncoder alloc] initWithCipherParams:nil error:nil]; + } } - (void)publish:(NSString *)name data:(id)data {