From 2cf434944540d66ea683bdcb84499eb5f7edd2eb Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Thu, 9 Oct 2014 10:38:05 -0700 Subject: [PATCH] Fix TrampolineScheduler NullPointerException I tried for about 30 minutes to replicate the NPE reported in https://github.com/ReactiveX/RxJava/issues/1702 but couldn't. It makes sense reading the code that an unsubscribe could trigger an NPE though so I'm fixing it as per recommendation of @DylanSale even though I can't replicate. I confirmed that the items are being put in the queue BEFORE the wip variable is incremented, so that concurrency seems okay. --- src/main/java/rx/schedulers/TrampolineScheduler.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/rx/schedulers/TrampolineScheduler.java b/src/main/java/rx/schedulers/TrampolineScheduler.java index 99ab2bfeed..f792663dee 100644 --- a/src/main/java/rx/schedulers/TrampolineScheduler.java +++ b/src/main/java/rx/schedulers/TrampolineScheduler.java @@ -82,7 +82,11 @@ private Subscription enqueue(Action0 action, long execTime) { if (wip.getAndIncrement() == 0) { do { - queue.poll().action.call(); + TimedAction polled = queue.poll(); + // check for null as it could have been unsubscribed and removed + if (polled != null) { + polled.action.call(); + } } while (wip.decrementAndGet() > 0); return Subscriptions.empty(); } else {