google / google/error-prone

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

Open
#988 2 comments 4 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

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 ?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.