Repository navigation
Description
Summary
StreamableTimer.TimerStreamer.finish() overwrites its AtomicReference<Disposable> using lazySet(DisposableHelper.DISPOSED) instead of calling DisposableHelper.dispose(this).
As a result, the underlying scheduled task (and its worker thread) is never cancelled.
This leads to permanent retention of worker threads for the full delay duration, even after finish() is invoked. When a bounded ExecutorService is used, this can cause thread starvation and severe availability degradation.
Reported as a bug, not a security vulnerability, per project policy.
Affected Version
4.0.0-alpha-21
Affected Component
io.reactivex.rxjava4.internal.operators.streamable.StreamableTimerStreamableTimer.TimerStreamer.finish()(lines 96–101)
Root Cause
@Override
public @NonNull CompletionStage<Void> finish() {
waiter.complete(false);
lazySet(DisposableHelper.DISPOSED); // BUG: overwrites without cancelling
return FINISHED;
}TimerStreamer extends AtomicReference<Disposable>. The real scheduled resource (a Future or scheduler task) is stored in this reference during setOnce.
Correct disposal requires DisposableHelper.dispose(this), which:
- CAS-swaps the reference to
DISPOSED - calls
dispose()/cancel(true)on the held resource
finish() bypasses this entirely. Compare with the correctly implemented sibling:
// StreamableIntervalRange.IntervalStreamer
@Override
public @NonNull CompletionStage<Void> finish() {
DisposableHelper.dispose(this); // correct
return FINISHED;
}Impact
When Streamable.timer(delay, unit, executor) is used with a bounded ExecutorService (a common server-side pattern), calling finish() early does not free the worker thread.
- The worker remains blocked in
Thread.sleep(delay)for the full duration - Unrelated tasks on the same scheduler are delayed or blocked
- Impact scales linearly with pool size
This is especially visible under:
flatMap/switchMap/takeUntilcancellation- High subscription/cancellation churn
- Shared scheduler environments (e.g., backend services)
Reproduction (executed against real compiled library)
import io.reactivex.rxjava4.core.*;
import io.reactivex.rxjava4.disposables.*;
import java.util.concurrent.*;
public class TimerThreadStarvationDemo {
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(1);
CompositeDisposable cancellation = new CompositeDisposable();
Streamable<Long> timer = Streamable.timer(5, TimeUnit.SECONDS, pool);
var streamer = timer.stream(cancellation);
Thread.sleep(100); // ensure worker is inside sleep
long t0 = System.currentTimeMillis();
streamer.finish().toCompletableFuture().join();
System.out.println("finish() returned after " + (System.currentTimeMillis() - t0) + "ms");
long t1 = System.currentTimeMillis();
long ran = pool.submit(() -> System.currentTimeMillis()).get();
long delay = ran - t1;
System.out.println("Probe task queued at t1, actually ran after " + delay + "ms");
System.out.println(delay > 2000 ? "BUG REPRODUCED" : "NOT REPRODUCED");
}
}Observed behavior:
finish()returns immediately- Probe task waits ~4.9 seconds
- Worker thread remained blocked for the full 5-second delay
Multi-tenant / shared scheduler demonstration
int poolSize = 4;
ExecutorService pool = Executors.newFixedThreadPool(poolSize);
long delay = 30;
// Simulate multiple clients cancelling timers early
for (int i = 0; i < poolSize; i++) {
CompositeDisposable cd = new CompositeDisposable();
var s = Streamable.timer(delay, TimeUnit.SECONDS, pool).stream(cd);
Thread.sleep(20);
s.finish().toCompletableFuture().join();
}
// Unrelated, legitimate work
long t0 = System.currentTimeMillis();
long ran = pool.submit(() -> System.currentTimeMillis()).get();
System.out.println("Unrelated task waited " + (ran - t0) + "ms");Result:
- 4 cheap
finish()calls - Unrelated task delayed ~29.9 seconds
This demonstrates cross-client impact under shared schedulers, not merely self-inflicted resource waste.
Proposed Fix
@Override
public @NonNull CompletionStage<Void> finish() {
waiter.complete(false);
DisposableHelper.dispose(this); // actually cancel the underlying task
return FINISHED;
}This aligns StreamableTimer with:
StreamableIntervalRange- Other operators that correctly release resources on terminal events
Additional Notes
- This issue is a resource lifecycle bug, not a security exploit.
- No special privileges, race windows, or malformed inputs are required.
- The fix is minimal, low-risk, and consistent with existing operator patterns.
I’m happy to open a PR if the team agrees this is the right direction.
Activity