Document Rails 4.0 bidirectional dependent: :destroy recursion - #129
Open
JuanVqz wants to merge 2 commits into
Open
Document Rails 4.0 bidirectional dependent: :destroy recursion#129JuanVqz wants to merge 2 commits into
JuanVqz wants to merge 2 commits into
Conversation
Two models that each declare `dependent: :destroy` pointing at the other
terminate on Rails 3.2 and recurse forever on 4.0. Nothing in an app has
to change for this to appear: it is purely a shift in when Rails
registers the callback.
Rails 3.2 registers `belongs_to ..., dependent: :destroy` as an
after_destroy, in a belongs_to-specific `configure_dependency`
(activerecord-3.2.x/.../builder/belongs_to.rb, `model.after_destroy
method_name`). Rails 4.0 drops that special case and registers all three
macros from the shared builder as a before_destroy
(activerecord-4.0.x/.../builder/association.rb, `model.before_destroy
"#{macro}_dependent_for_#{name}"`). has_many and has_one were
before_destroy on both versions; only belongs_to moved.
So on 3.2 the belongs_to side fired after its own row was deleted and
the reciprocal cascade found nothing, stopping after one bounce. On 4.0
both fire while both rows still exist and a.destroy -> b.destroy ->
a.destroy never ends. Each lap reloads from the database, so nothing
detects the repetition: SystemStackError in tests, and in a request a
hang that times out into a 5xx.
Worth documenting because it is silent on upgrade. No deprecation
warning, the app boots normally, and it only fires on the delete path,
which suites tend to cover thinly. Fixed upstream in Rails 5.0 by
rails/rails#18548 (guards ActiveRecord::Callbacks#destroy against
re-entrant callbacks), not backported to 4.x.
Filed as MEDIUM rather than HIGH per the CLAUDE.md rubric: the app boots
and the suite runs, and it affects a noticeable class of apps rather
than all of them. It is a hard blocker for any app that has the pattern.
The recommended fix moves the cascade the app does not drive to an
explicit after_destroy, restoring the 3.2 ordering, rather than deleting
one `dependent:` — the latter stops the loop but silently orphans rows.
Both caveats that come with it are recorded: for soft-deleting models
"gone" means out of the default scope rather than deleted, and the
cleanup can no longer veto the destroy, so a failed cascade now leaves
an orphan and reports success where it used to halt and return false.
The detection pattern flags one side only, since a regex cannot see the
pair. Its explanation says so and points at walking reflections to
enumerate cycles across a whole app.
Guide entry inserted as MEDIUM 14; existing LOW entries renumbered
15-25. No cross-references to those numbers exist elsewhere in the repo.
bin/validate-patterns and bin/test-patterns both clean (157 assertions,
48/53 patterns covered).
JuanVqz
marked this pull request as ready for review
August 15, 2026 00:54
- Fix the Common Issues quick-reference table, which still pointed at the pre-renumbering sections (15/16/18/24/23 after inserting section 14). - Note that the after_destroy fix only terminates when the reciprocal association is re-read from the database. With inverse_of: or an already-loaded target the loop survives, and 4.0 has no re-entrancy guard (added in 5.0 as @_destroy_callback_already_called). Recommend an instance flag rather than destroyed?/frozen?, neither of which is set until the destroy completes. - Narrow the regex to dependent: :destroy and teach it the 3.2-era :dependent => :destroy hash-rocket form, which it previously missed. belongs_to accepts only :destroy and :delete, and :delete skips callbacks, so :destroy is the complete set of cycle-capable values. - State in the detection section that a single hit is not a bug: the cycle needs both edges destroying each other. - Document the remaining single-line limitation and add the matching fixtures.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two models that each declare
dependent: :destroypointing at the other terminate on Rails 3.2 and recurse forever on 4.0. Nothing in an app has to change for this to appear — only when Rails registers the callback moved.Rails 3.2 registered
belongs_to ..., dependent: :destroyas an after_destroy, in a belongs_to-specificconfigure_dependency(activerecord-3.2.x/.../builder/belongs_to.rb). Rails 4.0 dropped that special case and registers all three macros as a before_destroy from the shared builder (activerecord-4.0.x/.../builder/association.rb).has_manyandhas_onewerebefore_destroyon both versions; onlybelongs_tomoved.So on 3.2 the
belongs_toside fired after its own row was deleted and the reciprocal cascade found nothing. On 4.0 both fire while both rows still exist, anda.destroy → b.destroy → a.destroynever ends. Each lap reloads from the database, so nothing detects the repetition:SystemStackErrorin tests, and in a request a hang that times out into a 5xx.Worth documenting because it is silent on upgrade — no deprecation warning, the app boots normally, and it only fires on the delete path. Fixed upstream in Rails 5.0 by rails/rails#18548, not backported to 4.x.
The recommended fix moves the cascade the app does not drive to an explicit
after_destroy, restoring the 3.2 ordering, rather than deleting onedependent:— the latter stops the loop but silently orphans rows.Judgment calls worth a second opinion:
bin/validate-patternsandbin/test-patternsclean across all pattern files (157 assertions, 48/53 patterns covered).