Skip to content

Removing an Entity consisting of a Camera and ContrastAdaptiveSharpening in a conditional system causes crash #18896

@nickdangelo97

Description

@nickdangelo97

Bevy version

0.15.3

Relevant system information

AdapterInfo { name: "Apple M2 Pro", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }

What you did

Attempted to remove an entity consisting of a Camera and a ContrastAdaptiveSharpening component

mod render;

use bevy::{
    core_pipeline::{
        contrast_adaptive_sharpening::ContrastAdaptiveSharpening,
        fxaa::{Fxaa, Sensitivity},
    },
    input::common_conditions::input_just_released,
    pbr::ShadowFilteringMethod,
    prelude::*,
    window::{PresentMode, WindowResolution},
};

fn main() {
    App::new()
        .add_plugins((DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window {
                title: "Camera Removal Test".into(),
                present_mode: PresentMode::AutoNoVsync,
                resolution: WindowResolution::new(1920.0, 1080.0).with_scale_factor_override(1.0),
                ..default()
            }),
            ..default()
        }),))
        .add_systems(Startup, setup)
        .add_systems(Update, update.run_if(input_just_released(KeyCode::KeyK)))
        .run();
}

pub const CAS: ContrastAdaptiveSharpening = ContrastAdaptiveSharpening {
    enabled: true,
    sharpening_strength: 0.6, // bevy recommended default
    denoise: false,
};

pub const MSAA: Msaa = Msaa::Sample4;
pub const SHADOW_FILTERING: ShadowFilteringMethod = ShadowFilteringMethod::Gaussian;
pub const FXAA: Fxaa = Fxaa {
    enabled: true,
    edge_threshold: Sensitivity::High,
    edge_threshold_min: Sensitivity::High,
};

fn setup(mut commands: Commands) {
    commands.spawn((
        Camera3d::default(),
        Msaa::Sample4,
        Fxaa {
            enabled: true,
            edge_threshold: Sensitivity::High,
            edge_threshold_min: Sensitivity::High,
        },
        ContrastAdaptiveSharpening::default(),
    ));
}

fn update(mut commands: Commands, entities: Query<Entity, With<Camera3d>>) {
    if !entities.is_empty() {
        commands.entity(entities.single()).despawn();
    }
}

In this example, the run_if condition is based upon a key event, but this also occurs using an Event

What went wrong

The application crashes after the update system is run.

Additional information

After crashing the following log output is produced:

bevy_core_pipeline-0.15.3/src/contrast_adaptive_sharpening/mod.rs:253:18:
Attempting to create an EntityCommands for entity 3v1#4294967299, which doesn't exist.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_core_pipeline::contrast_adaptive_sharpening::prepare_cas_pipelines`!
2025-04-22T02:46:16.512081Z  WARN bevy_ecs::world::command_queue: CommandQueue has un-applied commands being dropped. Did you forget to call SystemState::apply?

My hunch is that the prepare_cas_pipelines system is looping over a list of removals (RemovedComponents<CasUniform>) and performing commands.entity(entity).remove::<ViewCasPipeline>(). The entity in question has already been removed in the main world, hence the Attempting to create an EntityCommands for entity 3v1#4294967299, which doesn't exist. Link to source: https://github.com/bevyengine/bevy/blob/main/crates/bevy_anti_aliasing/src/contrast_adaptive_sharpening/mod.rs#L238

should this be replaced with get_entity instead of entity? Or is there something inherently wrong with the logic of the example?

Appreciate any assistance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-BugAn unexpected or incorrect behaviorP-CrashA sudden unexpected crashS-Needs-InvestigationThis issue requires detective work to figure out what's going wrong

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions