Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upExplicit transactions not rolling back when validation fails on nested attributes #14698
Comments
robin850
added
the
activerecord
label
Apr 12, 2014
arthurnn
referenced this issue
Apr 13, 2014
Closed
Save model after a transaction rollback fails in a way #14703
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
chancancode
Apr 13, 2014
Member
Ah. Thanks for looking into this @lcreid. I dug a bit deeper and found the documentation I was looking for the other day. It turns out that this is indeed expected and documented behaviour (see the example in the docs I linked). And it also turns out that I have been bitten by this more than once (see #12944, also #4566), so clearly this is super confusing
I'll look into the reason behind this (performance maybe?) and see if anything can be changed here in Rails 5.0, either make requires_new the default or perhaps make inner transaction blocks propagate the exceptions.
Closing for now, but thanks again for looking into this with me!
|
Ah. Thanks for looking into this @lcreid. I dug a bit deeper and found the documentation I was looking for the other day. It turns out that this is indeed expected and documented behaviour (see the example in the docs I linked). And it also turns out that I have been bitten by this more than once (see #12944, also #4566), so clearly this is super confusing I'll look into the reason behind this (performance maybe?) and see if anything can be changed here in Rails 5.0, either make Closing for now, but thanks again for looking into this with me! |
chancancode
closed this
Apr 13, 2014
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
chancancode
Apr 15, 2014
Member
I'm actually a bit suspicious of the correctness of the documentation I linked, I think this might have been valid – I'll try to get a more definitive answer
|
I'm actually a bit suspicious of the correctness of the documentation I linked, I think this might have been valid – I'll try to get a more definitive answer |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
matthewd
Apr 15, 2014
Member
@chancancode FWIW, I agree... that sounds more like it's documenting existing behaviour, rather than intended.
|
@chancancode FWIW, I agree... that sounds more like it's documenting existing behaviour, rather than intended. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
chancancode
Apr 15, 2014
Member
So it all started with this docrails commit – 53bbbcc, which the documented (but perhaps buggy) behaviour at that time to what we have now.
@evtuhovich, I know this is a long time ago, but do you remember the back story for this change? Have you confirmed that the behaviour you documented is intended? Thanks in advance
|
So it all started with this docrails commit – 53bbbcc, which the documented (but perhaps buggy) behaviour at that time to what we have now. @evtuhovich, I know this is a long time ago, but do you remember the back story for this change? Have you confirmed that the behaviour you documented is intended? Thanks in advance |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evtuhovich
Apr 16, 2014
Contributor
I remember this bug, i spent one day to find a solution.
This was default behaviour for nested transaction, because not all databases support it. I only found this and document. So, if you want consistent nested transaction, always use Model.transaction(:requires_new => true)
|
I remember this bug, i spent one day to find a solution. This was default behaviour for nested transaction, because not all databases support it. I only found this and document. So, if you want consistent nested transaction, always use |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
chancancode
Apr 16, 2014
Member
I believe the intention was that if you don't use requires_new it should rollback the outermost block, whereas if you do it will rollback that specific block - that was the originally documented behavior. Have you verified with others that the behavior you documented is intened (as opposed to just a buggy implementation)?
|
I believe the intention was that if you don't use |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evtuhovich
Apr 16, 2014
Contributor
I also was sure, that rollback should rollback outremost block. But this is wrong. When you rollback inner transaction nothing happens, outer transaction don't get rollback and continue its execution.
And this is why a spent a day to understand that problem in my project.
|
I also was sure, that rollback should rollback outremost block. But this is wrong. When you rollback inner transaction nothing happens, outer transaction don't get rollback and continue its execution. And this is why a spent a day to understand that problem in my project. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
chancancode
Apr 16, 2014
Member
Right, if I understand you correctly, you are saying the documentation (at the time) didn't agree with the actual behaviour of the code (at the time), correct? But in that case, it could be either the code or the documentation that is wrong. In this case I suspect it's the former – which is why I asked if you have confirmed this behaviour is desirable (for example, filed a bug on this and was told by a core team member that the docs are wrong).
I'm trying to find those back stories to support this doc change, because it appears otherwise that the code should have been fixed instead.
|
Right, if I understand you correctly, you are saying the documentation (at the time) didn't agree with the actual behaviour of the code (at the time), correct? But in that case, it could be either the code or the documentation that is wrong. In this case I suspect it's the former – which is why I asked if you have confirmed this behaviour is desirable (for example, filed a bug on this and was told by a core team member that the docs are wrong). I'm trying to find those back stories to support this doc change, because it appears otherwise that the code should have been fixed instead. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evtuhovich
Apr 16, 2014
Contributor
I don't think, this is desireble behaviour, i've just documented the state of code at that time, and i hope, this helps other people.
And i sure the code should be fixed, of course.
|
I don't think, this is desireble behaviour, i've just documented the state of code at that time, and i hope, this helps other people. And i sure the code should be fixed, of course. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
chancancode
Apr 16, 2014
Member
Thanks for the update!
For the future, please file a bug report for these kind of things – the documentation should reflect how the code should behave, so we shouldn't change the documentation when the code misbehave. This will cause other people to make incorrect inference about the code and result in things like issues incorrectly being closed, or worse – more code being changed to align itself with the buggy behaviour.
|
Thanks for the update! For the future, please file a bug report for these kind of things – the documentation should reflect how the code should behave, so we shouldn't change the documentation when the code misbehave. This will cause other people to make incorrect inference about the code and result in things like issues incorrectly being closed, or worse – more code being changed to align itself with the buggy behaviour. |
chancancode
reopened this
Apr 16, 2014
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
cc @fxn |
chancancode
referenced this issue
Apr 16, 2014
Closed
Stop swallowing errors on transaction after callbacks. #14488
added a commit
to chancancode/rails
that referenced
this issue
May 7, 2014
added a commit
to chancancode/rails
that referenced
this issue
May 7, 2014
chancancode
referenced this issue
May 7, 2014
Closed
Fixed "fake" nested transactions swallowing manual rollbacks #15017
added a commit
to chancancode/rails
that referenced
this issue
May 8, 2014
chancancode
added
JRuby
and removed
JRuby
labels
Jun 26, 2014
chancancode
added this to the 4.2.0 milestone
Jul 2, 2014
chancancode
self-assigned this
Jul 2, 2014
chancancode
removed this from the 4.2.0 milestone
Aug 12, 2014
rails-bot
added
the
stale
label
Nov 19, 2014
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
rails-bot
Nov 19, 2014
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 team are limited, and so we are asking for your help.
If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.
rails-bot
commented
Nov 19, 2014
|
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
lcreid commentedApr 11, 2014
In certain situations, when a validation fails on a model referenced in nested attributes, when the update attributes is executed inside an explicit transaction, the transaction is not rolled back, and the database is left in a state that's different than it was before the transaction.
Example:
In the controller:
Note that
user.update!does not cause the issue.The models:
A complete file that shows a case that causes, and some cases that are similar but don't cause the issue:
If the above is in a file called
repro.rb, then:should produce output ending in:
The issues has been observed in 4.1 master and 4.0 master. I haven't yet checked how far back it might go.
The issue was shown to my by @chancancode . I'm documenting the issue here without much further research yet because I didn't want it to get dropped.