Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-1657

"Connection is not open" in httpClient.execute

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 4.4.1
    • None
    • None
    • None

    Description

      I have a program that needs to solve several problems. For each problem, it can try several different algorithms in parallel. As soon as one algorithm solves the problem, I would like the others to terminate promptly. Some algorithms are accessed via HTTP requests: you make a POST with the problem to a server, block, and the server returns an answer. If another thread finds the answer before the server returns, I would like that thread to cancel the http request.

      My code looks as follows

      Example.java
      // fields
              private final Lock lock;
              private final AtomicBoolean activeSolve;
              private HttpPost post;
              private final CloseableHttpClient httpClient = HttpClients.createDefault();
      
      
      public String makePost() {
          post = new HttpPost("some url");
          final StringEntity stringEntity = new StringEntity("some json string", ContentType.APPLICATION_JSON);
          post.setEntity(stringEntity);
          try {
              lock.lock();
              activeSolve.set(true);
              lock.unlock();
              try (final CloseableHttpResponse httpResponse = httpClient.execute(post)) {
                  final String response = EntityUtils.toString(httpResponse.getEntity());
                  return response;
              }
          } catch (IOException e) {
              if (post.isAborted()) {
                  log.trace("Web request was aborted");
                  return "aborted";
              } else {
                  throw new RuntimeException("Could not contact server", e);
              }
          } finally {
              lock.lock();
              activeSolve.set(false);
              lock.unlock();
          }
      }
      
         public void interrupt() {
              lock.lock();
              if (activeSolve.get()) {
                  post.abort();
              }
              lock.unlock();
          }
      
      
      Every ~1000 problems or so, I'll see the following stacktrace:
      java.lang.IllegalStateException: Connection is not open
      at org.apache.http.util.Asserts.check(Asserts.java:34)
      at org.apache.http.impl.BHttpConnectionBase.ensureOpen(BHttpConnectionBase.java:132)
      at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseEntity(DefaultBHttpClientConnection.java:177)
      at org.apache.http.impl.conn.CPoolProxy.receiveResponseEntity(CPoolProxy.java:172)
      at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:274)
      at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
      at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
      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:107)
      at ... my classes
      

      If I comment out the interrupt code and disable interrupting, I stop seeing this error.

      Am I using the library incorrectly? I can't figure out what circumstances cause this to occur.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              newmanne Neil Newman
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: