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

fluent: Response#saveContent(File) does not dispose response upon throwing

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 4.5.14, 5.1.4, 5.2-beta1
    • Fluent HC
    • None

    Description

       

      When throwing HttpResponseException, it does not dispose the underlying entity. This can cause conn pool to be stuck, imagine a normal retry loop using the same HttpClient. Perhaps it should be wrapped inside #handleResponse.

          public void saveContent(final File file) throws IOException {
              assertNotConsumed();
              final int status = response.getCode();
              if (status >= HttpStatus.SC_REDIRECTION) {
                  throw new HttpResponseException(status, response.getReasonPhrase());
              }
              try (FileOutputStream out = new FileOutputStream(file)) {
                  final HttpEntity entity = this.response.getEntity();
                  if (entity != null) {
                      entity.writeTo(out);
                  }
              } finally {
                  this.consumed = true;
              }
          }
      

      An alternative may be calling response.close(), which its behavior is documented here. I'm not sure how to check the "if supported" part though.

       

          /**
           * Returns a content stream of the entity.
      ...omitted...
           * <p>
           * If this entity belongs to an incoming HTTP message, calling
           * {@link InputStream#close()} on the returned {@code InputStream} will
           * try to consume the complete entity content to keep the connection
           * alive. In cases where this is undesired, e.g. when only a small part
           * of the content is relevant and consuming the complete entity content
           * would be too inefficient, <i>only</i> the HTTP message from which
           * this entity was obtained should be closed (if supported).
           * </p>
      ...omitted...
           */
          InputStream getContent() throws IOException, UnsupportedOperationException; 

       

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            robertabcd Robert Wang
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: