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

Discarding new disposable on a disposed SerialDisposable #688

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions Sources/Disposable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ public final class SerialDisposable: Disposable {
}

set(disposable) {
_inner.swap(disposable)?.dispose()

if let disposable = disposable, isDisposed {
disposable.dispose()
if isDisposed {
disposable?.dispose()
} else {
_inner.swap(disposable)?.dispose()
Copy link
Member

Choose a reason for hiding this comment

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

We need to check isDisposed again after swapping, since self could be disposed of in parallel during the process, racing against https://github.com/ReactiveCocoa/ReactiveSwift/pull/688/files#diff-7056bb5d45375e26f2eeb3adeabac16bR373.

Copy link
Contributor Author

@DevAndArtist DevAndArtist Dec 4, 2018

Choose a reason for hiding this comment

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

Can you provide a change suggestion and elaborate more why we need to do it, because I'm not sure I follow. I always thought that such operations should not be done in a multi-threading fashion, so it will be the users responsibility to serialize correctly.

If this PR gets approved, I'll add more documentations because this slightly changes the behavior of the type after it's disposed.

Copy link
Member

Choose a reason for hiding this comment

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

Change suggestions do not seem to be available in this repo.

Essentially, we need to do this after the swapping in the else path:

if isDisposed {
    _inner.swap(nil)?.dispose()
}

Because dispose() can be called in parallel to inner = new on different threads. Depending on the order observed by the system memory, it could yield a boundary case where isDisposed is true but the inner disposable is left untouched. The conflicting path may

I always thought that such operations should not be done in a multi-threading fashion, so it will be the users responsibility to serialize correctly.

Disposables have always meant to be thread safe, despite us not explicitly having written this as part of the contract.

Copy link
Contributor

Choose a reason for hiding this comment

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

Change suggestions do not seem to be available in this repo

Weird. I don't see a setting for it.

}
}
}
Expand Down