From 6bf6bdd0b8f76184b917bf2386ab72e63d19b509 Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Mon, 7 Oct 2024 12:16:45 +0100 Subject: [PATCH] guard stack remove with isEmpty --- .../main/java/co/elastic/apm/agent/impl/ActiveStack.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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;