You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by bu...@apache.org on 2010/11/27 23:45:46 UTC

DO NOT REPLY [Bug 50353] New: Calling asyncContext.getResponse() returns null after async timeout

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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

Sylvain Laurent <sy...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|trunk                       |7.0.5

-- 
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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

--- Comment #6 from Mark Thomas <ma...@apache.org> 2011-08-15 18:37:30 UTC ---
NPE is also unchecked so the end result is the same and now there is a clearer
explanation of what went wrong.

Any API changes will require a change to the Servlet specification. For that,
you'll need to lobby the Servlet EG. It is on my list of issues to raise but it
won't hurt if you raise it first.

-- 
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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

--- Comment #5 from David <da...@msn.com> 2011-08-15 18:32:35 UTC ---
It now throws IllegalStateException which is an unchecked exception.

I don't think i'm splitting hairs here, that I strongly believe it should be
throwing a checked exception.

Maybe there's a deficiency with the API, but in the application code, there's
no way that I know to handle this case properly without handling the unchecked
exception (an unchecked exception is supposed to be a bug e.g.
nullpointerException).

The asyncContext error/timeout handler cannot be atomically synchronized with
the causal event so in a Thread race between error/timout and writing a
response it is possible to get this unchecked exsception and there is no proper
way to write the code.

So really it has to ba a checked exception, and the API altered if necessary.

-- 
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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

Mark Thomas <ma...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |zhh200910@gmail.com

--- Comment #3 from Mark Thomas <ma...@apache.org> 2011-06-28 17:23:38 UTC ---
*** Bug 51432 has been marked as a duplicate of this bug. ***

-- 
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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

--- Comment #1 from Sylvain Laurent <sy...@gmail.com> 2010-11-27 18:04:53 EST ---
Same behavior with getRequest() : it returns null. 
This leads to a NPE if asyncContext.dispatch() is called after the timeout :


java.lang.NullPointerException
    at
org.apache.catalina.core.AsyncContextImpl.dispatch(AsyncContextImpl.java:127)
    at test.MyServlet$1.run(MyServlet.java:42)
    at
org.apache.catalina.core.AsyncContextImpl$RunnableWrapper.run(AsyncContextImpl.java:439)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)

(with a variation of the above example)

-- 
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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

Mark Thomas <ma...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

--- Comment #2 from Mark Thomas <ma...@apache.org> 2010-11-29 12:22:10 EST ---
Hmm. This is further complicated by the fact that Tomcat recycles Request and
Response objects so they can't be kept around forever. I have added this to me
list of things to nag the Servlet EG about.

I'm currently neutral on whether or not the current behaviour should change.

Switching to an enhancement since I don't see an actual bug (as in non-spec
compliant behaviour) here.

-- 
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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

--- Comment #8 from Mark Thomas <ma...@apache.org> 2011-08-15 18:53:33 UTC ---
http://java.net/projects/servlet-spec/lists and post to the users list.

-- 
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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

--- Comment #7 from David <da...@msn.com> 2011-08-15 18:51:00 UTC ---
Agreed - null is not specified (by the API) though it did provide a route in
setting a local var and then testing for null (assuming the reference passed
had volatile handling).

Though without API support (unlikely) the checked exception idea seems a better
way. Then the application code would handle it like e.g. java.io.IOException.

How do I "lobby the Servlet EG"?

-- 
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


DO NOT REPLY [Bug 50353] Calling asyncContext.getResponse() returns null after async timeout

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=50353

Mark Thomas <ma...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED

--- Comment #4 from Mark Thomas <ma...@apache.org> 2011-06-28 17:25:04 UTC ---
This has been fixed in trunk and will be included in 7.0.17 onwards.

-- 
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