Skip to content

Commit 6aa716c

Browse files
committed
Fix race condition in getXOpaqueId in tests
Due to a race condition between isClosed and getHeader it is possible that getXOpaqueId can throw an IllegalStateException. This only occurs when run with multiple tests on the same JVM. This value is not to be asserted in these tests. Only when running in isolation in its own JVM. This won't happen in production as we only expect one ThreadContext per Node (jvm) closes elastic#45443
1 parent 808345e commit 6aa716c

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

server/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,19 @@ public Void run() {
261261
}
262262

263263
public String getXOpaqueId(Set<ThreadContext> threadContexts) {
264-
return threadContexts.stream()
265-
.filter(t -> t.isClosed() == false)
266-
.filter(t -> t.getHeader(Task.X_OPAQUE_ID) != null)
267-
.findFirst()
268-
.map(t -> t.getHeader(Task.X_OPAQUE_ID))
269-
.orElse("");
264+
for (ThreadContext threadContext : threadContexts) {
265+
try {
266+
if (threadContext.isClosed() == false) {
267+
String header = threadContext.getHeader(Task.X_OPAQUE_ID);
268+
if (header != null) {
269+
return header;
270+
}
271+
}
272+
} catch (IllegalStateException e) {
273+
// ignore exception as this is due to a race condition between isClosed and getHeader
274+
}
275+
}
276+
return "";
270277
}
271278

272279
/**

0 commit comments

Comments
 (0)