Closed zhm closed 2 years ago
Yikes ... cc @nateberkopec
Let's start at the beginning here though. What is the correct way of spawning this thread only once per process? Starting and stopping a thread 30 times does not feel ideal.
At a minimum ... perhaps ... if the thread is already started we don't spawn a new one ... don't stop the old one?
Yeah, as we say in the comments for after_worker_fork
(alias of after_worker_boot
), emphasis mine:
Code to run in the master after a worker has been started This is called everytime a worker is to be started.
So code run in this hook should be idempotent. I'm not sure how this gem works but if you can run in the workers rather than the master process, you could hook in to on_worker_boot(&block)
instead.
I see... @zhm how does this look?
@zhm closing this in favor of #219 . It applies the fix more widely.
Thank you so much for bringing this up!
@SamSaffron this is fantastic, #219 is a much more comprehensive solution!
The README for puma recommends using this code to initialize puma instrumentation:
This results in a puma monitor thread being spawned per worker from the main process. When combined with puma-worker-killer this results in an accumulation of leaked threads and memory. The problematic one is
PrometheusExporter::Instrumentation::Puma.start
, which unconditionally spawns a new thread.This PR adds the same
stop
/kill
logic from theActiveRecord
andProcess
instrumentation to thePuma
instrumentation.When we add this fix: