diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ActiveStack.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ActiveStack.java index 7bce0fef45..cc27580699 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ActiveStack.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ActiveStack.java @@ -116,7 +116,9 @@ boolean deactivate(TraceStateImpl context, List activatio } TraceStateImpl activeContext = currentContext(); - activeContextStack.remove(); + if (!activeContextStack.isEmpty()) { + activeContextStack.remove(); + } try { assertIsActive(context, activeContext, assertionsEnabled); @@ -185,7 +187,9 @@ > T wrapActiveContextIfRequired(Class wrapperClas // replace the currently active on the stack, however currentContext() will make sure to return the original // context in order to keep wrapping transparent. - activeContextStack.remove(); + if (!activeContextStack.isEmpty()) { + activeContextStack.remove(); + } activeContextStack.push(wrapper); return wrapped;