This project is archived and is in readonly mode.

#6064 ✓stale
Robert Pankowecki

Exceptions from after_commit and after_rollback from observers are quietly swallowed

Reported by Robert Pankowecki | November 25th, 2010 @ 03:56 PM

class CarObserver < ActiveRecord::Observer

 def after_commit(car)
  puts "X"
  raise "X"
 end

 def after_rollback(car)
  puts "Y"
  raise "Y"
 end

 def after_update(car)
  puts "Z"
  raise "Z"
 end

end

ruby-1.9.2-head > Car.transaction do
ruby-1.9.2-head >      Car.new.save!
ruby-1.9.2-head ?>  end
X
 => true 
ruby-1.9.2-head > 
ruby-1.9.2-head >   Car.transaction do
ruby-1.9.2-head >       Car.new.save!
ruby-1.9.2-head ?>    raise ActiveRecord::Rollback
ruby-1.9.2-head ?>  end
Y
 => nil 
ruby-1.9.2-head > 
ruby-1.9.2-head >   Car.transaction do
ruby-1.9.2-head >      Car.first.save!
ruby-1.9.2-head ?>  end
Z
Y
RuntimeError: Z

As you can see the hooks are run but exceptions are swallowed. I think it is a bug.

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh November 26th, 2010 @ 01:50 AM

    • Assigned user set to “Neeraj Singh”
    • Importance changed from “” to “Low”

    This is what I observed. There is a rescue Exception for both after_commit and after_rollback callback. After rescuing the exception, the exception is being logged and that's it. The raised exception is not being re-raised.

    Also note that After the after_commit and after_rollback callbacks are executing all the code is ensuring that transaction status is cleared which would happen even if exception is raised.

        def committed! #:nodoc:
          _run_commit_callbacks
        ensure
          clear_transaction_record_state
        end
    
        # Call the after rollback callbacks. The restore_state argument indicates if the record
        # state should be rolled back to the beginning or just to the last savepoint.
        def rolledback!(force_restore_state = false) #:nodoc:
          _run_rollback_callbacks
        ensure
          restore_transaction_record_state(force_restore_state)
        end
    

    There is also a test which explicitly checks that an exception raised in after_commit is not raised all the way to the top. So this is done intentionally.

    I will try to find out what the intention was. At the least intention should be documented and the fact that exceptions raised in after_commit and after_rolledback will only be logged should be documented too.

  • Robert Pankowecki

    Robert Pankowecki November 26th, 2010 @ 04:10 PM

    It would be good to know the intentions. So far I see no reason for that. Only makes me harder to spot a bug in my code.

  • Robert Pankowecki
  • Neeraj Singh

    Neeraj Singh December 1st, 2010 @ 05:37 AM

    • Assigned user changed from “Neeraj Singh” to “Aaron Patterson”
  • Aaron Patterson

    Aaron Patterson December 9th, 2010 @ 01:00 AM

    • State changed from “new” to “open”

    @Neeraj, do you remember the test that made this assertion? I can do some code archeology.

  • Neeraj Singh

    Neeraj Singh December 9th, 2010 @ 02:18 AM

    @Aaron : I should have provided more information in the first place.

    Here is where exception is being gobbled up . https://github.com/rails/rails/blob/master/activerecord/lib/active_...

    Here is where an after_commit has raise '*' and the test is checking that nothing should blow up. https://github.com/rails/rails/blob/master/activerecord/test/cases/...

    If you wish I can work on it. However first just wanted to know if this is the path we should move along.

  • Robert Pankowecki

    Robert Pankowecki December 9th, 2010 @ 09:22 AM

    Could you also spare some of you precious time to this ticket: https://rails.lighthouseapp.com/projects/8994/tickets/6065-activere... . There is even fix already :-)

  • Aaron Patterson

    Aaron Patterson December 9th, 2010 @ 06:47 PM

    I don't like this behavior. Exceptions should be raised. Unless someone stops me, I will make that happen. :-)

  • Neeraj Singh

    Neeraj Singh December 9th, 2010 @ 07:01 PM

    No one can stop you. As JV said you are the BFF of ActiveRecord :-)

  • rails

    rails March 10th, 2011 @ 12:00 AM

    • Tag changed from activecord, after_commit, after_rollback, exception, observers, rails3, rails3.0.3 to activecord, after_commit, after_rollback, exception, observers, rails3, rails303

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails March 10th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages