Skip to content

4.x fix status from deadlock health check if invoking the MBean fails #9694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023 Oracle and/or its affiliates.
* Copyright (c) 2018, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.lang.System.Logger.Level;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.Arrays;

import io.helidon.common.NativeImageHelper;
import io.helidon.health.HealthCheck;
Expand Down Expand Up @@ -95,17 +96,22 @@ public HealthCheckResponse call() {
.build();
}

boolean noDeadLock = false;
HealthCheckResponse.Builder builder = HealthCheckResponse.builder();

try {
// Thanks to https://stackoverflow.com/questions/1102359/programmatic-deadlock-detection-in-java#1102410
noDeadLock = (threadBean.findDeadlockedThreads() == null);
long[] deadlockedThreads = threadBean.findDeadlockedThreads();
if (deadlockedThreads != null) {
builder.status(Status.DOWN);
LOGGER.log(Level.TRACE, "Health check observed deadlocked threads: " + Arrays.toString(deadlockedThreads));
}
} catch (Throwable e) {
// ThreadBean does not work - probably in native image
LOGGER.log(Level.TRACE, "Failed to find deadlocks in ThreadMXBean, ignoring this healthcheck", e);
// ThreadBean does not work - probably in native image. Report ERROR, not DOWN, because we do not know that
// there are deadlocks which DOWN should imply; we simply cannot find out.
LOGGER.log(Level.TRACE, "Error invoking ThreadMXBean to find deadlocks; cannot complete this healthcheck", e);
builder.status(Status.ERROR);
}
return HealthCheckResponse.builder()
.status(noDeadLock ? Status.UP : Status.DOWN)
.build();
return builder.build();
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022 Oracle and/or its affiliates.
* Copyright (c) 2018, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,4 +63,13 @@ void noDeadlockDetected() {
HealthCheckResponse response = check.call();
assertThat(response.status(), is(HealthCheckResponse.Status.UP));
}

@Test

void errorInvokingMBean() {
Mockito.when(threadBean.findDeadlockedThreads()).thenThrow(new RuntimeException("Simulated error invoking MBean"));
DeadlockHealthCheck check = new DeadlockHealthCheck(threadBean);
HealthCheckResponse response = check.call();
assertThat(response.status(), is(HealthCheckResponse.Status.ERROR));
}
}
Loading