diff --git a/extensions/smallrye-health/runtime/src/main/java/io/quarkus/smallrye/health/runtime/SmallRyeHealthHandlerBase.java b/extensions/smallrye-health/runtime/src/main/java/io/quarkus/smallrye/health/runtime/SmallRyeHealthHandlerBase.java index e999375418769..c24dcfb60cc2b 100644 --- a/extensions/smallrye-health/runtime/src/main/java/io/quarkus/smallrye/health/runtime/SmallRyeHealthHandlerBase.java +++ b/extensions/smallrye-health/runtime/src/main/java/io/quarkus/smallrye/health/runtime/SmallRyeHealthHandlerBase.java @@ -28,18 +28,19 @@ abstract class SmallRyeHealthHandlerBase implements Handler { public void handle(RoutingContext ctx) { ManagedContext requestContext = Arc.container().requestContext(); if (requestContext.isActive()) { - doHandle(ctx); + doHandle(ctx, null); } else { requestContext.activate(); try { - doHandle(ctx); - } finally { + doHandle(ctx, requestContext); + } catch (Exception e) { requestContext.terminate(); + throw e; } } } - private void doHandle(RoutingContext ctx) { + private void doHandle(RoutingContext ctx, ManagedContext requestContext) { QuarkusHttpUser user = (QuarkusHttpUser) ctx.user(); if (user != null) { Arc.container().instance(CurrentIdentityAssociation.class).get().setIdentity(user.getSecurityIdentity()); @@ -48,6 +49,9 @@ private void doHandle(RoutingContext ctx) { Context context = Vertx.currentContext(); getHealth(reporter, ctx).emitOn(MutinyHelper.executor(context)) .subscribe().with(health -> { + if (requestContext != null) { + requestContext.terminate(); + } HttpServerResponse resp = ctx.response(); if (health.isDown()) { resp.setStatusCode(503); @@ -60,6 +64,10 @@ private void doHandle(RoutingContext ctx) { } catch (IOException e) { throw new UncheckedIOException(e); } + }, failure -> { + if (requestContext != null) { + requestContext.terminate(); + } }); } }