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

Fixed a race condition in Signal terminal event handling. #267

Merged
merged 4 commits into from
Feb 18, 2017

Conversation

andersio
Copy link
Member

No description provided.

@andersio andersio changed the base branch from master to hotfix-1.0.1 February 17, 2017 15:45
Copy link
Contributor

@sharplet sharplet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to understand how this fix works, could you describe it for me? I also think this kind of explanation would be great to have in the commit message itself, too.

s.state = .alive(AliveState(observers: observers,
retaining: observers.isEmpty ? nil : self))

withExtendedLifetime(snapshot.retaining) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is retaining here? This line reads a little weirdly to me, I wonder if we can improve the name.

Copy link
Member Author

@andersio andersio Feb 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in AliveState:

A self-retaining reference [of the signal]. It is set when there are one or more active observers.


withExtendedLifetime(snapshot.retaining) {
s.state = .alive(AliveState(observers: observers,
retaining: observers.isEmpty ? nil : self))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on extracting this to a local newState so we can remove the awkward formatting over two lines?

@andersio andersio force-pushed the signal-deadlock-fix branch 3 times, most recently from c1b80ea to 8604418 Compare February 17, 2017 16:13
An explicit guard is added to 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.
@andersio
Copy link
Member Author

@sharplet Addressed both in code and in the commit. The implementation has been updated too.

// deadlock in cases where a `Signal` legitimately receives terminal
// events recursively as a result of the deinitialization of the
// snapshot.
withExtendedLifetime(snapshot) {
Copy link
Member Author

@andersio andersio Feb 17, 2017

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.

Copy link
Contributor

@sharplet sharplet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on extracting this to a local newState so we can remove the awkward formatting over two lines?

FWIW, I still think this would be a useful formatting improvement. But LGTM

let action1 = Action<(), ViewModel, NoError> { SignalProducer(value: ViewModel()) }

action1.values
.flatMap(.latest) { viewModel in viewModel.action2.values.map { _ in viewModel } }
Copy link
Member Author

@andersio andersio Feb 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deadlock happens as the observer disposable releases the closure { _ in viewModel } here without releasing the mapped signal's updateLock first. The deinitialization triggers the propagation of terminal event of the Action, which eventually hits the mapped signal.

@andersio andersio force-pushed the signal-deadlock-fix branch 2 times, most recently from 507e5c8 to 4bdf97c Compare February 17, 2017 16:54
@andersio andersio merged commit ded5801 into hotfix-1.0.1 Feb 18, 2017
@andersio andersio deleted the signal-deadlock-fix branch February 18, 2017 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants