1

My testing indicates that Android is silently ignoring AssertionError when it is thrown in an Executor thread. Nothing is printed in logcat and the app doesn't crash. This seems wrong, are other people able to reproduce this as well? Any idea why this is happening or if there is a way to work-around or fix?

Here is my test:

public class TestActivity extends Activity {

  private final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    exec.execute(new Runnable() {
      @Override
      public void run() {
        System.err.println("about to fail");
        if (true ) throw new AssertionError();
      }
    });
  }
}
0

2 Answers 2

0

It's not just Android; this is (annoying) bog-standard Java behavior. ExecutorService threads where the runnable throws an uncaught exception simply terminate without logging.

The easiest workaround is to wrap your Executors like so - you can either do this by hand, or create a replacement ExecutorService that you use everywhere instead of the stock one:

public class PrintingExecutor implements ExecutorService {
  private final ExecutorService mDelegate;

  public PrintingExecutor (ExecutorService pDelegate) {
    mDelegate = pDelegate;
  }

  @Override
  public Future<?> submit(Runnable task) {
    return mDelegate.submit (new PrintingRunnable(task));
  }

  //override the rest of Executor's methods

  private static class PrintingRunnable implements Runnable {
    private final Runnable mDelegate; 
    public PrintingRunnable (Runnable toRun) {
      mDelegate = toRun;
    }

    @Override
    public void run () {
      try {
        mDelegate.run();
      } catch (Throwable t) {
        t.printStackTrace();
      }
    }
  } 
}
0

I believe this question duplicates Exception handling in ThreadPools and How to properly catch RuntimeExceptions from Executors?

So I think I understand what is going on now. This is not an Android specific issue, it is general Java. The issue has to do with how you schedule your task on the service.

If you use execute then the runtime exception will stop the service thread and VM will handle that by printing out the stacktrace and stopping the whole process (Unless you are using a Scheduled executor, if so see further down).

If you use submit then you are returned a Future, when you call get() on that Future it will throw java.util.concurrent.ExecutionException and you can inspect that to see what happened. In that case the service keeps processing tasks.

Thus the fix is to make sure you call get() on the Future if you are going to use submit, or just use execute().

HOWEVER:

Executors.newSingleThreadScheduledExecutor() is different from Executors.newSingleThreadExecutor). The Scheduled executor poses a further complication because it restarts the thread if it dies and it does so silently. So in that case you need to wrap your Runnable as suggested in this answer.

Here is some code demonstrating the difference:

  private final ScheduledExecutorService service = Executors.newSingleThreadExecutor();

  public static void testExecuteBomb(ExecutorService service) {
    service.execute(new Runnable() {
      @Override
      public void run() {
        printWithMillis("execute bombing...");
        if (true) throw new RuntimeException("Bomb");
      }
    });

    sleep();

    service.shutdownNow();
  }

  public static void testSubmitBomb(ExecutorService service) {
    try {
      Future t = service.submit(new Runnable() {
        @Override
        public void run() {
          printWithMillis("submit bombing...");
          if (true) throw new RuntimeException("Bomb");
        }
      });

      sleep();

      try {
        printWithMillis(String.valueOf(t.get()));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    } finally {
      service.shutdownNow();
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.