Core: LowLevelHttpResponse not disconnected when HttpResponse construction throws RuntimeException

オープン 初心者向け
#2,177 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
84/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
静か
技術スタック
java
領域
networking

調査の方向性

google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java から始め、HttpRequest.execute() とその finally ブロックに注目してください。getContentEncoding() が例外をスローする LowLevelHttpResponse で失敗を再現し、その後 testExecute_disconnectOnResponseConstructionFailure を実行してください。レスポンスの構築に失敗したときに低レベルレスポンスが切断され、回帰テストに合格すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Java
スター
1.4k
フォーク
473
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

googleapis/google-http-java-client のほかの issue

googleapis/google-http-java-client の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。