Skip to content

Commit

Permalink
Add synchronized access to EventEngine's state property (#199)
Browse files Browse the repository at this point in the history
fix(subscribe): fix the crash issue caused by multiple accesses to the `state` property in multithreaded scenarios
  • Loading branch information
jguz-pubnub authored Jan 16, 2025
1 parent 2e3e028 commit 5878413
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
matrix:
environment: [iOS, tvOS, macOS]
timeout-minutes: 5
timeout-minutes: 7
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
9 changes: 7 additions & 2 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
---
name: swift
scm: github.com/pubnub/swift
version: "8.2.4"
version: "8.2.5"
schema: 1
changelog:
- date: 2025-01-16
version: 8.2.5
changes:
- type: bug
text: "Fix the crash issue caused by multiple accesses to the `state` property in multithreaded scenarios."
- date: 2025-01-10
version: 8.2.4
changes:
Expand Down Expand Up @@ -643,7 +648,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: PubNub
location: https://github.com/pubnub/swift/archive/refs/tags/8.2.4.zip
location: https://github.com/pubnub/swift/archive/refs/tags/8.2.5.zip
supported-platforms:
supported-operating-systems:
macOS:
Expand Down
16 changes: 8 additions & 8 deletions PubNub.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3999,7 +3999,7 @@
"@loader_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 8.2.4;
MARKETING_VERSION = 8.2.5;
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
Expand Down Expand Up @@ -4050,7 +4050,7 @@
"@loader_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 8.2.4;
MARKETING_VERSION = 8.2.5;
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
Expand Down Expand Up @@ -4158,7 +4158,7 @@
"@loader_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 8.2.4;
MARKETING_VERSION = 8.2.5;
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
Expand Down Expand Up @@ -4211,7 +4211,7 @@
"@loader_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 8.2.4;
MARKETING_VERSION = 8.2.5;
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
Expand Down Expand Up @@ -4332,7 +4332,7 @@
"@loader_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 8.2.4;
MARKETING_VERSION = 8.2.5;
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
Expand Down Expand Up @@ -4384,7 +4384,7 @@
"@loader_path/Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 8.2.4;
MARKETING_VERSION = 8.2.5;
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++17";
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
Expand Down Expand Up @@ -4864,7 +4864,7 @@
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 8.2.4;
MARKETING_VERSION = 8.2.5;
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++14";
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
Expand Down Expand Up @@ -4907,7 +4907,7 @@
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 8.2.4;
MARKETING_VERSION = 8.2.5;
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++14";
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
Expand Down
2 changes: 1 addition & 1 deletion PubNubSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PubNubSwift'
s.version = '8.2.4'
s.version = '8.2.5'
s.homepage = 'https://github.com/pubnub/swift'
s.documentation_url = 'https://www.pubnub.com/docs/swift-native/pubnub-swift-sdk'
s.authors = { 'PubNub, Inc.' => '[email protected]' }
Expand Down
28 changes: 22 additions & 6 deletions Sources/PubNub/EventEngine/Core/EventEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@ struct EventEngineDependencies<Dependencies> {
class EventEngine<State, Event, Invocation: AnyEffectInvocation, Input> {
private let transition: any TransitionProtocol<State, Event, Invocation>
private let dispatcher: any Dispatcher<Invocation, Event, Input>
private(set) var state: State
private let recursiveLock = NSRecursiveLock()
private var internalStateContainer: State

private(set) var state: State {
get {
recursiveLock.lock()
defer { recursiveLock.unlock() }
return internalStateContainer
} set {
recursiveLock.lock()
defer { recursiveLock.unlock() }
internalStateContainer = newValue
}
}

var dependencies: EventEngineDependencies<Input>
var onStateUpdated: ((State) -> Void)?
Expand All @@ -29,27 +42,30 @@ class EventEngine<State, Event, Invocation: AnyEffectInvocation, Input> {
dispatcher: some Dispatcher<Invocation, Event, Input>,
dependencies: EventEngineDependencies<Input>
) {
self.state = state
self.internalStateContainer = state
self.onStateUpdated = onStateUpdated
self.transition = transition
self.dispatcher = dispatcher
self.dependencies = dependencies
}

func send(event: Event) {
objc_sync_enter(self)
recursiveLock.lock()

defer {
objc_sync_exit(self)
recursiveLock.unlock()
}

let currentState = state

guard transition.canTransition(
from: state,
from: currentState,
dueTo: event
) else {
return
}

let transitionResult = transition.transition(from: state, event: event)
let transitionResult = transition.transition(from: currentState, event: event)
let invocations = transitionResult.invocations

state = transitionResult.state
Expand Down
2 changes: 1 addition & 1 deletion Sources/PubNub/Helpers/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public enum Constant {

static let pubnubSwiftSDKName: String = "PubNubSwift"

static let pubnubSwiftSDKVersion: String = "8.2.4"
static let pubnubSwiftSDKVersion: String = "8.2.5"

static let appBundleId: String = {
if let info = Bundle.main.infoDictionary,
Expand Down
6 changes: 3 additions & 3 deletions fastlane/.env.ios
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DEVICES="iPhone 13 (15.5),iPhone 15 (17.5)"
COVERAGE="iPhone 15 (17.5)"
DEVICES="generic/platform=iOS Simulator"
COVERAGE="generic/platform=iOS Simulator"

DESTINATION_EXAMPLE="OS=17.4,name=iPhone 15"
DESTINATION_EXAMPLE="generic/platform=iOS Simulator"
TEST_OUTPUT_DIR="fastlane/test_output/iOS"
SCHEME_EXAMPLE=Example-iOS
PLATFORM=ios
4 changes: 2 additions & 2 deletions fastlane/.env.tvos
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DEVICES="Apple TV (15.4),Apple TV (17.5)"
COVERAGE="Apple TV (17.5)"
DEVICES="generic/platform=tvOS Simulator"
COVERAGE="generic/platform=tvOS Simulator"

TEST_OUTPUT_DIR="fastlane/test_output/tvOS"
PLATFORM=tvos

0 comments on commit 5878413

Please sign in to comment.