Skip to content

Fix (OnDelete, Delete) cascade skipping entities - #341

Closed
RayanR000 wants to merge 2 commits into
Ukendio:mainfrom
RayanR000:fix/322-delete-cascade-skip
Closed

RayanR000 wants to merge 2 commits into
Ukendio:mainfrom
RayanR000:fix/322-delete-cascade-skip

Conversation

@RayanR000

Copy link
Copy Markdown

Brief Description of your Changes.

Fixes #322.

In world_delete, the (OnDelete, Delete) branch iterates idr.records with next(), and each recursive world_delete can create new archetypes (archetype_traverse_remove -> archetype_create -> archetype_append_to_records), which inserts new keys into that same table mid-traversal. Assigning to a non-existent key during next() traversal is undefined, and in practice some archetypes get skipped. Entities in them survive the delete, keep the deleted component, and still match query(e). With the repro from the issue, 13 of 50 survive.

The loop now snapshots the archetype ids, deletes from each (skipping any already destroyed by a recursive call), and repeats until a pass deletes nothing. A pass only repeats if it deleted something, and the cascade never creates entities, so it terminates.

The second commit applies the same change to the (R, *) relation branch (idr_r.records), which has the same pattern (17/31/50 survivors for 64/100/200 targets before).

None of the repros use hooks, so this isn't the on_remove structural-change case from #324.

Impact of your Changes

(OnDelete, Delete) now deletes every entity with the id, as documented in how_to/100_cleanup_traits.luau. Plain deletes and Remove policies are untouched.

Perf (median of interleaved runs, 21 reps): plain delete of 65536 entities, large tag/relation cascades and ChildOf cascades are within noise. Many tiny (OnDelete, Delete) cascades cost about 1-3% more, from one snapshot table per call.

Tests Performed

New cases in TEST("world:delete()"), asserting world state (contains, query, component_index):

  • cascade moving entities into new archetypes, over 5 table layouts, since whether next() skips depends on hash layout
  • nested (OnDelete, Delete) cascades with a surviving bystander
  • the relation (R, *) case, 5 layouts
  • neighbours that shouldn't change: (OnDelete, Remove), (OnDeleteTarget, Delete) across 64 archetypes, deleting an id with no dependents

luau test/tests.luau: 153/153 on the branch; the 3 cascade tests fail on current main.

Additional Comments

The repros from #326 and #327 give the expected result on current main; they look fixed by #340.

world_delete walked idr.records with next() while the recursive
world_delete could create archetypes that still contain the deleted id
(deleting a dependent strips (R, child) from another entity that also
has the id). archetype_create then inserted new keys into the records
being traversed, which is undefined for next(), and the archetypes that
landed behind the iterator were never visited: their entities survived
the delete, kept the dead id, and still matched queries.

Iterate a snapshot of the archetype ids and take a new snapshot until a
pass deletes nothing. Each repeated pass deletes at least one entity, so
the loop terminates.
The (R, *) branch of world_delete had the same problem as the one fixed
for #322: it walked idr_r.records with next() while the recursive
world_delete could create archetypes with (R, *) pairs (deleting a
dependent strips (S, child) from another entity that also has (R, t)),
inserting keys into the records mid-traversal. Entities in archetypes
that landed behind the iterator survived world:delete(R).

Use the same snapshot-and-repeat loop as the component branch.
Copilot AI lite review requested due to automatic review settings September 23, 2026 13:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Ukendio

Ukendio commented Sep 23, 2026

Copy link
Copy Markdown
Owner

AI-generated pull requests will be closed. There is a lot of ai-prose in both the code and in this message. Repeat submissions may result in a ban.

@Ukendio Ukendio closed this Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(OnDelete, Delete) cascade can silently skip entities: world_delete iterates idr.records while the cascade inserts new keys into it

3 participants