Memory leak when thread is terminated after using logger.with_level
まだ誰も着手していません。
- #185 @wtn による — マージされずにクローズ
評価
調査の方向性
まず、報告された MemoryProfiler の再現を実行し、Logger#with_level、level_override ハッシュ、およびその Fiber.current のキーを調べます。終了したスレッドまたはファイバーを解放できるかどうかを判断し、Logger の変更が妥当であればリグレッションテストを追加します。スレッドの終了後に保持されたメモリが増加しなくなれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Hello. We were debugging a memory leak that appeared in our rails app after we started using with_level method to adjust visibility of some logs. How to reproduce:
require "logger"
require "memory_profiler"
logger = Logger.new $stdout
MemoryProfiler.report do
100.times { Thread.new { Thread.current[:data] = Array.new(200); logger.with_level(Logger::ERROR) { logger.debug("test") } }.join }
GC.start
end.pretty_print
# Total allocated: 336800 bytes (400 objects)
# Total retained: 336800 bytes (400 objects)
# ...
# retained memory by class
-----------------------------------
164000 Array
114400 Fiber
50400 Thread
8000 Proc
All memory is freed if logger.with_level is removed. My guess is that it happens because Logger uses Fiber.current as key in level_override hash to track log level for current fiber. This reference to Fiber.current is never cleared so GC cannot free connected terminated threads.
For more context this is why it happened in our rails application - we are deployed in kubernetes and have small plugin in puma to process liveness and readiness probes (simple http requests to specific endpoints). One of these requests checks connection to database via simple query. We hide these queries from logs via logger.with_level(Logger::Error) .... Problem was that our puma plugin options had min_threads: 0, max_threads: 1 so puma terminated (and later created again) its thread from time to time, which lead to level_override filling up with old Fiber.current for terminated threads.
When we eventually figured this out the fix was to just keep min_threads same as max_threads in puma.
I am not sure what would be the best way to prevent it (Fiber local variable instead of hash?) or if even something should be changed in Logger, because I realize that our problem was most likely quite unusual. If you think that this should be fixed I would be happy to prepare the change if you point me to the right direction. If not then this could at least help someone who is searching for similar problem.
- 主要言語
- Ruby
- スター
- 161
- フォーク
- 76
- 平均マージ
- 2日 5時間
- マージ済み PR(30日)
- 3
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ruby/logger のほかの issue
-
Programs using Logger sometimes resist SIGTERM / SIGINT対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 52/100
-
`datetime_format=` does not apply to custom formatters対応中かも @Rubyist007 が 229 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
似ている issue
-
SecureRandom.alphanumeric can silently return an empty or wrong-length secret instead of raisingオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
ruby/securerandom#92 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
open-feature/ruby-sdk-contrib#340 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
zold-io/wts.zold.io#651 ·
メンテナーはふだん 1 日以内に返信
-
good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 87/100
erkamyaman/rust-anything#788 ·
メンテナーはふだん 1 日以内に返信