tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 50353] New: Calling asyncContext.getResponse() returns null after async timeout
Date Sat, 27 Nov 2010 22:45:46 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

           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
        AssignedTo: dev@tomcat.apache.org
        ReportedBy: sylvain.laurent@gmail.com


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 java.io.IOException;
import java.io.PrintWriter;

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,
response);
        asyncContext.addListener(this);
        asyncContext.setTimeout(10*1000);

        asyncContext.start(new Runnable() {

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

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

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

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

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

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message