Skip to content

3.5 Why is thread-safety of Subscription.cancel() required? #319

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

Closed
anthonyvdotbe opened this issue Apr 7, 2016 · 7 comments
Closed

3.5 Why is thread-safety of Subscription.cancel() required? #319

anthonyvdotbe opened this issue Apr 7, 2016 · 7 comments
Assignees
Milestone

Comments

@anthonyvdotbe
Copy link
Contributor

Rule 3.5 says that Subscription.cancel() MUST be thread-safe.
Why is this so, given that rule 2.7 excludes the possibility of Subscription.cancel() being invoked concurrently? Why isn't it sufficient if the fact of cancellation is guaranteed to be visible to any subsequent calls on the Subscription?

For example, say I have something like the following:

volatile boolean cancelled = false;

public void cancel() {
    myHashMap.put("hello", "world");
    cancelled = true;
}

Why isn't this allowed?

@akarnokd
Copy link
Contributor

akarnokd commented Apr 7, 2016

Experience with RxJava 2, Rsc and Reactor indicates that rule 2.7 adds unnecessary burden because cancels can happen from any thread and in any numbers. For example, a timeout cancels a stream that is cancelled by a limiting operator at the same time.

In contrast, making cancel thread safe is usually very simple, just like in your example. It usually sets a cancelled flag, or performs some CAS that succeeds only once and does cleanup, etc.

@viktorklang
Copy link
Contributor

@akarnokd Why would it be a burden, all which is required is to wrap the original Subscription in a safe wrapper? Or did I misunderstand?

@akarnokd
Copy link
Contributor

akarnokd commented Apr 8, 2016

It's not only the wrapping but the serialization primitive overhead. Plus the hundreds of operators I've written, the thread-safe cancellation was an easy / trivial thing (see linked by the examples).

@viktorklang
Copy link
Contributor

@akarnokd Yes, threadsafe cancellation is "easy", however, should the spec mandate how it is implemented? Or what do you propose given your experience?

@akarnokd
Copy link
Contributor

akarnokd commented Apr 8, 2016

Drop §2.7. §3.5 is fine by itself.

@viktorklang
Copy link
Contributor

@akarnokd §2.7 covers more than cancel though.

@viktorklang
Copy link
Contributor

AFAIR addressed by #339

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

No branches or pull requests

3 participants