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 all commits
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
17 changes: 11 additions & 6 deletions src/main/java/io/reactivex/rxjava3/core/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

import io.reactivex.rxjava3.annotations.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.disposables.*;
import io.reactivex.rxjava3.internal.schedulers.*;
import io.reactivex.rxjava3.internal.util.ExceptionHelper;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.schedulers.SchedulerRunnableIntrospection;

Expand Down Expand Up @@ -542,9 +540,10 @@ public void run() {
try {
run.run();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
worker.dispose();
throw ExceptionHelper.wrapOrThrow(ex);
// Exceptions.throwIfFatal(ex); nowhere to go
dispose();
RxJavaPlugins.onError(ex);
throw ex;
Copy link
Member Author

Choose a reason for hiding this comment

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

@JakeWharton Does this syntax work with Android desugar (rethrowing final Throwables)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm this looks like a normal throw. I don't understand what might be a compatibility concern here.

Copy link
Member Author

Choose a reason for hiding this comment

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

The Java 7 compiler feature that let's you rethrow constant cheched or Throwable exceptions from within a catch block, even if the method didn't specify a throws clause.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, missed it was Throwable. Checked exceptions are only a feature of the Java compiler not the Java bytecode so yeah the Android toolchain won't care 👍 .

}
}
}
Expand Down Expand Up @@ -586,7 +585,13 @@ static final class DisposeTask implements Disposable, Runnable, SchedulerRunnabl
public void run() {
runner = Thread.currentThread();
try {
decoratedRun.run();
try {
decoratedRun.run();
} catch (Throwable ex) {
// Exceptions.throwIfFatal(e); nowhere to go
RxJavaPlugins.onError(ex);
throw ex;
}
} finally {
dispose();
runner = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ public void run() {
}
try {
actual.run();
} catch (Throwable ex) {
// Exceptions.throwIfFatal(ex); nowhere to go
RxJavaPlugins.onError(ex);
throw ex;
} finally {
lazySet(true);
}
Expand Down Expand Up @@ -386,7 +390,13 @@ public void run() {
thread = Thread.currentThread();
if (compareAndSet(READY, RUNNING)) {
try {
run.run();
try {
run.run();
} catch (Throwable ex) {
// Exceptions.throwIfFatal(ex); nowhere to go
RxJavaPlugins.onError(ex);
throw ex;
}
} finally {
thread = null;
if (compareAndSet(RUNNING, FINISHED)) {
Expand Down Expand Up @@ -463,11 +473,17 @@ public void run() {
Runnable r = get();
if (r != null) {
try {
r.run();
} finally {
lazySet(null);
timed.lazySet(DisposableHelper.DISPOSED);
direct.lazySet(DisposableHelper.DISPOSED);
try {
r.run();
} finally {
lazySet(null);
timed.lazySet(DisposableHelper.DISPOSED);
direct.lazySet(DisposableHelper.DISPOSED);
}
} catch (Throwable ex) {
// Exceptions.throwIfFatal(ex); nowhere to go
RxJavaPlugins.onError(ex);
throw ex;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;

Expand Down Expand Up @@ -54,12 +53,13 @@ public Void call() {
runner = Thread.currentThread();
try {
task.run();
setRest(executor.submit(this));
runner = null;
setRest(executor.submit(this));
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
// Exceptions.throwIfFatal(ex); nowhere to go
runner = null;
RxJavaPlugins.onError(ex);
throw ex;
}
return null;
}
Expand Down
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,10 +38,11 @@ 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);
dispose();
RxJavaPlugins.onError(ex);
throw ex;
}
}
}
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,16 @@ 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);
throw ex;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void run() {
} catch (Throwable e) {
// Exceptions.throwIfFatal(e); nowhere to go
RxJavaPlugins.onError(e);
throw e;
}
} finally {
lazySet(THREAD_INDEX, null);
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/io/reactivex/rxjava3/core/DisposeTaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/

package io.reactivex.rxjava3.core;

import static org.junit.Assert.fail;
import static org.testng.Assert.assertTrue;

import org.junit.Test;

import io.reactivex.rxjava3.core.Scheduler.DisposeTask;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.testsupport.TestHelper;

public class DisposeTaskTest extends RxJavaTest {

@Test
public void runnableThrows() throws Throwable {
TestHelper.withErrorTracking(errors -> {

Scheduler.Worker worker = Schedulers.single().createWorker();

DisposeTask task = new DisposeTask(() -> {
throw new TestException();
}, worker);

try {
task.run();
fail("Should have thrown!");
} catch (TestException expected) {
// expected
}

TestHelper.assertUndeliverable(errors, 0, TestException.class);

assertTrue(worker.isDisposed());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/

package io.reactivex.rxjava3.core;

import static org.junit.Assert.fail;
import static org.testng.Assert.assertTrue;

import java.util.List;

import org.junit.Test;

import io.reactivex.rxjava3.core.Scheduler.PeriodicDirectTask;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.testsupport.TestHelper;

public class PeriodicDirectTaskTest extends RxJavaTest {

@Test
public void runnableThrows() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
Scheduler.Worker worker = Schedulers.single().createWorker();

PeriodicDirectTask task = new PeriodicDirectTask(() -> {
throw new TestException();
}, worker);

try {
task.run();
fail("Should have thrown!");
} catch (TestException expected) {
// expected
}

TestHelper.assertUndeliverable(errors, 0, TestException.class);

assertTrue(worker.isDisposed());

task.run();
} finally {
RxJavaPlugins.reset();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/

package io.reactivex.rxjava3.internal.schedulers;

import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Test;

import io.reactivex.rxjava3.core.RxJavaTest;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.internal.schedulers.ExecutorScheduler.ExecutorWorker.BooleanRunnable;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.testsupport.TestHelper;

public class BooleanRunnableTest extends RxJavaTest {

@Test
public void runnableThrows() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
BooleanRunnable task = new BooleanRunnable(() -> {
throw new TestException();
});

try {
task.run();
fail("Should have thrown!");
} catch (TestException expected) {
// expected
}

TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public void run() {
}
}, exec);

assertNull(task.call());
try {
task.call();
fail("Should have thrown!");
} catch (TestException excepted) {
// excepted
}

TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/

package io.reactivex.rxjava3.internal.schedulers;

import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Test;

import io.reactivex.rxjava3.core.RxJavaTest;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.internal.schedulers.ExecutorScheduler.ExecutorWorker.InterruptibleRunnable;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.testsupport.TestHelper;

public class InterruptibleRunnableTest extends RxJavaTest {

@Test
public void runnableThrows() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
InterruptibleRunnable task = new InterruptibleRunnable(() -> {
throw new TestException();
}, null);

try {
task.run();
fail("Should have thrown!");
} catch (TestException expected) {
// expected
}

TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
}
Loading