Skip to content

Make !Send data initalization use closures #24390

Description

@NthTensor

This is a tracking issue for making !Send data use closures for initialization.

Goal

We currently allow users to directly insert instances of non-send types into a bevy App before startup. This pins that app, and the entire ECS update loop, onto the thread on which those types are constructed (usually the main process thread), and forces us to mount the ECS update loop within the windowing update loop.

This "run-loop turducken" is cursed and bad, and no one I have ever talked to thinks this is a good idea. The first step to removing it is fixing the initialization step. Soooooo...

Proposal

  1. Allow users to provide Send closures that insert objects into the "Main-Thread Storage" during setup.
fn setup_non_send<'app, F, R>(&'app mut self, func: F) -> &mut App
where 
    F: FnOnce(world: &mut World) -> R + Send + 'app
    R: 'static

Closures provided to this function will be retained in the app, and then called only once the runner has started up it's ecs event-loop (on a thread of it's choosing).

  1. Deprecate all existing ways to directly insert !Send data.

    We do not have to deprecate init_non_send (or init_main_thread_storage or whatever it's called at this point). We do have to deprecate insert_non_send; users should try to migrate to setup_non_send instead.

  2. After a full release, remove the deprecated methods.

  3. As follow up, move the ECS out of the main windowing thread, then initialize the non-send data on that new thread.

Tracking

This has the potential to break users, so we are going to "scream-test" it. This issue should be the main place to discuss the change, and any issues caused by it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-AppBevy apps and pluginsA-ECSEntities, components, systems, and eventsA-WindowingPlatform-agnostic interface layer to run your app inC-Code-QualityA section of code that is hard to understand or changeC-Tracking-IssueAn issue that collects information about a broad development initiativeD-StraightforwardSimple bug fixes and API improvements, docs, test and examples

    Type

    No type

    Projects

    Status
    Needs SME Triage

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions