Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

bug in ActiveRecord::Railties::ControllerRuntime #9215

Closed
Fivell opened this Issue · 2 comments

3 participants

@Fivell

cleanup_view_runtime method is buggy
Discussion is here http://stackoverflow.com/questions/14665016/rails-activesupport-notifications-wrong-db-runtime-value

 def cleanup_view_runtime
        if ActiveRecord::Base.connected?
          db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
          runtime = super
          db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
          self.db_runtime = db_rt_before_render + db_rt_after_render
          runtime - db_rt_after_render
        else
          super
        end
      end

Shortly runtime = super line can cause ActionView::MissingTemplate exception for ActionController::Responder . This exception resuced next way

 # All other formats follow the procedure below. First we try to render a
    # template, if the template is not available, we verify if the resource
    # responds to :to_format and display it.
    #
    def to_format
      if get? || !has_errors? || response_overridden?
        default_render
      else
        display_errors
      end
    rescue ActionView::MissingTemplate => e
      api_behavior(e)
    end

The problem is that after raising exception self.db_runtime is not populated so in that case payload[:db_runtime] always equals to 0.

@Fivell

as for now I'm using this monkey patch resucing and raising exception again

module ActiveRecord
  module Railties
    module ControllerRuntime #:nodoc:

      def cleanup_view_runtime
              if ActiveRecord::Base.connected?
                db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
                begin
                  runtime = super
                  db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
                  self.db_runtime =  (db_runtime || 0) + db_rt_before_render + db_rt_after_render
                  runtime - db_rt_after_render
                rescue ActionView::MissingTemplate => e
                   self.db_runtime = (db_runtime || 0) + db_rt_before_render
                   raise e
                end
              else
                super
              end
            end


    end
  end
end
@jamesotron

Are you able to supply a failing test? I'll happily write a patch.

@Fivell Fivell referenced this issue from a commit in Fivell/rails
@Fivell Fivell failing test for #9215 430888a
@Fivell Fivell referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@Fivell Fivell referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.