You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transaction rollback doesn't work correctly for duped objects. Somehow the start state of object is mixed with the original. Consequently the rollback leaves us with faulty objects which can lead to all sorts of errors...
This happens in Rails 4.1.12, 4.0.13, but it doesn't happen in 4.2.3.
Below is an example demoing the issue. Key thing is that we dupe an object inside a transaction, save duped object, and the original too. And then we roll back.
#Activate the gem you are reporting the issue against. gem'activerecord','4.1.12'require'active_record'require'logger'# This connection will do for database-independent bug reports. ActiveRecord::Base.establish_connection(adapter: 'sqlite3',database: ':memory:')ActiveRecord::Base.logger=Logger.new(STDOUT)# Create table for Person model ActiveRecord::Schema.definedocreate_table:people,force: truedo |t|
t.string:nameendendclassPerson < ActiveRecord::Basebefore_create{true}end# script to demo transaction rollback issue original=Person.create{|p| p.name='test'}copy=nilPerson.transactiondocopy=original.dupcopy.save!original.save!putsoriginal.inspectputsoriginal.hashputscopy.inspectputscopy.hashraiseActiveRecord::Rollbackendputsoriginal.inspectputsoriginal.hashputscopy.inspectputscopy.hash
As visible from the output, the copy and original vars are referencing the same object after rollback!
Note: There are few conditions for this bug to appear. First, the model needs to have at least one before_create callback. Second, we need to call #save on the original object inside a transaction (original.save! - which does nothing to the db because object is not dirty). If we remove any of these two conditions, the bug does not appear.
The text was updated successfully, but these errors were encountered:
On 4.2, we did some big refactoring on the code of transaction, to fix mainly a bug around callback exceptions been swallowed. With the refactor I assumed that I fixed other problems too. I guess this was one of the issues solved. As the refactor was big, we could not backport it to Rails 4.1 or 4.0 .
So if this is working on 4.2 and master I would say this is not a bug.
Technically, the fact that we did a big refactor that happens to fix the bug does not imply the bug can't be fixed with a more targeted patch.
However, I currently can't volunteer to take on this myself, and to be quite honest, with 5.0 due soon, 4.1 is very low on everyone's priority list (4.1 will be EOL'ed when 5.0 comes out, and we already don't ship bug fixes for 4.0 anymore) - it's an unfortunate reality since we don't have unlimited resources so we have to pick our battles.
If you ended up figuring out how to fix this in a targeted way, and you can PR it before 5.0 ships, I think we will be more than happy to take the patch!
Transaction rollback doesn't work correctly for duped objects. Somehow the start state of object is mixed with the original. Consequently the rollback leaves us with faulty objects which can lead to all sorts of errors...
This happens in Rails 4.1.12, 4.0.13, but it doesn't happen in 4.2.3.
Below is an example demoing the issue. Key thing is that we dupe an object inside a transaction, save duped object, and the original too. And then we roll back.
The output:
As visible from the output, the
copy
andoriginal
vars are referencing the same object after rollback!Note: There are few conditions for this bug to appear. First, the model needs to have at least one before_create callback. Second, we need to call
#save
on the original object inside a transaction (original.save!
- which does nothing to the db because object is not dirty). If we remove any of these two conditions, the bug does not appear.The text was updated successfully, but these errors were encountered: