Clarify intent of rule 1.9 - #488
Open
NiteshKant wants to merge 1 commit into
Open
Conversation
Rule 1.9 defines that `onSubscribe()` on a `Subscriber` should be called before calling any other method on that `Subscriber`. However, this does not clarify that if `onSubscribe()` is signalled asynchronously then there should be a happens-before relationship between `subscribe()` and signalling of `onSubscribe()`.
In absence of such a guarantee, non-final fields initialized in a constructor have no guarantee to be visible inside `onSubscribe()`.
Considering a simple example:
public class UnsafeSubscriber implements Subscriber<String> {
private boolean duplicateOnSubscribe = false;
@OverRide
public void onSubscribe(final Subscription s) {
if (duplicateOnSubscribe) {
throw new IllegalStateException("Duplicate onSubscribe() calls.");
}
duplicateOnSubscribe = true;
}
@OverRide
public void onNext(final String s) {
}
@OverRide
public void onError(final Throwable t) {
}
@OverRide
public void onComplete() {
}
}
If an UnsafeSubscriber instance is created in a different thread than the one that invokes onSubscribe() (true for an asynchronous Publisher), according to the java memory model, this statement inside onSubscribe():
if (duplicateOnSubscribe) {
is guaranteed to compute to false if and only if the instance is published safely between these threads. None of the rules in the specifications establish a happens-before relationship between Publisher#subscribe() and Subscriber#onSubscribe(). So, the usage above can be categorized as unsafe. In a more convoluted form, the assignment:
private boolean duplicateOnSubscribe = false;
can be interleaved with
duplicateOnSubscribe = true; such that duplicateOnSubscribe is set to false later.
Has this been considered before or am I missing something?
Fixes reactive-streams#486
Contributor
|
@NiteshKant Your Subscriber is not a valid subscriber since it violates 2:13: https://github.com/reactive-streams/reactive-streams-jvm#2.13 |
Author
|
@viktorklang yikes, yes. Throwing if (duplicateOnSubscribe) {
System.out.println("Duplicate onSubscribe() calls.");
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rule 1.9 defines that
onSubscribe()on aSubscribershould be called before calling any other method on thatSubscriber. However, this does not clarify that ifonSubscribe()is signalled asynchronously then there should be a happens-before relationship betweensubscribe()and signalling ofonSubscribe().In absence of such a guarantee, non-final fields initialized in a constructor have no guarantee to be visible inside
onSubscribe().Considering a simple example:
public class UnsafeSubscriber implements Subscriber {
private boolean duplicateOnSubscribe = false;
}
If an UnsafeSubscriber instance is created in a different thread than the one that invokes onSubscribe() (true for an asynchronous Publisher), according to the java memory model, this statement inside onSubscribe():
if (duplicateOnSubscribe) {
is guaranteed to compute to false if and only if the instance is published safely between these threads. None of the rules in the specifications establish a happens-before relationship between Publisher#subscribe() and Subscriber#onSubscribe(). So, the usage above can be categorized as unsafe. In a more convoluted form, the assignment:
private boolean duplicateOnSubscribe = false;
can be interleaved with
duplicateOnSubscribe = true; such that duplicateOnSubscribe is set to false later.
Has this been considered before or am I missing something?
Fixes #486