google / error-prone

Catch common Java mistakes as compile-time errors
https://errorprone.info
Apache License 2.0
6.82k stars 740 forks source link

False positive for FutureReturnValueIgnored on CompletableFuture.runAsync(...).exceptionally(...) #988

Open adrian-baker opened 6 years ago

adrian-baker commented 6 years ago

I had some code like this:

final ExecutorService executor = Executors.newSingleThreadExecutor();
final Runnable runnable = () -> { // lots of stuff };
executor.submit(runnable);

The FutureReturnValueIgnored check rightly highlighted that an exception from the Runnable would be lost.

I fixed this with java.util.concurrent.CompletableFuture:

CompletableFuture.runAsync( runnable, executor).exceptionally(t -> {
  LOG.error("...", t);
  return null;
});

However, the check still flags this as a problem. Admittedly there is still the risk that an exception in the exceptionally block would be thrown and lost.

Perhaps this could be treated similarly to io.netty.channel.ChannelFuture#addListener ?

pkoenig10 commented 3 years ago

This should be fixed by https://github.com/google/error-prone/pull/1154.

kilink commented 3 years ago

I think the same issue exists with whenComplete, handle, and handleAsync.