Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] handling the same request multiple times?

You startAsync fine, but then don't test to see if it was started before.
That handler is reentrant for a single request processing.
The behavior depends on how the suspend / resume works out.

You might want to ensure that you don't attempt to start the AsyncContext twice.
There are many different ways to accomplish this.

One possible approach.

AsyncContext context = (AsyncContext) request.getAttribute(AsyncContext.class.getName());
if(context == null) {
    context = request.startAsync();
    request.setAttribute(AsyncContext.class.getName(), context);
    ServletInputStream inputStream = request.getInputStream();
    inputStream.setReadListener(new ReadListener() { ... });
} else {
    System.out.println("Re-entrant AsyncContext");
    // do what you want/need here.
    // this could even be an error handling routine.

Also of note, since you are using a naked Handler, it will always be called.
Even if you have something after that handler, or a RequestDispatcher call, or an error handling, etc ...

Joakim Erdfelt / joakim@xxxxxxxxxxx

On Mon, Dec 21, 2015 at 5:39 AM, Robben, Bert <Bert.Robben@xxxxxxxxxxxxx> wrote:

When I put my Jetty server under high load, I sometimes get IllegalStateException being thrown mentioning “ReadListener already set”. These exceptions are thrown when a ReadListener is set more than once to the inputstream of an incoming request. However, I never do that. I can only explain this if the same request is sent to my handler multiple times, which I don’t really understand. Can somebody explain how this can happen so that I can avoid these exceptions? Note that I don’t get these exception under light load.


Here’s some more background:


(1)          I’m using jetty 9.3.6.v20151106

(2)          I’m running an embedded Jetty

(3)          I only have a single request handler. It extends AbstractHandler and does the following:


       public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

             String id = request.getRequestURI().substring(1);


             long count = requests.decrementAndGet();

             if (count < 0) {





             AsyncContext context = request.startAsync();

            ServletInputStream inputStream = request.getInputStream();

             inputStream.setReadListener(new ReadListener() { ... });




So as you can see, all requests are handled asynchronously unless my server is overloaded (which is not the case when the exceptions occur).







PS: Here’s the complete stacktrace


java.lang.IllegalStateException: ReadListener already set

        at org.eclipse.jetty.server.HttpInput.setReadListener( ~[opf-0.0.1-SNAPSHOT-allinone.jar:na]

        at ~[opf-0.0.1-SNAPSHOT-allinone.jar:na]

        at org.eclipse.jetty.server.handler.HandlerWrapper.handle( ~[opf-0.0.1-SNAPSHOT-allinone.jar:na]

        at org.eclipse.jetty.server.Server.handleAsync( ~[opf-0.0.1-SNAPSHOT-allinone.jar:na]

        at org.eclipse.jetty.server.HttpChannel.handle( [opf-0.0.1-SNAPSHOT-allinone.jar:na]

        at [opf-0.0.1-SNAPSHOT-allinone.jar:na]

        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob( [opf-0.0.1-SNAPSHOT-allinone.jar:na]

        at org.eclipse.jetty.util.thread.QueuedThreadPool$ [opf-0.0.1-SNAPSHOT-allinone.jar:na]

        at [na:1.8.0_66]

12:27:09.743 [qtp2027944996-257] WARN  org.eclipse.jetty.server.HttpChannel - ERROR_DISPATCH loop detected on Request[POST //be-nft4-app-04:8080/590715314303067]@5928fccb java.lang.IllegalStateException: ReadListener already set

12:27:09.743 [qtp2027944996-309] WARN  org.eclipse.jetty.server.HttpChannel - ERROR_DISPATCH loop detected on Request[POST //be-nft4-app-04:8080/590715314304262]@7d407fdb java.lang.IllegalStateException: ReadListener already set


The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.

jetty-users mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit

Back to the top