Skip to content

Conversation

@andrey-radchenko
Copy link
Contributor

No description provided.

s.onSubscribe(parent);

single.subscribe(parent);
s.onSubscribe(parent);
Copy link
Member

Choose a reason for hiding this comment

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

This violates the RS contract. OnSubscribe must happen before an other calls to onXXX.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will restore origin subscribe() flow

TestSubscriber<Object> ts = new TestSubscriber<Object>();

toSingle(toPublisher(Observable.just(1))).subscribe(ts);

Copy link
Member

Choose a reason for hiding this comment

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

Please don't change existing unit tests and add new ones instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will restore original just() unit test and will add singleJust() that was hanging.

@akarnokd
Copy link
Member

The bug is that there is a return missing just before L90

@akarnokd akarnokd added the bug label Oct 17, 2016
@akarnokd
Copy link
Member

Maybe its better if you start over from a fresh branch of main.

@andrey-radchenko
Copy link
Contributor Author

Well, I started over from the tip of 1.x and added the unit test. However, adding return just before L90 has not changed anything. The problem is with the infinite loop. Without the actual single subscription, the current state will not change from outside the loop and there is no transition from NO_REQUEST_NO_DATA state within the loop, so that will keep it spinning forever.

@akarnokd
Copy link
Member

I see. Try this:

        @Override
        public void onSuccess(T value) {
            if (cancelled) {
                return;
            }
            if (value == null) {
                state.lazySet(HAS_REQUEST_HAS_VALUE);
                actual.onError(new NullPointerException("value"));
                return;
            }
            for (;;) {
                int s = state.get();

                if (s == NO_REQUEST_HAS_VALUE || s == HAS_REQUEST_HAS_VALUE || cancelled) {
                    break;
                } else
                if (s == HAS_REQUEST_NO_VALUE) {
                    state.lazySet(HAS_REQUEST_HAS_VALUE);
                    actual.onNext(value);
                    if (!cancelled) {
                        actual.onComplete();
                    }
                    break;
                } else {
                    this.value = value;
                    if (state.compareAndSet(s, NO_REQUEST_HAS_VALUE)) {
                        break;
                    }
                }
            }
        }

        @Override
        public void onError(Throwable error) {
            if (cancelled) {
                return;
            }
            state.lazySet(HAS_REQUEST_HAS_VALUE);
            actual.onError(error);
        }

        @Override
        public void request(long n) {
            if (n > 0) {
                for (;;) {
                    int s = state.get();
                    if (s == HAS_REQUEST_HAS_VALUE || s == HAS_REQUEST_NO_VALUE || cancelled) {
                        break;
                    } else
                    if (s == NO_REQUEST_HAS_VALUE) {
                        if (state.compareAndSet(s, HAS_REQUEST_HAS_VALUE)) {
                            T v = value;
                            value = null;

                            actual.onNext(v);
                            if (!cancelled) {
                                actual.onComplete();
                            }
                        }
                        break;
                    }
                    if (state.compareAndSet(NO_REQUEST_NO_VALUE, HAS_REQUEST_NO_VALUE)) {
                        break;
                    }
                }
            }
        }

@andrey-radchenko
Copy link
Contributor Author

Yes, this works. Breaking the request loop on NO_REQUEST_NO_VALUE was missing there. Committed the change and created PR. (#156)

Please decline the previous PR. (#155)

@akarnokd
Copy link
Member

Closing via #156.

@akarnokd akarnokd closed this Oct 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants