Skip to content

Document Rails 4.0 bidirectional dependent: :destroy recursion - #129

Open
JuanVqz wants to merge 2 commits into
mainfrom
feature/rails-40-bidirectional-dependent-destroy
Open

Document Rails 4.0 bidirectional dependent: :destroy recursion#129
JuanVqz wants to merge 2 commits into
mainfrom
feature/rails-40-bidirectional-dependent-destroy

Conversation

@JuanVqz

@JuanVqz JuanVqz commented Aug 13, 2026

Copy link
Copy Markdown
Member

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 — only when Rails registers the callback moved.

Rails 3.2 registered belongs_to ..., dependent: :destroy as an after_destroy, in a belongs_to-specific configure_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_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. 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. 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 one dependent: — the latter stops the loop but silently orphans rows.

Judgment calls worth a second opinion:

  • Filed as 🟡 MEDIUM per the CLAUDE.md rubric (app boots, suite runs, affects a noticeable class of apps rather than all). It is a hard blocker for any app that has the pattern, so HIGH is defensible.
  • Inserting at the end of MEDIUM renumbered the LOW guide entries 14-24 → 15-25. Nothing in the repo references those numbers.
  • The detection pattern flags one side of the pair only, since a regex cannot see the other model. Its explanation says so and points at walking reflections instead.

bin/validate-patterns and bin/test-patterns clean across all pattern files (157 assertions, 48/53 patterns covered).

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 JuanVqz self-assigned this Aug 13, 2026
@JuanVqz
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant