-
Notifications
You must be signed in to change notification settings - Fork 534
Risk of deadlock? I don't think so, but... #367
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
Comments
None of the rules requires you to lock on these components because most logic around them can be and is achieved in a lock-free manner in the major 3 implementations. In RxJava specifically, there is a rule for the operators that one should not call
In theory, no; in practice, yes. request implementation has to be thread-safe and reentrant-safe because a request may come at any time from any thread to the
The main idea of Reactive-Streams is the non-blocking backpressure, thus when we (I assume) think about our existing and proven implementations, we avoid thinking in locks around signals entirely. |
@akarnokd What does "external synchronization" mean, then, if not the use of a lock? A lock-free (or in actuality, locks-hidden) approach may work, but is it spec-compliant and guaranteed interoperable with spec-compliant implementations? |
How would a Synchronization in the spec doesn't mean Java's |
The wording of the spec strongly implies that the objects to lock are the
Then their implementation would be non-compliant. The spec should say what it means, and "external synchronization" means something very specific. You are interpreting the spec as if it said that Edit: I assume the spec was designed this way because only the caller knows whether synchronization is needed. An implementation that uses internal synchronization pays the price of synchronization even when used in only one thread. (Granted, the price of an uncontested lock is negligible.) |
@kjkrum Thanks for asking this question which reveals that the What is meant by Edit: To clarify, it does not mandate, suggest, or recommend using the JVM construct |
All right, thanks for clarifying. (And thanks for not biting my head off.) |
@kjkrum You're most welcome. Thank you for pointing out this weakness in the documentation! :) |
Assign milestone 1.0.1? |
At a glance, these four rules seem to be a recipe for deadlock:
1.3
2.7
3.2
3.10
request
/onNext
If a
Publisher
thread locks theSubscriber
per 1.3 and calls a version ofonNext
that attempts to lock theSubscription
per 2.7, while aSubscriber
thread locks theSubscription
per 2.7 and calls a version ofrequest
that attempts to lock theSubscriber
per 1.3, the threads will deadlock.Do the synchronous calls allowed by 3.10 and 3.2 require external synchronization per 1.3 and 2.7?
I think the answer is "no", but the reason is subtle.
If
request
is called in multiple threads, the callers would synchronize on theSubscription
. Ifrequest
does not synchronize on theSubscriber
when callingonNext
, then there is no effective synchronization with callers ofSubscriber
methods that synchronize on theSubscriber
. Are there any such callers?If the
Publisher
is synchronous, then the only callers ofonNext
will be the callers ofrequest
, which are effectively synchronized with each other.If the
Publisher
is asynchronous, then its implementation ofrequest
will probably not make synchronous calls toonNext
in the first place.This should be guaranteed:
X.1
X.2
onSubscribe
/request
I think it's safe for an asynchronous
Subscriber
to lock theSubscription
inonSubscribe
to synchronously callrequest
, because although the caller ofonSubscribe
may hold the lock on theSubscriber
, noSubscriber
thread would be trying to obtain the locks in the opposite order at the timeonSubscribe
is called.request
/onError
I think it's safe for a
Subscription
produced by an asynchronousPublisher
to lock theSubscriber
inrequest
to synchronously callonError
(rule 3.9), because although the caller ofrequest
may hold the lock on theSubscription
, noPublisher
thread would be trying to obtain the locks in the opposite order. Again, the statefulness ofSubscriber
prevents the calls that could deadlock from occurring at the same time.Assuming my "X" rules are followed, is there any other situation where a thread would try to lock both the
Subscriber
and theSubscription
?The text was updated successfully, but these errors were encountered: