Memory leak when thread is terminated after using logger.with_level
Ninguém assumiu esta issue ainda.
- #185 de @wtn — fechado sem integrar
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 30/100
Direção de pesquisa
Comece executando a reprodução reportada do MemoryProfiler e inspecione Logger#with_level, o hash level_override e suas chaves de Fiber.current. Determine se threads ou fibers encerrados podem ser liberados e adicione um teste de regressão caso uma alteração em Logger seja justificável; considera-se concluído quando a memória retida não crescer mais depois que as threads terminarem.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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.
- Linguagem predominante
- Ruby
- Estrelas
- 161
- Forks
- 76
- Merge médio
- 2d 5h
- PRs com merge (30d)
- 3
Preparar o ambiente
Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de ruby/logger
-
Programs using Logger sometimes resist SIGTERM / SIGINTTalvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 82/100
-
Later writes fail with "closed stream" when renaming the log fails during rotation on WindowsAberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 68/100
-
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 48/100
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 52/100
-
`datetime_format=` does not apply to custom formattersTalvez já em andamento @Rubyist007 assumiu há 229 dias. Aberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 72/100
Todas as issues de ruby/logger
Issues semelhantes
-
SecureRandom.alphanumeric can silently return an empty or wrong-length secret instead of raisingAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 67/100
ruby/securerandom#92 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
open-feature/ruby-sdk-contrib#340 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
zold-io/wts.zold.io#651 ·
Mantenedores costumam responder em até 1 dia
-
good first issue help wanted
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 87/100
erkamyaman/rust-anything#788 ·
Mantenedores costumam responder em até 1 dia