Skip to content

Commit 6db3afc

Browse files
committed
Fixes NPEs reported in #1702 by synchronizing queue. Adds unit test for regression.
1 parent 833c6be commit 6db3afc

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

src/main/java/rx/schedulers/TrampolineScheduler.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ private Subscription enqueue(Action0 action, long execTime) {
7171
return Subscriptions.unsubscribed();
7272
}
7373
final TimedAction timedAction = new TimedAction(action, execTime, COUNTER_UPDATER.incrementAndGet(TrampolineScheduler.this));
74-
queue.add(timedAction);
74+
synchronized (queue) {
75+
queue.add(timedAction);
76+
}
7577

7678
if (wip.getAndIncrement() == 0) {
7779
do {
78-
TimedAction polled = queue.poll();
79-
// check for null as it could have been unsubscribed and removed
80-
if (polled != null) {
81-
polled.action.call();
80+
synchronized (queue) {
81+
if (!queue.isEmpty()) {
82+
TimedAction polled = queue.poll();
83+
polled.action.call();
84+
}
8285
}
8386
} while (wip.decrementAndGet() > 0);
8487
return Subscriptions.unsubscribed();
@@ -88,9 +91,8 @@ private Subscription enqueue(Action0 action, long execTime) {
8891

8992
@Override
9093
public void call() {
91-
PriorityQueue<TimedAction> _q = queue;
92-
if (_q != null) {
93-
_q.remove(timedAction);
94+
synchronized (queue) {
95+
queue.remove(timedAction);
9496
}
9597
}
9698

src/test/java/rx/schedulers/TrampolineSchedulerTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
import static org.junit.Assert.*;
1919

2020
import java.util.*;
21+
import java.util.concurrent.TimeUnit;
2122

2223
import org.junit.Test;
2324

2425
import rx.*;
26+
import rx.Observer;
2527
import rx.Scheduler.Worker;
2628
import rx.Observable;
2729
import rx.functions.*;
30+
import rx.observers.Observers;
31+
import rx.observers.TestSubscriber;
2832
import rx.subscriptions.CompositeSubscription;
2933

3034
public class TrampolineSchedulerTest extends AbstractSchedulerTests {
@@ -95,6 +99,47 @@ public void call() {
9599
}
96100
}
97101

102+
/**
103+
* This is a regression test for #1702. Concurrent work scheduling that is improperly synchronized can cause an
104+
* action to be added or removed onto the priority queue during a poll, which can result in NPEs during queue
105+
* sifting. While it is difficult to isolate the issue directly, we can easily trigger the behavior by spamming the
106+
* trampoline with enqueue requests from multiple threads concurrently.
107+
*/
108+
@Test
109+
public void testTrampolineWorkerHandlesConcurrentScheduling() {
110+
final Worker trampolineWorker = Schedulers.trampoline().createWorker();
111+
final Observer<Subscription> observer = Observers.empty();
112+
final TestSubscriber<Subscription> ts = new TestSubscriber<Subscription>(observer);
113+
114+
// Spam the trampoline with actions.
115+
Observable.range(0, 50)
116+
.flatMap(new Func1<Integer, Observable<Subscription>>() {
117+
118+
@Override
119+
public Observable<Subscription> call(Integer count) {
120+
return Observable.interval(1, TimeUnit.MICROSECONDS).map(
121+
new Func1<Long, Subscription>() {
122+
123+
@Override
124+
public Subscription call(Long count) {
125+
return trampolineWorker.schedule(new Action0() {
126+
127+
@Override
128+
public void call() {}
129+
130+
});
131+
}
132+
133+
}).limit(100);
134+
}
135+
136+
})
137+
.subscribeOn(Schedulers.computation())
138+
.subscribe(ts);
139+
ts.awaitTerminalEvent();
140+
ts.assertNoErrors();
141+
}
142+
98143
private static Worker doWorkOnNewTrampoline(final String key, final ArrayList<String> workDone) {
99144
Worker worker = Schedulers.trampoline().createWorker();
100145
worker.schedule(new Action0() {

0 commit comments

Comments
 (0)