Catch exception in servlet context getter.

Request may finish after check has been perform but before context has
been read leading to exception in some implementations.
This commit is contained in:
Nikolay Martynov 2019-01-25 12:27:54 -05:00
parent 63779c7816
commit 4ba0adfab3
2 changed files with 12 additions and 2 deletions

View File

@ -81,7 +81,12 @@ public class JettyHandlerAdvice {
} else {
final AtomicBoolean activated = new AtomicBoolean(false);
if (req.isAsyncStarted()) {
req.getAsyncContext().addListener(new TagSettingAsyncListener(activated, span));
try {
req.getAsyncContext().addListener(new TagSettingAsyncListener(activated, span));
} catch (final IllegalStateException e) {
// org.eclipse.jetty.server.Request may throw an exception here if request became
// finished after check above. We just ignore that exception and move on.
}
}
if (!req.isAsyncStarted() && activated.compareAndSet(false, true)) {
Tags.HTTP_STATUS.set(span, resp.getStatus());

View File

@ -93,7 +93,12 @@ public class Servlet3Advice {
} else {
final AtomicBoolean activated = new AtomicBoolean(false);
if (req.isAsyncStarted()) {
req.getAsyncContext().addListener(new TagSettingAsyncListener(activated, span));
try {
req.getAsyncContext().addListener(new TagSettingAsyncListener(activated, span));
} catch (final IllegalStateException e) {
// org.eclipse.jetty.server.Request may throw an exception here if request became
// finished after check above. We just ignore that exception and move on.
}
}
// Check again in case the request finished before adding the listener.
if (!req.isAsyncStarted() && activated.compareAndSet(false, true)) {