tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject DO NOT REPLY [Bug 50353] New: Calling asyncContext.getResponse() returns null after async timeout
Date Sat, 27 Nov 2010 22:45:46 GMT

           Summary: Calling asyncContext.getResponse() returns null after
                    async timeout
           Product: Tomcat 7
           Version: trunk
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Catalina

If the async thread calls asyncContext.getResponse() after the async timeout,
it gets a null reference. 
In the following example, it leads to a NPE.

The servlet spec is not very clear on the behavior to adopt after a timeout,
but I don't think null should be returned. Maybe an IllegalStateException
instead ? It seems to be the case if complete() is called after the timeout.

package test;


import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * Servlet implementation class MyServlet
@WebServlet(value = "/MyServlet", asyncSupported = true)
public class MyServlet extends HttpServlet implements AsyncListener {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
        final AsyncContext asyncContext = request.startAsync(request,

        asyncContext.start(new Runnable() {

            public void run() {
                System.out.println("Entering async thread");
                try {
                    Thread.sleep(20 * 1000);
                    System.out.println("<Asyncthread> about to write
                    ServletResponse response2 = asyncContext.getResponse();
                    PrintWriter writer = response2.getWriter();
                    writer.write("Hello world");
                    System.out.println("<Asyncthread> about to complete");
                } catch (Exception e) {

    public void onComplete(AsyncEvent evt) throws IOException {
        System.out.println("onComplete " + evt);

    public void onError(AsyncEvent evt) throws IOException {
        System.out.println("onError " + evt);

    public void onStartAsync(AsyncEvent evt) throws IOException {
        System.out.println("onStartAsync " + evt);

    public void onTimeout(AsyncEvent evt) throws IOException {
        System.out.println("onTimeout " + evt);
        evt.getAsyncContext().getResponse().getWriter().write("Timed out");

Configure bugmail:
------- You are receiving this mail because: -------
You are the assignee for the bug.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message