`PhantomReachableExecutorService` is never cleaned up because its Cleaner action captures the wrapper
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
調査の方向性
まず PhantomReachableExecutorService と、issue に示されている closeWhenPhantomReachable の登録を確認します。アクションが delegate をどのように捕捉するかを調べてから、executor のクリーンアップと弱参照に関する既存のテストを探します。wrapper が到達不能になった後に回収され、executor がシャットダウンされれば完了です。提案された修正はコンパイルも実行もされていません。
索引モデルが issue の本文から書いたものです。
説明
Problem
PhantomReachableExecutorService is meant to shut down its executor once the wrapper is only phantom reachable.
It can't, because the action registered with the Cleaner holds a strong reference to the wrapper:
internal class PhantomReachableExecutorService(private val executorService: ExecutorService) :
ExecutorService {
init {
closeWhenPhantomReachable(this) { executorService.shutdown() }
}
Here executorService compiles to a read of the private property through this.
The generated lambda has a this$0 field, as javap -p -c 'com.openai.core.PhantomReachableExecutorService$1' shows on 4.49.0, 4.80.0 and 4.99.0:
final com.openai.core.PhantomReachableExecutorService this$0;
...
invokestatic com/openai/core/PhantomReachableExecutorService.access$getExecutorService$p
invokeinterface java/util/concurrent/ExecutorService.shutdown:()V
The Cleaner holds the action strongly, so the wrapper and its executor stay reachable for the life of the JVM.
Impact
ClientOptions.Builder.build() creates this wrapper by default, and streamHandlerExecutor(...) applies it to any ExecutorService.
So every client strands one wrapper and one ThreadPoolExecutor, whether or not it ever streams.
Calling close() shuts the pool down but doesn't release it.
This matters to applications that build clients repeatedly, for example one per tenant API key.
Reproduction
Build 2,000 sync and async client pairs, discard them, then take a class histogram with jcmd <pid> GC.class_histogram, which forces a full GC.
| Client setup | Retained PhantomReachableExecutorService |
|---|---|
| Default | 4,000 |
Default, calling close() on each client |
4,000 |
streamHandlerExecutor given a plain Executor |
0 |
Clients, sleepers and HTTP client wrappers were released in all three.
Suggested fix
Make the action capture the delegate rather than the wrapper, for example by registering it from a function that doesn't have this in scope, or by copying the delegate into a local first:
init {
val delegate = executorService
closeWhenPhantomReachable(this) { delegate.shutdown() }
}
This suggestion hasn't been compiled or run.
A regression test could register a wrapper, drop it, and assert through a WeakReference that it's collected after System.gc(), or that the delegate's isShutdown becomes true.
Workaround
Pass an Executor that isn't an ExecutorService to ClientOptions.Builder.streamHandlerExecutor(...).
- 主要言語
- Kotlin
- スター
- 1.6k
- フォーク
- 275
- 平均マージ
- 10時間 37分
- マージ済み PR(30日)
- 171
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
openai/openai-java のほかの issue
-
HttpRequest.url() logs spaces in path segments as literal plus signs対応中かも @sylvesterkaczmarek が 79 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
openai/openai-java#886 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Make diarized transcription duration and task optional対応中かも @wskr00 が 110 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
openai/openai-java#802 · コメント 2 件 · リアクション 3 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 30/100
openai/openai-java#1175 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 半日 初心者へのやさしさ 42/100
openai/openai-java#1160 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
openai/openai-java#956 ·
メンテナーはふだん 1 日以内に返信
openai/openai-java の issue をすべて見る
似ている issue
-
e2e-failure
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
home-assistant/android#7809 ·
メンテナーはふだん 1 日以内に返信
-
index-request triaged
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
JetBrains/klibs-io#611 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 3 日以内に返信
-
afk-ok area:ci size:S
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
enorm-labs/event-junkie#4518 ·
メンテナーはふだん 1 日以内に返信