Skip to content

Dropping NonSend resources is unsound #3310

@alice-i-cecile

Description

@alice-i-cecile

Bevy version

0.5 (and present main)

What you did

use bevy::prelude::*;
use std::cell::UnsafeCell;
struct MyNonSend(UnsafeCell<()>);

impl Drop for MyNonSend {
    fn drop(&mut self) {
        dbg!("dropped");
    }
}

fn main() {
    let mut world = World::new();

    world.insert_non_send(MyNonSend(UnsafeCell::new(())));

    std::thread::spawn(move || {
        drop(world);
    })
    .join()
    .unwrap();
}

What you expected to happen

The NonSend resource is dropped correctly.

What actually happened

"Dropped" is printed.

Additional information

Full credit to @TheRawMeatball, @jakobhellermann @BoxyUwU and @DJMcNab. Blame them; I'm just the ticketmaster!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorP-HighThis is particularly urgent, and deserves immediate attentionP-UnsoundA bug that results in undefined compiler behaviorS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions