Skip to content

Commit

Permalink
Prevent recursion when logging recursive advice bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKunz committed Dec 10, 2024
1 parent 8b52d93 commit a5826fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public class IndyBootstrap {
static Method bootstrapLoggingMethod;

private static final CallDepth callDepth = CallDepth.get(IndyBootstrap.class);
private static final CallDepth loggingForNestedCall = CallDepth.get("indy-bootstrap-nested-logging");

private static Logger logger() {
// must not be a static field as it would initialize logging before it's ready
Expand Down Expand Up @@ -430,7 +431,12 @@ private static ConstantCallSite internalBootstrap(MethodHandles.Lookup lookup, S
// avoid re-entrancy and stack overflow errors
// may happen when bootstrapping an instrumentation that also gets triggered during the bootstrap
// for example, adding correlation ids to the thread context when executing logger.debug.
logger().warn("Nested instrumented invokedynamic instruction linkage detected", new Throwable());
if (!loggingForNestedCall.isNestedCallAndIncrement()) {
// We might be unlucky and cause an infinite recurison through logger()
// for this reason we have the loggingForNestedCall in place to prevent this recursion
logger().warn("Nested instrumented invokedynamic instruction linkage detected", new Throwable());
}
loggingForNestedCall.decrement();
return null;
}
String adviceClassName = (String) args[0];
Expand Down Expand Up @@ -498,7 +504,7 @@ private static ConstantCallSite internalBootstrap(MethodHandles.Lookup lookup, S
// When calling findStatic now, the lookup class will be one that is loaded by the plugin class loader
MethodHandle methodHandle = indyLookup.findStatic(adviceInPluginCL, adviceMethodName, adviceMethodType);
return new ConstantCallSite(methodHandle);
} catch (Exception e) {
} catch (Throwable e) {
logger().error(e.getMessage(), e);
return null;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ private CallDepth() {
public static CallDepth get(Class<?> adviceClass) {
// we want to return the same CallDepth instance even if the advice class has been loaded from different class loaders
String key = adviceClass.getName();
return get(key);
}

public static CallDepth get(String key) {
CallDepth callDepth = registry.get(key);
if (callDepth == null) {
registry.putIfAbsent(key, new CallDepth());
Expand Down

0 comments on commit a5826fe

Please sign in to comment.