Skip to content

Clarify SDK initialization instructions for rails applications #144

Closed

Description

@ohsabry

The SDK documentation says that for a rails application, creating an instance of the client in an initializer, eg config/initializers/launchdarkly.rb:

https://docs.launchdarkly.com/docs/ruby-sdk-reference#section-initializing-ldclient-in-a-rails-application

However, it does then mention instructions for different servers like unicorn, puma, etc where reinitialises the client when a worker is forked

Similarly, with Unicorn, you'll need to specify an after_fork hook in your unicorn.rb config file:

after_fork do |server,worker|
  Rails.configuration.ld_client = LaunchDarkly::LDClient.new('SDK KEY')
end

Is it necessary to reinitialize the client on every worker fork? If so, why?

Activity

  1. eli-darkly commented on Nov 25, 2019

    @eli-darkly
    Contributor

    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.

  2. ohsabry commented on Nov 25, 2019

    @ohsabry
    Author

    @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?

  3. eli-darkly commented on Nov 25, 2019

    @eli-darkly
    Contributor

    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.

  4. grzuy commented on Oct 27, 2020

    @grzuy

    it means that you should do that instead of the default procedure described at the top.

    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.rb altogether and only keep it initialized in a config/puma.rb e.g.?

  5. rod-murphy commented on Nov 18, 2020

    @rod-murphy

    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

  6. ryanolds-drizly commented on Oct 27, 2021

    @ryanolds-drizly

    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.

  7. louis-launchdarkly commented on Oct 29, 2021

    @louis-launchdarkly
    Contributor

    @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.

  8. disposedtrolley commented on Nov 8, 2021

    @disposedtrolley

    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?

  9. louis-launchdarkly commented on Nov 8, 2021

    @louis-launchdarkly
    Contributor

    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.

  10. disposedtrolley commented on Nov 9, 2021

    @disposedtrolley

    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.

  11. louis-launchdarkly commented on Nov 10, 2021

    @louis-launchdarkly
    Contributor

    @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."

  12. disposedtrolley commented on Nov 10, 2021

    @disposedtrolley

    @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?

  13. louis-launchdarkly commented on Feb 9, 2022

    @louis-launchdarkly
    Contributor

    @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.

  14. 1 remaining item

  15. added a commit that references this issue on Sep 7, 2022
  16. jjb commented on Jul 20, 2023

    @jjb

    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?

  17. camilohollanda commented on Jun 3, 2024

    @camilohollanda

    Same question here

  18. julian-berbel commented on Jul 4, 2024

    @julian-berbel

    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).

  19. jondkinney commented on Apr 22, 2025

    @jondkinney

    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.

  20. keelerm84 commented on Jun 25, 2025

    @keelerm84
    Member

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions