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

bevy_reflect: Contextual serialization error messages #13888

Merged

Conversation

MrGVSV
Copy link
Member

@MrGVSV MrGVSV commented Jun 17, 2024

Objective

Reflection serialization can be difficult to debug. A lot of times a type fails to be serialized and the user is left wondering where that type came from.

This is most often encountered with Bevy's scenes. Attempting to serialize all resources in the world will fail because some resources can't be serialized.

For example, users will often get complaints about bevy_utils::Instant not registering ReflectSerialize. Well, Instant can't be serialized, so the only other option is to exclude the resource that contains it. But what resource contains it? This is where reflection serialization can get a little tricky (it's Time<Real> btw).

Solution

Add the debug_stack feature to bevy_reflect. When enabled, the reflection serializers and deserializers will keep track of the current type stack. And this stack will be used in error messages to help with debugging.

Now, if we unknowingly try to serialize Time<Real>, we'll get the following error:

type `bevy_utils::Instant` did not register the `ReflectSerialize` type data. For certain types, this may need to be registered manually using `register_type_data` (stack: `bevy_time::time::Time<bevy_time::real::Real>` -> `bevy_time::real::Real` -> `bevy_utils::Instant`)

Implementation

This makes use of thread_local! to manage an internal TypeInfoStack which holds a stack of &'static TypeInfo. We push to the stack before a type is (de)serialized and pop from the stack afterwards.

Using a thread-local should be fine since we know two (de)serializers can't be running at the same time (and if they're running on separate threads, then we're still good).

The only potential issue would be if a user went through one of the sub-serializers, like StructSerializer. However, I don't think many users are going through these types (I don't even know if we necessarily want to keep those public either, but we'll save that for a different PR). Additionally, this is just a debug feature that only affects error messages, so it wouldn't have any drastically negative effect. It would just result in the stack not being cleared properly if there were any errors.

Lastly, this is not the most performant implementation since we now fetch the TypeInfo an extra time. But I figured that for a debug tool, it wouldn't matter too much.

Feature

This also adds a debug feature, which enables the debug_stack feature.

I added it because I think we may want to potentially add more debug tools in the future, and this gives us a good framework for adding those. Users who want all debug features, present and future, can just set debug. If they only want this feature, then they can just use debug_stack.

I also made the debug feature default to help capture the widest audience (i.e. the users who want this feature but don't know they do). However, if we think it's better as a non-default feature, I can change it!

And if there's any bikeshedding around the name debug_stack, let me know!

Testing

Run the following command:

cargo test --package bevy_reflect --features debug_stack

Changelog

  • Added the debug and debug_stack features to bevy_reflect
  • Updated the error messages returned by the reflection serializers and deserializers to include more contextual information when the debug_stack or debug feature is enabled

@MrGVSV MrGVSV added C-Docs An addition or correction to our documentation A-Reflection Runtime information about types D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward S-Blocked This cannot move forward until something else changes labels Jun 17, 2024
@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/serde-debug-stack branch 3 times, most recently from 26a00a8 to d85de9b Compare June 17, 2024 19:06
@MrGVSV MrGVSV removed the S-Blocked This cannot move forward until something else changes label Jun 17, 2024
@MrGVSV MrGVSV marked this pull request as ready for review June 17, 2024 19:06
@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use and removed C-Docs An addition or correction to our documentation labels Jul 14, 2024
Copy link
Contributor

@bushrat011899 bushrat011899 left a comment

Choose a reason for hiding this comment

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

This looks great, I love the idea of trying to provide better error messages to users when possible. I have only a couple of non-blocking comments that I thought I'd mention, otherwise LGTM!

crates/bevy_reflect/src/type_info_stack.rs Outdated Show resolved Hide resolved
Comment on lines 198 to 209
pub fn new(value: &'a dyn Reflect, registry: &'a TypeRegistry) -> Self {
#[cfg(feature = "debug_stack")]
TYPE_INFO_STACK.set(crate::type_info_stack::TypeInfoStack::new());

TypedReflectSerializer { value, registry }
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since TypedReflectSerializer is responsible for creating/clearing then TypeInfoStack, I feel like it should be possible to store the stack within this serializer, and pass references to it around like the TypeRegistry, perhaps wrapping it all in a ReflectContext.

I don't see any problems with the thread_local implementation as-is beyond a general ick around global state; you're justification is perfectly valid. Just making a note here for a possible future PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't fully remember unfortunately.

I believe the reason was that the serializer would need to hold onto a &mut TypeInfoStack but we don't want to require the user to define this themselves.

We might be able to get around this by creating an InternalReflectSerializer. Then ReflectSerializer can create and own the stack, and then pass a mutable reference to it to the internal serializer.

That would be a breaking change though. I'll try to look into it and see if it will actually work.

Copy link
Member Author

@MrGVSV MrGVSV Sep 8, 2024

Choose a reason for hiding this comment

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

Just looked into this and while I believe it's possible to do this with an internal serializer, it wouldn't really work properly across threads.

This is because a Serialize impl only requires a &self reference. This means that two references to the same serializer could be sent to different threads and be serialized at the same time (maybe to send multiple network packets, save to separate files, etc.). Doing so would cause the stack to receive entries from both, throwing it completely off.

So for now, I think using a thread-local is probably the safest option.

@rparrett rparrett added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 2, 2024
@alice-i-cecile
Copy link
Member

Gentle reminder to fix merge conflicts here.

@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/serde-debug-stack branch from d85de9b to e051c41 Compare September 8, 2024 18:21
@alice-i-cecile
Copy link
Member

@MrGVSV I regret to inform you that someone has caused merge conflicts for this again in #15107 ;) 😩

@alice-i-cecile alice-i-cecile added this to the 0.15 milestone Sep 9, 2024
@MrGVSV
Copy link
Member Author

MrGVSV commented Sep 9, 2024

@MrGVSV I regret to inform you that someone has caused merge conflicts for this again in #15107 ;) 😩

Now what kind of dummy would perform a huge refactor on the exact set of modules he just put up a PR for this PR is touching and urge the refactor to get merged first?!

...

@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/serde-debug-stack branch from b931cc8 to 995999a Compare September 9, 2024 16:07
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 9, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 9, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 9, 2024
@alice-i-cecile alice-i-cecile removed this pull request from the merge queue due to a manual request Sep 9, 2024
@alice-i-cecile
Copy link
Member

CI failures look real: you seem to have forgotten to feature flag an import.

@MrGVSV MrGVSV force-pushed the mrgvsv/reflect/serde-debug-stack branch from 995999a to 75076c0 Compare September 9, 2024 17:38
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 9, 2024
Merged via the queue into bevyengine:main with commit 90bb1ad Sep 9, 2024
30 checks passed
@MrGVSV MrGVSV deleted the mrgvsv/reflect/serde-debug-stack branch September 9, 2024 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Reflection Runtime information about types C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

4 participants