Skip to content

Commit 412828d

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

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2014 Netflix, Inc.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -71,14 +71,18 @@ 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+
TimedAction polled;
81+
synchronized (queue) {
82+
polled = queue.poll();
83+
}
8084
if (polled != null) {
81-
polled.action.call();
85+
polled.action.call();
8286
}
8387
} while (wip.decrementAndGet() > 0);
8488
return Subscriptions.unsubscribed();
@@ -88,9 +92,8 @@ private Subscription enqueue(Action0 action, long execTime) {
8892

8993
@Override
9094
public void call() {
91-
PriorityQueue<TimedAction> _q = queue;
92-
if (_q != null) {
93-
_q.remove(timedAction);
95+
synchronized (queue) {
96+
queue.remove(timedAction);
9497
}
9598
}
9699

@@ -130,7 +133,7 @@ public int compareTo(TimedAction that) {
130133
return result;
131134
}
132135
}
133-
136+
134137
// because I can't use Integer.compare from Java 7
135138
private static int compare(int x, int y) {
136139
return (x < y) ? -1 : ((x == y) ? 0 : 1);

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)