Memory leak when thread is terminated after using logger.with_level
还没有人认领这个 Issue。
- #185 来自 @wtn —— 已关闭,未合并
评估
调研方向
首先运行报告中的 MemoryProfiler 复现,并检查 Logger#with_level、level_override 哈希及其 Fiber.current 键。确定已终止的线程或 fiber 是否可以释放;如果有必要修改 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 小时
- 30 天内合并 PR
- 3
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ruby/logger 的其他 Issue
-
Programs using Logger sometimes resist SIGTERM / SIGINT可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 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
-
难度 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 天内回复
-
难度 1/5 1 小时以内 新手友好度 74/100
TheOdinProject/curriculum#31579 ·
维护者通常 1 天内回复