title: Rails singleton client lost after a Rails crash
body: <p>I have several third party APIs I need to connect to, so I have defined an abstract module for Singleton behavior, and the specific connector singleton isntances.</p>
  
  <p>My problem : everytime the Rails server crash (500 error, localhost), it seems I'm losing the @client instance variable of the connector</p>
  
  <pre><code>module ServiceConnector
    extend ActiveSupport::Concern
  
    included do
      include Singleton
      @activated = false
      @activation_attempt = false
      @client = nil
  
      def client
        @client
      end
  
      def service_name
        self.class.name.gsub('Connector', '')
      end
  
      def activate
        @activation_attempt = true
        if credentials_present?
          @client = service_client
          @activated = true
        end
        status_report
      end
  
  class MyConnector
    include ServiceConnector
    @app_id = nil
    @api_key = nil
  
    def credentials_present?
      @app_id.present? and @api_key.present?
    end
  
    def service_client
      ::MyService::Client.new(
        app_id: @app_id,
        api_key: @api_key
      )
    end
  
    def set_credentials(id, key)
      @app_id = id
      @api_key = key
    end
  </code></pre>
  
  <p>I initialize my singleton in a Rails initializer</p>
  
  <pre><code>#config/initializers/my_connectors.rb
  my_service = MyConnector.instance
  if my_service_should_be_activated?
    my_service.set_credentials(
      Rails.application.secrets.app_id,
      Rails.application.secrets.api_key
    )
    my_service.activate
  end
  
  def some_action_on_client
    client.push_event(...)
  end
  </code></pre>
  
  <p>The set of events</p>
  
  <pre><code>rails s
  ...
  # MyConnector client Online and working fine
  ...
  # error 500 on some controller
  # (next client request)
  # MyConnector @client variable mysteriously set to nil
  </code></pre>