bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Abort on dropping NonSend in non-origin thread.

Open james7132 opened this issue 3 years ago • 0 comments

Objective

Fixes #3310.

Solution

Add a origin_thread_id to all Resources, check it on dropping ResourceData, if there's a mismatch, abort the process. Choosing to abort here over panic to avoid potentially problematic state being introduced by catching the unwind. The abort is replaced with a panics in tests since testing for aborts is painfully inflexible and difficult to maintain.

All !Send resources now individually track which thread they were inserted from. This is validated against for every access, mutation, and drop that could be done against the value.

This change will likely impact the performance of anything rapidly fetching, inserting, or removing resources, regardless of whether they're Send or not. This can be remedied by splitting the storage between Resources and NonSendResources and only force the latter to validate, which could be tackled in this PR or a followup.

A regression test using an altered version of the example from #3310 has been added.

james7132 avatar Nov 10 '22 10:11 james7132