diff --git a/platform-service-framework/src/main/java/org/hypertrace/core/serviceframework/PlatformService.java b/platform-service-framework/src/main/java/org/hypertrace/core/serviceframework/PlatformService.java index a241711..c14356b 100644 --- a/platform-service-framework/src/main/java/org/hypertrace/core/serviceframework/PlatformService.java +++ b/platform-service-framework/src/main/java/org/hypertrace/core/serviceframework/PlatformService.java @@ -133,12 +133,12 @@ public void start() { context.addServlet(new ServletHolder(new JVMDiagnosticServlet()), "/diags/*"); final Thread thread = new Thread(this::doStart); + thread.setUncaughtExceptionHandler( + (threadWithException, exception) -> this.shutdownWithError(exception)); try { thread.start(); } catch (Exception e) { - LOGGER.error("Failed to start thread for application.", e); - System.exit(1); - throw e; + this.shutdownWithError(e); } // Start the webserver. @@ -152,9 +152,7 @@ public void start() { thread.join(); adminServer.join(); } catch (Exception e) { - LOGGER.error("Failed to start service servlet."); - this.shutdown(); - System.exit(1); + this.shutdownWithError(e); } } @@ -190,4 +188,14 @@ public void shutdown() { PlatformMetricsRegistry.stop(); LOGGER.info("Service - {} is shutdown.", getServiceName()); } + + private void shutdownWithError(Throwable exception) { + LOGGER.error("Shutting down due to unrecoverable exception", exception); + try { + this.shutdown(); + } catch (Exception e) { + // Ignore if failed to shut down + } + System.exit(1); + } }