diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 0bd971e1d1..fc6a47d782 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -40,6 +40,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes: [float] ===== Bug fixes * Fixed SQS NoClassDefFoundError in AWS SDK instrumentation - {pull}3254[#3254] +* Fixed reference counting issues in elasticsearch instrumentation - {pull}3256[#3256] [[release-notes-1.x]] === Java Agent version 1.x diff --git a/apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-common/src/main/java/co/elastic/apm/agent/esrestclient/ResponseListenerWrapper.java b/apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-common/src/main/java/co/elastic/apm/agent/esrestclient/ResponseListenerWrapper.java index 90e0fed029..f0df0a31a7 100644 --- a/apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-common/src/main/java/co/elastic/apm/agent/esrestclient/ResponseListenerWrapper.java +++ b/apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-common/src/main/java/co/elastic/apm/agent/esrestclient/ResponseListenerWrapper.java @@ -53,7 +53,7 @@ ResponseListenerWrapper withClientSpan(ResponseListener delegate, Span span) // Order is important due to visibility - write to span last on this (initiating) thread this.delegate = delegate; this.isClientSpan = true; - this.context = span; + setContext(span); return this; } @@ -61,7 +61,7 @@ ResponseListenerWrapper withContextPropagation(ResponseListener delegate, Abstra // Order is important due to visibility - write to span last on this (initiating) thread this.delegate = delegate; this.isClientSpan = false; - this.context = context; + setContext(context); return this; } @@ -149,10 +149,20 @@ private void finishClientSpan(@Nullable Response response, @Nullable Throwable t } } + private void setContext(@Nullable AbstractSpan newContext) { + if (newContext != null) { + newContext.incrementReferences(); + } + if (context != null) { + context.decrementReferences(); + } + context = newContext; + } + @Override public void resetState() { delegate = null; - context = null; + setContext(null); isClientSpan = false; } }