-
Notifications
You must be signed in to change notification settings - Fork 431
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
Fixed a race condition in Signal
terminal event handling.
#267
Changes from 1 commit
2dfa7cb
34e3e5e
403e4c7
b9b7ea7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -313,13 +313,24 @@ public final class Signal<Value, Error: Swift.Error> { | |
return ActionDisposable { [weak self] in | ||
if let s = self { | ||
s.updateLock.lock() | ||
|
||
if case let .alive(snapshot) = s.state { | ||
var observers = snapshot.observers | ||
observers.remove(using: token) | ||
s.state = .alive(AliveState(observers: observers, | ||
retaining: observers.isEmpty ? nil : self)) | ||
|
||
// Ensure the old signal state snapshot does not deinitialize before | ||
// `updateLock` is released. Otherwise, it might result in a | ||
// deadlock in cases where a `Signal` legitimately receives terminal | ||
// events recursively as a result of the deinitialization of the | ||
// snapshot. | ||
withExtendedLifetime(snapshot) { | ||
s.state = .alive(AliveState(observers: observers, | ||
retaining: observers.isEmpty ? nil : self)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on extracting this to a local |
||
s.updateLock.unlock() | ||
} | ||
} else { | ||
s.updateLock.unlock() | ||
} | ||
s.updateLock.unlock() | ||
} | ||
} | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ class ActionSpec: QuickSpec { | |
action.errors.observeValues { errors.append($0) } | ||
action.completed.observeValues { completedCount += 1 } | ||
} | ||
|
||
it("should retain the state property") { | ||
var property: MutableProperty<Bool>? = MutableProperty(false) | ||
weak var weakProperty = property | ||
|
@@ -114,6 +114,21 @@ class ActionSpec: QuickSpec { | |
expect(action.isExecuting.value) == false | ||
} | ||
|
||
it("should not deadlock") { | ||
final class ViewModel { | ||
let action2 = Action<(), (), NoError> { SignalProducer(value: ()) } | ||
} | ||
|
||
let action1 = Action<(), ViewModel, NoError> { SignalProducer(value: ViewModel()) } | ||
|
||
action1.values | ||
.flatMap(.latest) { viewModel in viewModel.action2.values.map { _ in viewModel } } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The deadlock happens as the observer disposable releases the closure |
||
.observeValues { _ in } | ||
|
||
action1.apply().start() | ||
action1.apply().start() | ||
} | ||
|
||
describe("completed") { | ||
beforeEach { | ||
enabled.value = true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not strictly necessary, since
snapshot
is guaranteed to live till the end of the scope. But it serves a documentation purpose,and would be optimised away by the compiler IIRC.