Skip to content

Commit 92a857d

Browse files
authored
Safely set context classloader on final blocks (#9795)
1 parent 1ace833 commit 92a857d

File tree

1 file changed

+12
-4
lines changed
  • dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap

1 file changed

+12
-4
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/Agent.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ public boolean isEnabledByDefault() {
184184
private static boolean distributedDebuggerEnabled = false;
185185
private static boolean agentlessLogSubmissionEnabled = false;
186186

187+
private static void safelySetContextClassLoader(ClassLoader classLoader) {
188+
try {
189+
// this method call can cause a SecurityException if a security manager is installed.
190+
Thread.currentThread().setContextClassLoader(classLoader);
191+
} catch (final Throwable ignored) {
192+
}
193+
}
194+
187195
/**
188196
* Starts the agent; returns a boolean indicating if Agent started successfully
189197
*
@@ -924,7 +932,7 @@ private static synchronized void startJmxFetch() {
924932
} catch (final Throwable ex) {
925933
log.error("Throwable thrown while starting JmxFetch", ex);
926934
} finally {
927-
Thread.currentThread().setContextClassLoader(contextLoader);
935+
safelySetContextClassLoader(contextLoader);
928936
}
929937
}
930938

@@ -1305,7 +1313,7 @@ private static boolean startProfilingAgent(
13051313
} catch (final Throwable ex) {
13061314
log.error(SEND_TELEMETRY, "Throwable thrown while starting profiling agent", ex);
13071315
} finally {
1308-
Thread.currentThread().setContextClassLoader(contextLoader);
1316+
safelySetContextClassLoader(contextLoader);
13091317
}
13101318
StaticEventLogger.end("ProfilingAgent");
13111319
}
@@ -1349,7 +1357,7 @@ private static void shutdownProfilingAgent(final boolean sync) {
13491357
} catch (final Throwable ex) {
13501358
log.error("Throwable thrown while shutting down profiling agent", ex);
13511359
} finally {
1352-
Thread.currentThread().setContextClassLoader(contextLoader);
1360+
safelySetContextClassLoader(contextLoader);
13531361
}
13541362
}
13551363

@@ -1383,7 +1391,7 @@ private static synchronized void startDebuggerAgent(
13831391
} catch (final Throwable ex) {
13841392
log.error("Throwable thrown while starting debugger agent", ex);
13851393
} finally {
1386-
Thread.currentThread().setContextClassLoader(contextLoader);
1394+
safelySetContextClassLoader(contextLoader);
13871395
}
13881396

13891397
StaticEventLogger.end("Debugger");

0 commit comments

Comments
 (0)