Skip to content
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

Use Observable.create() to construct LocationServicesOkObservable #438

Merged

Conversation

thuytrinh
Copy link
Contributor

Fix #404 again.

@CLAassistant
Copy link

CLAassistant commented Jun 7, 2018

CLA assistant check
All committers have signed the CLA.

public void cancel() throws Exception {
try {
context.unregisterReceiver(broadcastReceiver);
} catch (IllegalStateException ignored) {
Copy link
Owner

Choose a reason for hiding this comment

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

We may end up leaking a context if this will get called before the subscribe finishes

Copy link
Contributor Author

@thuytrinh thuytrinh Jun 7, 2018

Choose a reason for hiding this comment

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

Good catch @dariuszseweryn 👍 There may be a case like that. For example, considering the following execution flow:

  • The program passes by line 51 at if (!emitter.isDisposed()) {. There's still at least a subscriber of this Observable, so the context.registerReceiver() is about to be executed.
  • Then, somehow no subscriber subscribes to the Observable anymore. The code in setCancellable() gets invoked.
  • Then context.registerReceiver() gets executed. Thus the leak has just happened.

I'm thinking about doing another check if (!emitter.isDisposed()) right after context.registerReceiver() but it looks kinda weird. I never wrote such conditional flow before. Wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or another idea is to move the setCancellable() into the isDispose() check. For example:

if (!emitter.isDisposed()) {
    context.registerReceiver(broadcastReceiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
    emitter.onNext(initialValue);
    emitter.setCancellable(new Cancellable() {
        @Override
        public void cancel() throws Exception {
            try {
                context.unregisterReceiver(broadcastReceiver);
            } catch (IllegalStateException ignored) {
            }
        }
    });
}

Wdyt?

Copy link
Contributor Author

@thuytrinh thuytrinh Jun 7, 2018

Choose a reason for hiding this comment

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

I've changed the impl into below based on @akarnokd's suggestion link:

    public Observable<Boolean> get() {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(final ObservableEmitter<Boolean> emitter) throws Exception {
                final boolean initialValue = locationServicesStatus.isLocationProviderOk();
                final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        final boolean newValue = locationServicesStatus.isLocationProviderOk();
                        emitter.onNext(newValue);
                    }
                };
                emitter.onNext(initialValue);
                context.registerReceiver(broadcastReceiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
                emitter.setCancellable(new Cancellable() {
                    @Override
                    public void cancel() throws Exception {
                        try {
                            context.unregisterReceiver(broadcastReceiver);
                        } catch (IllegalStateException ignored) {
                        }
                    }
                });
            }
        }).distinctUntilChanged();
    }

I think this impl is able to prevent the above leak from happening.

Copy link
Owner

Choose a reason for hiding this comment

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

Yup, but we should get rid of the try/catch in the Cancellable as it could only hide potential issues

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dariuszseweryn Good call 👍 Done.

Copy link
Collaborator

@uKL uKL left a comment

Choose a reason for hiding this comment

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

Looks good to me :) #notasubclassingfananyway

Copy link
Owner

@dariuszseweryn dariuszseweryn left a comment

Choose a reason for hiding this comment

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

🚀

@dariuszseweryn dariuszseweryn merged commit 09c96c6 into dariuszseweryn:master Jun 8, 2018
@dariuszseweryn
Copy link
Owner

Awesome! Thank you for the contribution.

@thuytrinh thuytrinh deleted the thuy/fix-IllegalStateException branch June 8, 2018 07:57
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

Successfully merging this pull request may close these issues.

None yet

4 participants