Skip to content

3.x: Fix scheduled tasks' fatal exception behavior #6956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.reactivex.rxjava3.internal.schedulers;

import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;

/**
Expand All @@ -39,7 +38,7 @@ public void run() {
runnable.run();
runner = null;
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
// Exceptions.throwIfFatal(ex); nowhere to go
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it seems make #748 a problem again. It seems tricky to balance this issue and #748. After all, if we passed StackOverflowError to RxJavaPlugins.onError(), RxJavaPlugins.onError() should throw it and it will be swallowed again.

I think customizing the future task will completely solve #748 and this issue. JDK supports that and at least as I know Android are using that approach. And I think it is not complex as the core code is only implementing the JDK's interface. It even simplifies the schedulers implementation, as I see ; )

runner = null;
lazySet(FINISHED);
RxJavaPlugins.onError(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.util.concurrent.Callable;

import io.reactivex.rxjava3.plugins.RxJavaPlugins;

/**
* A Callable to be submitted to an ExecutorService that runs a Runnable
* action and manages completion/cancellation.
Expand All @@ -35,10 +37,15 @@ public ScheduledDirectTask(Runnable runnable) {
public Void call() {
runner = Thread.currentThread();
try {
runnable.run();
} finally {
lazySet(FINISHED);
runner = null;
try {
runnable.run();
} finally {
lazySet(FINISHED);
runner = null;
}
} catch (Throwable ex) {
// Exceptions.throwIfFatal(e); nowhere to go
RxJavaPlugins.onError(ex);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import java.util.HashMap;
import java.util.concurrent.*;

import io.reactivex.rxjava3.disposables.Disposable;
import org.junit.Test;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.core.Scheduler.Worker;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.schedulers.ComputationScheduler;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;

public class ComputationSchedulerTests extends AbstractSchedulerConcurrencyTests {

Expand Down Expand Up @@ -192,4 +193,95 @@ public void run() {

assertEquals(0, calls[0]);
}

@Test
public void exceptionFromObservableShouldNotBeSwallowed() throws Exception {
// Exceptions, fatal or not, should be handled by
// #1 observer's onError(), or
// #2 RxJava exception handler, or
// #3 thread's uncaught exception handler,
// and should not be swallowed.
try {
CountDownLatch latch = new CountDownLatch(1);

// #3 thread's uncaught exception handler
Scheduler computationScheduler = new ComputationScheduler(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setUncaughtExceptionHandler((thread, throwable) -> {
latch.countDown();
});
return t;
}
});

// #2 RxJava exception handler
RxJavaPlugins.setErrorHandler(h -> {
latch.countDown();
});

// #1 observer's onError()
Observable.create(s -> {

s.onNext(1);

if (true) {
throw new OutOfMemoryError();
}

s.onComplete();

}).subscribeOn(computationScheduler)
.subscribe(v -> {
}, e -> {
latch.countDown();
});

assertTrue(latch.await(2, TimeUnit.SECONDS));
} finally {
RxJavaPlugins.reset();
}
}

@Test
public void exceptionFromObserverShouldNotBeSwallowed() throws Exception {
// Exceptions, fatal or not, should be handled by
// #1 observer's onError(), or
// #2 RxJava exception handler, or
// #3 thread's uncaught exception handler,
// and should not be swallowed.
try {
CountDownLatch latch = new CountDownLatch(1);

// #3 thread's uncaught exception handler
Scheduler computationScheduler = new ComputationScheduler(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setUncaughtExceptionHandler((thread, throwable) -> {
latch.countDown();
});
return t;
}
});

// #2 RxJava exception handler
RxJavaPlugins.setErrorHandler(h -> {
latch.countDown();
});

// #1 observer's onError()
Flowable.interval(500, TimeUnit.MILLISECONDS, computationScheduler)
.subscribe(v -> {
throw new OutOfMemoryError();
}, e -> {
latch.countDown();
});

assertTrue(latch.await(2, TimeUnit.SECONDS));
} finally {
RxJavaPlugins.reset();
}
}
}