Skip to content

Commit c4ef6b0

Browse files
authored
Remove handler callbacks before setting disposed.
This change ensures that you can never observe `disposed` being set to `true` inside `run()`. `removeCallbacks` and the mechanism by which a `Looper` retrieves the head of the message queue are both governed by a lock. When `dispose()` is called on a non-main thread taking this lock is the race which determines cancelation and **not** the boolean. Include a comment emphasizing that the boolean is tracked solely for the purposes of accurate `isDisposed()` reporting.
1 parent 5cb2391 commit c4ef6b0

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

rxandroid/src/main/java/io/reactivex/android/schedulers/HandlerScheduler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static final class ScheduledRunnable implements Runnable, Disposable {
9696
private final Handler handler;
9797
private final Runnable delegate;
9898

99-
private volatile boolean disposed;
99+
private volatile boolean disposed; // Tracked solely for isDisposed().
100100

101101
ScheduledRunnable(Handler handler, Runnable delegate) {
102102
this.handler = handler;
@@ -114,8 +114,8 @@ public void run() {
114114

115115
@Override
116116
public void dispose() {
117-
disposed = true;
118117
handler.removeCallbacks(this);
118+
disposed = true;
119119
}
120120

121121
@Override

0 commit comments

Comments
 (0)