Skip to content

Commit d48ed0b

Browse files
authored
Task may be unregistered outside of the trace context in exceptional cases. (#137865) (#138004)
This change makes sure we're stopping traces in these cases so we don't risk leaking spans in APMTracer.
1 parent 56b8ea5 commit d48ed0b

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

server/src/main/java/org/elasticsearch/tasks/TaskManager.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,6 @@ void maybeStartTrace(ThreadContext threadContext, Task task) {
191191
tracer.startTrace(threadContext, task, task.getAction(), attributes);
192192
}
193193

194-
void maybeStopTrace(ThreadContext threadContext, Task task) {
195-
if (threadContext.hasTraceContext()) {
196-
tracer.stopTrace(task);
197-
}
198-
}
199-
200194
public <Request extends ActionRequest, Response extends ActionResponse> Task registerAndExecute(
201195
String type,
202196
TransportAction<Request, Response> action,
@@ -358,7 +352,7 @@ public Task unregister(Task task) {
358352
return removedTask;
359353
}
360354
} finally {
361-
maybeStopTrace(threadPool.getThreadContext(), task);
355+
tracer.stopTrace(task); // stop trace if started / known by tracer
362356
for (RemovedTaskListener listener : removedTaskListeners) {
363357
listener.onRemoved(task);
364358
}

server/src/test/java/org/elasticsearch/tasks/TaskManagerTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import static org.mockito.Mockito.times;
7070
import static org.mockito.Mockito.verify;
7171
import static org.mockito.Mockito.verifyNoInteractions;
72+
import static org.mockito.Mockito.verifyNoMoreInteractions;
7273
import static org.mockito.Mockito.when;
7374

7475
public class TaskManagerTests extends ESTestCase {
@@ -311,6 +312,9 @@ public TaskId getParentTask() {
311312
? Map.of(Tracer.AttributeKeys.TASK_ID, task.getId(), Tracer.AttributeKeys.PARENT_TASK_ID, parentTask.toString())
312313
: Map.of(Tracer.AttributeKeys.TASK_ID, task.getId());
313314
verify(mockTracer).startTrace(any(), eq(task), eq("testAction"), eq(attributes));
315+
316+
taskManager.unregister(task);
317+
verify(mockTracer).stopTrace(task); // always attempt stopping to guard against leaks
314318
}
315319
}
316320

@@ -393,7 +397,8 @@ public TaskId getParentTask() {
393397
// no trace context
394398

395399
taskManager.unregister(task);
396-
verifyNoInteractions(mockTracer);
400+
verify(mockTracer).stopTrace(task); // always attempt stopping to guard against leaks
401+
verifyNoMoreInteractions(mockTracer);
397402
}
398403

399404
/**
@@ -438,6 +443,7 @@ public TaskId getParentTask() {
438443
);
439444

440445
verify(mockTracer).startTrace(any(), eq(task), eq("actionName"), anyMap());
446+
verify(mockTracer).stopTrace(task); // always attempt stopping to guard against leaks
441447
}
442448

443449
/**
@@ -480,7 +486,8 @@ public TaskId getParentTask() {
480486
ActionTestUtils.assertNoFailureListener(r -> {})
481487
);
482488

483-
verifyNoInteractions(mockTracer);
489+
verify(mockTracer).stopTrace(task); // always attempt stopping to guard against leaks
490+
verifyNoMoreInteractions(mockTracer);
484491
}
485492

486493
public void testRegisterWithEnabledDisabledTracing() {

0 commit comments

Comments
 (0)