Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Exceptions from after_commit and after_rollback from observers are quietly swallowed

#6064
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.

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

State: stale
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • 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.

    November 26th, 2010 @ 01:50 AM

  2. Robert Pankowecki
    Robert Pankowecki

    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.

    November 26th, 2010 @ 04:10 PM

  3. Robert Pankowecki
  4. Neeraj Singh
    Neeraj Singh
    • Assigned user changed from Neeraj Singh to Aaron Patterson

    December 1st, 2010 @ 05:37 AM

  5. Aaron Patterson
    Aaron Patterson
    • State changed from new to open

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

    December 9th, 2010 @ 01:00 AM

  6. Neeraj Singh
    Neeraj Singh

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

    December 9th, 2010 @ 02:18 AM

  7. Robert Pankowecki
    Robert Pankowecki

    Could you also spare some of you precious time to this ticket: #6065 ActiveRecord::Observer is not aware of method added by including modules . There is even fix already :-)

    December 9th, 2010 @ 09:22 AM

  8. Aaron Patterson
    Aaron Patterson

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

    December 9th, 2010 @ 06:47 PM

  9. Neeraj Singh
    Neeraj Singh

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

    December 9th, 2010 @ 07:01 PM

  10. rails
    rails
    • 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.

    March 10th, 2011 @ 12:00 AM

  11. rails
    rails
    • State changed from open to stale

    March 10th, 2011 @ 12:00 AM