Core: LowLevelHttpResponse not disconnected when HttpResponse construction throws RuntimeException

Đang mở Phù hợp với người mới
#2,177 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
84/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
java
Lĩnh vực
networking

Hướng nghiên cứu

Bắt đầu trong google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java, tập trung vào HttpRequest.execute() và khối finally của nó. Tái hiện lỗi bằng một LowLevelHttpResponse có getContentEncoding() ném ra ngoại lệ, sau đó chạy testExecute_disconnectOnResponseConstructionFailure. Hoàn tất khi phản hồi cấp thấp được ngắt kết nối nếu việc xây dựng phản hồi thất bại và kiểm thử hồi quy vượt qua.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Environment details:

  1. Core — HttpRequest.execute() in google-http-client
  2. OS type and version: Any (not OS-specific)
  3. Java version: Any (reproduces on Java 8+)
  4. google-http-client version(s): reproducible on current main

Steps to reproduce:

  1. Implement a LowLevelHttpResponse whose getContentEncoding() throws a RuntimeException.
  2. Execute an HttpRequest against that transport.
  3. Catch the RuntimeException from execute().
  4. Observe disconnect() was never called on the low-level response.

Code example:

MockLowLevelHttpResponse failingResponse = new MockLowLevelHttpResponse() {
  @Override
  public String getContentEncoding() {
    throw new RuntimeException("simulated failure");
  }
};

HttpTransport transport = new MockHttpTransport() {
  @Override
  public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
    return new MockLowLevelHttpRequest().setResponse(failingResponse);
  }
};

HttpRequest req = transport.createRequestFactory()
    .buildGetRequest(new GenericUrl("http://example.com"));

try {
  req.execute();
} catch (RuntimeException e) {
  // failingResponse.isDisconnected() == false  <-- BUG: socket leaked
}

Stack trace:
None — silent resource leak, not a crash. The RuntimeException propagates as expected; the
bug is that execute()'s finally block never calls LowLevelHttpResponse#disconnect() here.

External references:

  • HttpRequest#execute(): google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java

Any additional information:
Under sustained error conditions, leaked connections accumulate until the pool is exhausted,
risking DoS/thread starvation. Pre-existing bug, unrelated to security hardening — found
opportunistically during a security audit of this file.

Proposed fix — in execute()'s finally block, add a guarded disconnect():

} finally {
  if (!responseConstructed && lowLevelHttpResponse != null) {
    try {
      InputStream c = lowLevelHttpResponse.getContent();
      if (c != null) c.close();
    } catch (IOException ignored) {}
    try {
      lowLevelHttpResponse.disconnect();
    } catch (IOException ignored) {}
  }
}

Regression test testExecute_disconnectOnResponseConstructionFailure included in the
accompanying PR.

Ngôn ngữ chính
Java
Star
1.4k
Fork
473
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của googleapis/google-http-java-client

Tất cả issue của googleapis/google-http-java-client

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.