From d76c786803f2912b407b548d4b85d5de27dbafdd Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 14 Mar 2014 10:27:32 +1100 Subject: [PATCH] 430273 Cancel async timeout breaks volatile link to avoid race with slow expire --- .../eclipse/jetty/server/AsyncContextEvent.java | 16 +++++++++++++--- .../org/eclipse/jetty/server/HttpChannel.java | 3 ++- .../eclipse/jetty/server/HttpChannelState.java | 11 +---------- .../java/org/eclipse/jetty/server/Response.java | 8 +------- .../jetty/server/handler/ErrorHandler.java | 12 ++++++++++++ pom.xml | 2 +- 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextEvent.java b/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextEvent.java index ab71b226bfe..cc0eec89754 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextEvent.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextEvent.java @@ -29,14 +29,14 @@ import javax.servlet.ServletResponse; import org.eclipse.jetty.server.handler.ContextHandler.Context; import org.eclipse.jetty.util.thread.Scheduler; -public class AsyncContextEvent extends AsyncEvent +public class AsyncContextEvent extends AsyncEvent implements Runnable { final private Context _context; final private AsyncContextState _asyncContext; - volatile HttpChannelState _state; + private volatile HttpChannelState _state; private ServletContext _dispatchContext; private String _dispatchPath; - private Scheduler.Task _timeoutTask; + private volatile Scheduler.Task _timeoutTask; private Throwable _throwable; public AsyncContextEvent(Context context,AsyncContextState asyncContext, HttpChannelState state, Request baseRequest, ServletRequest request, ServletResponse response) @@ -143,6 +143,7 @@ public class AsyncContextEvent extends AsyncEvent public void completed() { + _timeoutTask=null; _asyncContext.reset(); } @@ -151,4 +152,13 @@ public class AsyncContextEvent extends AsyncEvent return _state; } + @Override + public void run() + { + Scheduler.Task task=_timeoutTask; + _timeoutTask=null; + if (task!=null) + _state.expired(); + } + } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java index 0718b23c2d0..f4172c3e6e5 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java @@ -307,7 +307,8 @@ public class HttpChannel implements HttpParser.RequestHandler, Runnable _response.setStatusWithReason(500,reason); - ErrorHandler eh = _state.getContextHandler().getErrorHandler(); + + ErrorHandler eh = ErrorHandler.getErrorHandler(getServer(),_state.getContextHandler()); if (eh instanceof ErrorHandler.ErrorPageMapper) { String error_page=((ErrorHandler.ErrorPageMapper)eh).getErrorPage((HttpServletRequest)_state.getAsyncContextEvent().getSuppliedRequest()); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelState.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelState.java index b7193b203e2..455edff54fc 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelState.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelState.java @@ -537,7 +537,7 @@ public class HttpChannelState { Scheduler scheduler = _channel.getScheduler(); if (scheduler!=null && _timeoutMs>0) - _event.setTimeoutTask(scheduler.schedule(new AsyncTimeout(),_timeoutMs,TimeUnit.MILLISECONDS)); + _event.setTimeoutTask(scheduler.schedule(_event,_timeoutMs,TimeUnit.MILLISECONDS)); } protected void cancelTimeout() @@ -691,13 +691,4 @@ public class HttpChannelState _channel.execute(_channel); } - public class AsyncTimeout implements Runnable - { - @Override - public void run() - { - HttpChannelState.this.expired(); - } - } - } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java index 7d92c5615ab..ec25a30e02c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java @@ -581,13 +581,7 @@ public class Response implements HttpServletResponse code!=SC_PARTIAL_CONTENT && code>=SC_OK) { - - ErrorHandler error_handler = null; - ContextHandler.Context context = request.getContext(); - if (context!=null) - error_handler=context.getContextHandler().getErrorHandler(); - if (error_handler==null) - error_handler = _channel.getServer().getBean(ErrorHandler.class); + ErrorHandler error_handler = ErrorHandler.getErrorHandler(_channel.getServer(),request.getContext()==null?null:request.getContext().getContextHandler()); if (error_handler!=null) { request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE,new Integer(code)); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java index 80c75721728..b1af5208a39 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ErrorHandler.java @@ -36,6 +36,7 @@ import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.server.Dispatcher; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.ByteArrayISO8859Writer; import org.eclipse.jetty.util.log.Log; @@ -304,4 +305,15 @@ public class ErrorHandler extends AbstractHandler { String getErrorPage(HttpServletRequest request); } + + /* ------------------------------------------------------------ */ + public static ErrorHandler getErrorHandler(Server server, ContextHandler context) + { + ErrorHandler error_handler=null; + if (context!=null) + error_handler=context.getErrorHandler(); + if (error_handler==null && server!=null) + error_handler = server.getBean(ErrorHandler.class); + return error_handler; + } } diff --git a/pom.xml b/pom.xml index 5bec6df4d7e..daef38ba1c5 100644 --- a/pom.xml +++ b/pom.xml @@ -414,7 +414,7 @@ jetty-servlet jetty-webapp jetty-spdy - jetty-fcgi + jetty-websocket jetty-servlets jetty-util-ajax