google / guava

Google core libraries for Java
Apache License 2.0
50.23k stars 10.91k forks source link

Service "swallows" exceptions #3835

Open hupfdule opened 4 years ago

hupfdule commented 4 years ago

Consider this code nippet:

public class ServiceTest {
  private static class ServiceA extends AbstractIdleService {

    @Override
    protected void startUp() throws Exception {
      System.out.println("Starting A");
      if (true) {
        throw new RuntimeException("Sorry…");
      }
      System.out.println("Finished starting A");
    }

    @Override
    protected void shutDown() throws Exception {
      System.out.println("Stopping A");
    }
  }

  public static void main(String[] args) throws Exception {
    final ServiceA serviceA= new ServiceA();
    serviceA.startAsync();
    // serviceA.awaitRunning();
    Thread.sleep(1000);
  }
}

Unless I activate the serviceA.awaitRunning() (or implement an explicit Listener) the exception thrown when starting up just vanishes. It doesn't even get logged or printed to stdout.

I don't consider that a sane default behaviour as it makes it harder to spot actual problems in the codebase.

cpovirk commented 4 years ago

Sorry, I was sure that I had responded here yesterday.

Thanks for reporting. I agree that this is unfortunate, but I'm not sure we can do better: In cases in which we do log (like Futures.allAsList), this also causes problems for users.

We can document this better, perhaps at the same time as we recommend that users use ServiceManager.

hupfdule commented 4 years ago

No problem. One day later is still a fast response. :-)

I agree. If this behaviour is not going to be changed, it should be clearly documented so that users are really aware of it.