[BUG] ClassLoader Leak / Metaspace OOM: Unmanaged static ThreadLocal<Kryo> in PersonKryoSerializer
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- java
調査の方向性
まず PersonKryoSerializer.java を読み、static な KRYO_THREAD_LOCAL と、write() および read() でのその使用に注目してください。Kryo インスタンスが Worker-Threads によってどのように保持されるかを確認し、Issue に記載されたライフサイクルの選択肢を比較してください。シリアライズによってアプリケーションの classloader が保持されなくなり、既存の Person のシリアライズ動作が維持されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
Summary:
In PersonKryoSerializer, a static final ThreadLocal is used to cache expensive Kryo instances. However, the code completely lacks a cleanup mechanism (no remove() is ever called).
Root Cause:
When a Hazelcast internal worker thread (e.g., partition operation thread or I/O thread) executes the write or read method, the Kryo instance is attached to the thread's ThreadLocalMap. Because the thread is pooled and long-lived, the Kryo instance is never garbage collected.
Impact (Critical):
-
Memory Leak: Accumulation of Kryo instances in Hazelcast worker threads.
-
ClassLoader Leak (Metaspace OOM): The Kryo instance registers the domain class: kryo.register(Person.class). If this serializer is deployed within a web container (e.g., Tomcat) or an OSGi environment, the Kryo instance holds a strong reference to Person.class, which in turn holds a reference to the WebappClassLoader. The unmanaged ThreadLocal forms a strong reference chain (Worker Thread -> ThreadLocalMap -> Kryo -> Person.class -> WebappClassLoader), preventing the application from being undeployed cleanly. Repeated redeployments will inevitably result in java.lang.OutOfMemoryError: Metaspace.
Code Snippet
Location: PersonKryoSerializer.java
// Definition: Static ThreadLocal holding application classes
private static final ThreadLocal KRYO_THREAD_LOCAL = new ThreadLocal<>() {
@Override
protected Kryo initialValue() {
Kryo kryo = new Kryo();
kryo.register(Person.class); // <--- Danger: Holds reference to WebappClassLoader
return kryo;
}
};
// Usage: get() is called, but remove() is NEVER called in write() or read()
Expected Behavior
ThreadLocals used in serialization components must not pin application classloaders.
Proposed Fix:
-
Avoid Static ThreadLocal for stateful serializers: If the serializer must be loaded by a child classloader, manage the Kryo instance lifecycle properly.
-
Use Object Pooling: Instead of ThreadLocal, consider using an object pool (like Apache Commons Pool) where instances are explicitly borrowed and returned/cleared in a try-finally block during the write/read operations.
- 主要言語
- Java
- スター
- 558
- フォーク
- 599
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
hazelcast/hazelcast-code-samples のほかの issue
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 35/100
-
hazelcast code sample for XA transactions no longer works with current Atomikos version (5.0.9)オープン
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
hazelcast/hazelcast-code-samples#553 · コメント 4 件 ·
-
UPDATE sql statementオープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
hazelcast/hazelcast-code-samples の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
tchiotludo/akhq#3438 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
Paul-Austin-Oswego-CSC480-HCI521/gift-app#266 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
ome/bioformats#4509 ·
メンテナーはふだん 2 日以内に返信
-
module/email Type/Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
ballerina-platform/ballerina-library#9758 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
Azure/azure-functions-java-worker#909 · リアクション 1 件 ·