From 24c3bc83b323d7fddd43ea0a40c31da8510c2710 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Wed, 22 Sep 2021 22:29:24 -0700 Subject: [PATCH] Fix JDBC instrumentation deadlock --- .../instrumentation/jdbc/internal/JdbcUtils.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcUtils.java b/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcUtils.java index 20672f74b9d4..ee2a45cf5d93 100644 --- a/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcUtils.java +++ b/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcUtils.java @@ -65,7 +65,16 @@ public static Connection connectionFromStatement(Statement statement) { } public static DbInfo extractDbInfo(Connection connection) { - return JdbcData.connectionInfo.computeIfAbsent(connection, JdbcUtils::computeDbInfo); + // intentionally not using computeIfAbsent() since that would perform computeDbInfo() under a + // lock, and computeDbInfo() calls back to the application code via Connection.getMetaData() + // which could then result in a deadlock + // (e.g. https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/4188) + DbInfo dbInfo = JdbcData.connectionInfo.get(connection); + if (dbInfo == null) { + dbInfo = computeDbInfo(connection); + JdbcData.connectionInfo.put(connection, dbInfo); + } + return dbInfo; } public static DbInfo computeDbInfo(Connection connection) {