You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clarify SDK initialization instructions for rails applications #144
You wouldn't be _re_initializing the client on every fork, but rather constructing it for the first time only after the fork starts. When a process is forked, the forked process gets an exact copy of every object that already existed in memory— but that won't work correctly if the object is maintaining active network connections, as the SDK client does.
@eli-darkly ah yes makes sense - I forgot that the SDK keeps a persistent connection.
Am I correct in assuming that if I initialise in after_fork, i don't need to construct the object in config/initializers/launchdarkly.rb?
Yes, I think the wording of the documentation was unclear there but it means that you should do that instead of the default procedure described at the top.
I might be missing something here probably, but wouldn't that mean that you won't have a LD client initialized in a rails console (or a rake task and other non-server boots), if you remove the config/initializers/launch_darkly.rb altogether and only keep it initialized in a config/puma.rb e.g.?
I might be missing something here probably, but wouldn't that mean that you won't have a LD client initialized in a rails console (or a rake task and other non-server boots), if you remove the config/initializers/launch_darkly.rb altogether and only keep it initialized in a config/puma.rb e.g.?
We implemented the following in config/initializers/launch_darkly.rb to get around that:
if !defined?(::Rails::Server)Rails.application.config.ld_client=LaunchDarkly::LDClient.new('SDK KEY',LaunchDarkly::Config.default,10)end
::Rails::Server should always be defined in a puma/unicorn worker
Reacted by Daniel Pritchard, Gabriel Batista de Lima, Max Hung and Trevor Hartman
Reacted by Leslie XS Zhang, Gabriel Batista de Lima, Jean-Francois Dumas and Trevor Hartman
Related question. If we are constructing a client for the worker/fork, do we also need to close that client when the worker exits/shutsdown?
We are using Puma and it looks like we are creating a new client for each worker, so it stands to reason that we would need to close the clients when the workers shutdown.
@ryanolds-drizly While I am not a specialist on how Puma web servers reclaim memories and resources, I believe closing the client will help memories and resources reclamation. So I recommend closing the client when the worker exits/shutdown, even when it may still works fine without explicitly closing it.
Our SDK Documentation: Shutdown Ruby does not currently call out the case when each fork/worker initialize its own client. We will update our documentation to clarify the behavior.
Our SDK Documentation: Shutdown Ruby does not currently call out the case when each fork/worker initialize its own client. We will update our documentation to clarify the behavior.
@louis-launchdarkly what's the correct method for shutting down individual clients in each worker process?
Based on Puma's Documentation about worker lifecycle, you can pass a block to the on_worker_shutdown and call close on the client reference inside the block. Because forking copies the object, calling close on the copy should not interfere with any other still running workers.
Thanks @louis-launchdarkly! Do you have any guidance for Unicorn-based Rails applications? They don't seem to provide a shutdown hook attached to each worker.
@disposedtrolley From the unicorn documentation, if you are using Unicorn 5.3.0+, there is an after_worker_exit block in which the worker is an argument to the block. You can call close there. I am not sure what is possible if you use a version before 5.3.0. Please let us know does that work for you.
And based on the discussion in #143, my recommendation is to "Please call close on the Client when the worker shutdown or it could leave an orphaned thread behind."
@louis-launchdarkly the after_worker_exit block appears to be called by the Unicorn master rather than the workers, so I don't believe it has access to the singleton client that was initialised by the worker.
A lot of this is coming down to my lack of knowledge on how Unicorn servers perform process forking -- is it possible to access objects held by workers from the master, within that after_worker_exit block?
regarding the discussion about closing clients when a puma worker shuts down - i'm not clear on the need here. a puma worker is an OS-level process. all resources will be gone when it shuts down. Or, is there a need to inform the LD service about this client going away, so overall statistics/sampling is correct?
Documentation on initialization is still unclear. Having a separate section for Rails makes it seem as though the sections for specific web servers like Puma / Unicorn are only for projects that do not use rails (like sinatra based apps).
This is still an issue, and confusing for new users. cc: @keelerm84 (Sorry to ping/tag, but you most recently replied to another thread, so wanted to make sure you could get 👀 here. And I found the Rails template on your github, so figured you may be the best person to help address this. Appreciate you taking a look!)
When installing LaunchDarkly and initially configuring things there is no "Rails" option, only a "Ruby" option, which is probably the opposite of what you want for a service like this for new folks? I could be wrong, but seems like LaunchDarkly would be more suited to web applications in Rails than to pure Ruby projects? In any case, once you realize the included instructions on the site itself after initial sign up will not be of much help in a Rails context, finding out what to put in the initializer becomes the next task. It wasn't immediately clear if there even was any Rails documentation because everything just references the Ruby SDK. So I googled. I did eventually find (through this thread) that the Ruby SDK docs do also cover Rails, albeit somewhat hidden in the toggle accordions near the bottom of the page.
It was implied/said here that only one or the other type of initialization is necessary (on boot or on fork, if I'm simplifying that properly...), with a possible workaround for being able to reference the client in the console, mentioned here.
Looking through some other issues to try to gain context and determine the proper course of action in a new Rails 8 app, I stumbled across https://github.com/keelerm84 's profile and saw that one of the recent things he committed to was the Rails template for a base LD config called hello-bootstrap-rails. In that repo, in the readme, they describe modifying the following files:
We modified and/or added the following files to create this example:
app/views/hello_world/index.html.erb
app/controllers/hello_world_controller.rb
config/initializers/launchdarkly.rb
config/puma.rb
And when looking at both the initializer and puma config, it appears that "doing both" is necessary, and I did eventually find that it is also mentioned in the SDK docs if you know to expand both the Rails and Puma options (which is what I think @julian-berbel was referring to last year).
If you use the Puma web server, we recommend initializing the client in on_worker_boot, as well as initializing in the Rails app:
The "as well as initializing in the Rails app" is a bit subtle. I'd suggest updating the SDK docs to more effectively show the initialization being needed in both spots to ensure new folks don't miss it. That and/or much more prominently pointing to the Rails starter would help for newbies to the service like me. And frankly, having a "Rails" option on initial sign up would actually be the best, IMO, for those users that squarely fall on the "using this in a Rails web app" context vs a general Ruby app/lib, like me.
@jondkinney my apologies for the delay in getting back to you on this, and thank you for providing such specific feedback!
When installing LaunchDarkly and initially configuring things there is no "Rails" option, only a "Ruby" option, which is probably the opposite of what you want for a service like this for new folks?
I have requested the team update the Quickstart so that searching for Rails will turn up the Ruby SDK.
I could be wrong, but seems like LaunchDarkly would be more suited to web applications in Rails than to pure Ruby projects?
The Ruby SDK is well suited to any number of server-side applications. While customers might first associate feature flagging with web applications, we are deployed in all sorts of amazing configurations and use cases.
I did eventually find (through this thread) that the Ruby SDK docs do also cover Rails, albeit somewhat hidden in the toggle accordions near the bottom of the page.
At the top of the Ruby SDK page, there is a section that links to example repositories. We include both a hello-ruby and a hello-boostrap-rails example. The latter shows how to bootstrap a client-side SDK from a server-side SDK.
The "as well as initializing in the Rails app" is a bit subtle.
I completely agree! In fact, we have updated the docs to hopefully clarify some of this. I would love if you would look at the worker-based server section again and let me know what your thoughts are.
Activity
eli-darkly commentedon Nov 25, 2019
You wouldn't be _re_initializing the client on every fork, but rather constructing it for the first time only after the fork starts. When a process is forked, the forked process gets an exact copy of every object that already existed in memory— but that won't work correctly if the object is maintaining active network connections, as the SDK client does.
ohsabry commentedon Nov 25, 2019
@eli-darkly ah yes makes sense - I forgot that the SDK keeps a persistent connection.
Am I correct in assuming that if I initialise in after_fork, i don't need to construct the object in
config/initializers/launchdarkly.rb?eli-darkly commentedon Nov 25, 2019
Yes, I think the wording of the documentation was unclear there but it means that you should do that instead of the default procedure described at the top.
grzuy commentedon Oct 27, 2020
Hi @eli-darkly ,
I might be missing something here probably, but wouldn't that mean that you won't have a LD client initialized in a rails console (or a rake task and other non-server boots), if you remove the
config/initializers/launch_darkly.rbaltogether and only keep it initialized in aconfig/puma.rbe.g.?rod-murphy commentedon Nov 18, 2020
We implemented the following in
config/initializers/launch_darkly.rbto get around that:::Rails::Servershould always be defined in a puma/unicorn workerupdate the default base url (#144)
ryanolds-drizly commentedon Oct 27, 2021
Related question. If we are constructing a client for the worker/fork, do we also need to close that client when the worker exits/shutsdown?
We are using Puma and it looks like we are creating a new client for each worker, so it stands to reason that we would need to close the clients when the workers shutdown.
louis-launchdarkly commentedon Oct 29, 2021
@ryanolds-drizly While I am not a specialist on how Puma web servers reclaim memories and resources, I believe closing the client will help memories and resources reclamation. So I recommend closing the client when the worker exits/shutdown, even when it may still works fine without explicitly closing it.
Our SDK Documentation: Shutdown Ruby does not currently call out the case when each fork/worker initialize its own client. We will update our documentation to clarify the behavior.
disposedtrolley commentedon Nov 8, 2021
@louis-launchdarkly what's the correct method for shutting down individual clients in each worker process?
louis-launchdarkly commentedon Nov 8, 2021
Based on Puma's Documentation about worker lifecycle, you can pass a block to the
on_worker_shutdownand callcloseon the client reference inside the block. Because forking copies the object, callingcloseon the copy should not interfere with any other still running workers.disposedtrolley commentedon Nov 9, 2021
Thanks @louis-launchdarkly! Do you have any guidance for Unicorn-based Rails applications? They don't seem to provide a shutdown hook attached to each worker.
louis-launchdarkly commentedon Nov 10, 2021
@disposedtrolley From the unicorn documentation, if you are using Unicorn 5.3.0+, there is an
after_worker_exitblock in which the worker is an argument to the block. You can callclosethere. I am not sure what is possible if you use a version before 5.3.0. Please let us know does that work for you.And based on the discussion in #143, my recommendation is to "Please call close on the Client when the worker shutdown or it could leave an orphaned thread behind."
disposedtrolley commentedon Nov 10, 2021
@louis-launchdarkly the
after_worker_exitblock appears to be called by the Unicorn master rather than the workers, so I don't believe it has access to the singleton client that was initialised by the worker.A lot of this is coming down to my lack of knowledge on how Unicorn servers perform process forking -- is it possible to access objects held by workers from the master, within that
after_worker_exitblock?louis-launchdarkly commentedon Feb 9, 2022
@disposedtrolley Sorry for missing your reply - based on the unicorn code signature, while it is called by the master, the code seems to still have the reference to the worker. So if your worker extends the https://github.com/defunkt/unicorn/blob/master/lib/unicorn/worker.rb class, you should be able to hold the reference that way.
prepare 6.3.2 release (#198)
1 remaining item
prepare 6.3.4 release (#203)
prepare 6.4.0 release (#205)
prepare 7.0.0 release (#207)
prepare 7.0.1 release (#208)
prepare 7.0.2 release (#209)
prepare 7.0.3 release (#211)
prepare 7.0.4 release (#215)
prepare 7.1.0 release (#217)
prepare 7.2.0 release (#218)
jjb commentedon Jul 20, 2023
regarding the discussion about closing clients when a puma worker shuts down - i'm not clear on the need here. a puma worker is an OS-level process. all resources will be gone when it shuts down. Or, is there a need to inform the LD service about this client going away, so overall statistics/sampling is correct?
camilohollanda commentedon Jun 3, 2024
Same question here
julian-berbel commentedon Jul 4, 2024
Documentation on initialization is still unclear. Having a separate section for Rails makes it seem as though the sections for specific web servers like Puma / Unicorn are only for projects that do not use rails (like sinatra based apps).
jondkinney commentedon Apr 22, 2025
This is still an issue, and confusing for new users. cc: @keelerm84 (Sorry to ping/tag, but you most recently replied to another thread, so wanted to make sure you could get 👀 here. And I found the Rails template on your github, so figured you may be the best person to help address this. Appreciate you taking a look!)
When installing LaunchDarkly and initially configuring things there is no "Rails" option, only a "Ruby" option, which is probably the opposite of what you want for a service like this for new folks? I could be wrong, but seems like LaunchDarkly would be more suited to web applications in Rails than to pure Ruby projects? In any case, once you realize the included instructions on the site itself after initial sign up will not be of much help in a Rails context, finding out what to put in the initializer becomes the next task. It wasn't immediately clear if there even was any Rails documentation because everything just references the Ruby SDK. So I googled. I did eventually find (through this thread) that the Ruby SDK docs do also cover Rails, albeit somewhat hidden in the toggle accordions near the bottom of the page.
It was implied/said here that only one or the other type of initialization is necessary (on boot or on fork, if I'm simplifying that properly...), with a possible workaround for being able to reference the client in the console, mentioned here.
Looking through some other issues to try to gain context and determine the proper course of action in a new Rails 8 app, I stumbled across https://github.com/keelerm84 's profile and saw that one of the recent things he committed to was the Rails template for a base LD config called hello-bootstrap-rails. In that repo, in the readme, they describe modifying the following files:
And when looking at both the initializer and puma config, it appears that "doing both" is necessary, and I did eventually find that it is also mentioned in the SDK docs if you know to expand both the Rails and Puma options (which is what I think @julian-berbel was referring to last year).
The "as well as initializing in the Rails app" is a bit subtle. I'd suggest updating the SDK docs to more effectively show the initialization being needed in both spots to ensure new folks don't miss it. That and/or much more prominently pointing to the Rails starter would help for newbies to the service like me. And frankly, having a "Rails" option on initial sign up would actually be the best, IMO, for those users that squarely fall on the "using this in a Rails web app" context vs a general Ruby app/lib, like me.
keelerm84 commentedon Jun 25, 2025
@jondkinney my apologies for the delay in getting back to you on this, and thank you for providing such specific feedback!
I have requested the team update the Quickstart so that searching for Rails will turn up the Ruby SDK.
The Ruby SDK is well suited to any number of server-side applications. While customers might first associate feature flagging with web applications, we are deployed in all sorts of amazing configurations and use cases.
At the top of the Ruby SDK page, there is a section that links to example repositories. We include both a hello-ruby and a hello-boostrap-rails example. The latter shows how to bootstrap a client-side SDK from a server-side SDK.
I completely agree! In fact, we have updated the docs to hopefully clarify some of this. I would love if you would look at the worker-based server section again and let me know what your thoughts are.