question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BehaviorSubject concurrent subscription and sending is broken

See original GitHub issue

BehaviorSubject should ensure that the last notification always reaches the subscriber. When the subscription and sending happens concurrently, there is a high probability that this property gets broken.

Test has been prepared that easily reproduces the error. https://gist.github.com/andrask/fc06abfd70daa6f91edb#file-behaviorsubjectsubscribeandsendconcurrently-java

The test involves two threads: 1) trying to subscribe 2) trying to send next. These are carefully coordinated to allow real concurrent execution. The test is that the subscriber must receive the sent value. The issue almost certainly happens in a few hundred retries.

Note that with a Thread.sleep(1) the issue goes away.

Note that the test is something I distilled from what I saw in my production code. There may be little issues with it but the concurrency problem certainly exists as it is reproducibly just by stepping through the code.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:21 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, May 24, 2016

Subscription is not an on* method and therefore this is not expected.

On Tue, May 24, 2016 at 7:38 PM Artem Zinnatullin notifications@github.com wrote:

This is expected behavior.

If you you write to the subject from multiple threads and need consistency of on*() events -> you can convert your subject to SerializedSubject via subject.toSerialized().

Javadoc of Subject.toSerialized():

/** * Wraps a {@link Subject} so that it is safe to call its various {@code on} methods from different threads. *

* When you use an ordinary {@link Subject} as a {@link Subscriber}, you must take care not to call its * {@link Subscriber#onNext} method (or its other {@code on} methods) from multiple threads, as this could * lead to non-serialized calls, which violates * the Observable contract and creates an * ambiguity in the resulting Subject. *

* To protect a {@code Subject} from this danger, you can convert it into a {@code SerializedSubject} with * code like the following: *

{@code * mySafeSubject = myUnsafeSubject.toSerialized(); * }
* * @return SerializedSubject wrapping the current Subject */

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/ReactiveX/RxJava/issues/1184#issuecomment-221433827

0reactions
sreggcommented, May 26, 2016

Seems that my idea of delaying the action in doOnSubscribe still doesn’t fix the issue. Now I’m thinking there’s something wrong between onNext and onComplete. Sometimes only onComplete is received…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular2 + RxJS BehaviorSubject subscription not working on ...
This is working perfectly if Component B is child of Component A, but if A is the root component and B is inside...
Read more >
3 Common Rxjs Pitfalls (and how to avoid them)
Now that we have a subscription, the HTTP lesson creation POST call will be sent to the backend as expected, which solves the...
Read more >
BehaviorSubject - Learn RxJS
Requires an initial value and emits the current value to new subscribers ; If you want the last emitted value(s) on subscription, but...
Read more >
Observable | RxJS API Document - ReactiveX
Maximum number of input Observables being subscribed to concurrently. scheduler, Scheduler. optional; default: null. The IScheduler to use for managing ...
Read more >
Observable - Monix
trait Observable[+A] { def subscribe(o: Observer[A]): Cancelable } ... Since they could be sent concurrently, it buffers the elements when ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found