Jetty+Jersey infinite loop with curl post query

2019-02-18 14:20发布

问题:

Sorry for the long post, I need to share every line of code.

I have created a JAX-RS application using Jersey hosted over jetty server. My following code however is going for infinite loop. Jetty keep posting messages and eats away the memory. This happens only when connected to CURL client.

Jetty-Server:

public class HttpServer {
  public static void main(String[] args) throws Exception
  {
     //Set JAX-RS
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    context.setContextPath("/");
    Server server = new Server(8888);
    server.setHandler(context);
    ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);
    // Tells the Jersey Servlet which REST service/class to load.
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",TestResource.class.getCanonicalName());

    try {
      server.start();
         while(true){
      server.join();
      logger.log(Level.WARNING,"HTTP Server Killed. Restarting");
      server.start();
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      logger.log(Level.SEVERE,"EXCPETION DETECTED:" +e.hashCode());
    }

  }


}

Jax-RS Resource

@Path("/test")

public class TestResource
{

  @POST
  @Consumes("application/json")
  @Produces("text/html")
  public Response fillData(String json)
  {
    System.out.println(json);

    return Response.status(200).entity("Successfully updated").build();

  }

}

Things To Note:

  1. curl query : curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:8888/test
  2. Posting a query using crome-poster doesn't cause this issue. Only happens with CURL!
  3. CURL hets response OK and safely exits, but jetty keep up in loop.
  4. I tried multiple content-type but issue persists.
  5. Issue is resolved by downgrading jetty from 9.3.0.M1 to 9.2.0.M1

Debug Logs at infinite loop:

11:21:40.760 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop woken up from select, 0/1 selected
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Running change org.eclipse.jetty.io.SelectChannelEndPoint$1@54ddf089
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Updating key, state UPDATE_PENDING for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=1,kio=0,kro=1}
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Key interests update 0 -> 1 for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=1,kio=0,kro=1}
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop waiting on select
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop woken up from select, 1/1 selected
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Processing, state UPDATED for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=1,kio=1,kro=1}
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - onSelected 1->0 for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=0,kio=1,kro=1}
11:21:40.761 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.AbstractConnection - FILL_INTERESTED-->FILLING HttpConnection@21c7226e{FILLING}
11:21:40.762 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Updating key, state UPDATE_PENDING for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,-,-,30000,HttpConnection}{io=0,kio=1,kro=1}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.server.HttpConnection - HttpConnection@21c7226e{FILLING} onFillable HttpChannelState@36dadde6{s=IDLE i=true a=null}
11:21:40.762 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG o.e.jetty.io.SelectChannelEndPoint - Key interests update 1 -> 0 for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,-,-,30000,HttpConnection}{io=0,kio=1,kro=1}
11:21:40.762 [qtp1669854350-19] DEBUG org.eclipse.jetty.http.HttpParser - atEOF HttpParser{s=CLOSED,0 of -1}
11:21:40.762 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop waiting on select
11:21:40.762 [qtp1669854350-19] DEBUG org.eclipse.jetty.http.HttpParser - parseNext s=CLOSED HeapByteBuffer@1[p=0,l=0,c=0,r=0]={<<<>>>}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.AbstractConnection - fillInterested HttpConnection@21c7226e{FILLING}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.AbstractConnection - FILLING-->FILLING_FILL_INTERESTED HttpConnection@21c7226e{FILLING_FILL_INTERESTED}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.AbstractConnection - FILLING_FILL_INTERESTED-->FILL_INTERESTED HttpConnection@21c7226e{FILL_INTERESTED}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.SelectChannelEndPoint - Changing interests in state UPDATED for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=0,kio=0,kro=1}
11:21:40.762 [qtp1669854350-19] DEBUG o.e.jetty.io.SelectChannelEndPoint - changeInterests s=UPDATED 0->1 for SelectChannelEndPoint@55446de5{/127.0.0.1:55160<->8888,Open,ISHUT,out,R,-,30000,HttpConnection}{io=0,kio=0,kro=1}
11:21:40.762 [qtp1669854350-19] DEBUG org.eclipse.jetty.io.SelectorManager - Queued change org.eclipse.jetty.io.SelectChannelEndPoint$1@54ddf089
11:21:40.762 [qtp1669854350-14-selector-ServerConnectorManager@62dfb098/0] DEBUG org.eclipse.jetty.io.SelectorManager - Selector loop woken

I can't make much sense out of the logs but one thing is certain that Jetty is not closing connection even when the client has safely exited. I tried setting timeout at jetty, but of no use.

Is there any issue with the code, or there seems to be some bug with newest version of Jetty. Note that I am totally naive in developing JAVA EE Applications. If that is a bug, I will report in Jetty Forums.

回答1:

This issue has already been identified as https://bugs.eclipse.org/bugs/show_bug.cgi?id=452465 and it's already fixed in the master branch, and will be part of Jetty 9.3.0.M2.



标签: jersey Jetty