Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-1669

httpContext in ApacheHttpClient4Executor is not thread safe and causes NPEs

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 3.0.24.Final, 3.1.4.Final
    • 3.1.3.Final
    • jaxrs
    • None

      Under high load with many parallel client requests we get the following exception:

      Caused by: java.lang.NullPointerException
      at org.apache.http.message.HeaderGroup.getHeaders(HeaderGroup.java:184)
      at org.apache.http.message.AbstractHttpMessage.getHeaders(AbstractHttpMessage.java:75)
      at org.apache.http.impl.client.DefaultClientConnectionReuseStrategy.keepAlive(DefaultClientConnectionReuseStrategy.java:51)
      at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:274)
      at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
      at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
      at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
      at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
      at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283)

      We found the problem in the none-thread-safe usage of the httpContext in ApacheHttpClient4Engine.

      ApacheHttpClient4Engine keeps the httpContext as member variable and uses the same context for each execution
      of the HttpClient.

      The HttpClient uses the httpContext to store and transport temporary request parameters:

      see:
      org.apache.http.impl.execchain.ProtocolExec#execute() stores HttpCoreContext.HTTP_REQUEST
      org.apache.http.impl.client.DefaultClientConnectionReuseStrategy#keepAlive() retrieves HttpCoreContext.HTTP_REQUEST

      Under high load two threads working with the same request instance and this causes to the exception above.

      Our workaround:

      We wrapped the HttpClient and cloned the httpContext for each invocation of execute().

      There was already a ticket for this problem: RESTEASY-906.
      But it was closed without fixing.

            rhn-support-asoldano Alessio Soldano
            rene.preissel Rene Preissel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: