Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent setting parent as itself #8980

Merged
merged 6 commits into from Aug 7, 2023

Conversation

lewiszlw
Copy link
Member

@lewiszlw lewiszlw commented Jun 28, 2023

Objective

Solution

  • Panic when parent and child are same entity.
  • Add checks into EntityCommands and EntityMut methods

@farend-east
Copy link
Contributor

The fix must also apply to InsertChildren PushChildren and ReplaceChildren

@lewiszlw
Copy link
Member Author

The fix must also apply to InsertChildren PushChildren and ReplaceChildren

Added!

@nicopap
Copy link
Contributor

nicopap commented Jun 28, 2023

This doesn't solve the problem for the EntityMut methods. Those methods are called in the functions you modified, so you can just move the check to the EntityMut methods.

In this impl block:

impl<'w> BuildWorldChildren for EntityMut<'w> {

With regard to EntityCommand implementations, Adding a panic in the commands methods would be ideal, so that the panic occurs, the backtrace shows exactly what code caused the issue.

Add the panic in the relevant functions in this impl block:

impl<'w, 's, 'a> BuildChildren for EntityCommands<'w, 's, 'a> {

The current change only solves the issue for parents inserted through commands, and has poor backtraces.

@nicopap nicopap self-requested a review June 28, 2023 07:22
@lewiszlw
Copy link
Member Author

This doesn't solve the problem for the EntityMut methods. Those methods are called in the functions you modified, so you can just move the check to the EntityMut methods.

Yeah, putting checks into EntityMut methods should be better.

With regard to EntityCommand implementations, Adding a panic in the commands methods would be ideal, so that the panic occurs, the backtrace shows exactly what code caused the issue.

I think the panic message might be enough. Waiting for more opinions.

@farend-east
Copy link
Contributor

We still need the same logic for EntityCommands as this is where the user will mainly call from commands.
It can be the same logic as the one added in EntityMut.

The function set_parent for both impl will also needs this check .

@nicopap
Copy link
Contributor

nicopap commented Jun 28, 2023

We still need the same logic for EntityCommands as this is where the user will mainly call from commands.

Technically, the commands then later will call the EntityMut methods with the same parameters, so it will panic. But I think they should be added to EntityCommands regardless.

I would strongly advise to add the check in the EntityCommands extension methods. Have you ever hit the "couldn't despawn entity, it is already despawned" error? It's so annoying, and it's basically impossible to know what code caused it.

This is because the panic is located in a different system than the one that triggered the despawn.

Pushing the panic in the EntityCommands extension methods will make the panic run within the system that tries to add the parent as child, as a result, the system will be included in the backtrace. With this, it's trivial for the bevy user to find where the error happens and fix it. Without it, it can become a struggle to figure out how to avoid the panic.

@lewiszlw
Copy link
Member Author

I would strongly advise to add the check in the EntityCommands extension methods. Have you ever hit the "couldn't despawn entity, it is already despawned" error? It's so annoying, and it's basically impossible to know what code caused it.

make sense.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior C-Usability A simple quality-of-life change that makes Bevy easier to use A-Hierarchy Parent-child entity hierarchies labels Jun 28, 2023
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I don't love adding more ways for commands to panic, but I think that surfacing the error early when the command is initialized is ultimately a nicer UX.

Copy link
Member

@mockersf mockersf left a comment

Choose a reason for hiding this comment

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

Some of those panics are added not in the commands execution, but in the command creation, so while we could still return a result.

If we want to panic in those methods, a panic section should be added to their docs

@lewiszlw
Copy link
Member Author

lewiszlw commented Jul 3, 2023

Added panics doc

@lewiszlw lewiszlw requested a review from mockersf July 24, 2023 02:25
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Aug 4, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Aug 7, 2023
Merged via the queue into bevyengine:main with commit 7ccb203 Aug 7, 2023
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Hierarchy Parent-child entity hierarchies C-Bug An unexpected or incorrect behavior C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stack overflow error when bevy_ui node has circular Parent Children dependency
5 participants