From be801fee41d67b4215c147c0afa744a7dc60aa62 Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Tue, 20 Feb 2024 15:07:34 +0100 Subject: [PATCH] SmallRye Health: terminate request context properly When the SmallRye Health route handler activates request context on its own, it also needs to terminate it. However, that termination needs to happen only after all health checks complete. That's what this commit does. --- .../health/runtime/SmallRyeHealthHandlerBase.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 e9993754187690..c5e465175618bf 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);