Repository: bevyengine/bevy
File: crates/bevy_render/src/error_handler.rs (Rust)

Code before:
use bevy_ecs::{resource::Resource, world::World};
use std::sync::Mutex;
use wgpu::ErrorSource;
use wgpu_types::error::ErrorType;

use crate::{
    insert_future_resources,
    render_resource::PipelineCache,
    renderer::{RenderDevice, WgpuWrapper},
    settings::{RenderCreation, WgpuSettings},
    FutureRenderResources,
};

/// Resource to indicate renderer behavior upon error.
#[expect(clippy::large_enum_variant, reason = "ergonomics")]
#[derive(Resource, Default)]
pub enum RenderErrorPolicy {
    /// Panics on error.
    Panic,
    #[default]
    /// Signals app exit on error.
    Shutdown,
    /// Keeps the app alive, but stops rendering further.
    StopRendering,
    /// Attempt renderer recovery the given number of times.
    Recover(usize, WgpuSettings),
}

/// The current state of the renderer.
#[derive(Resource, Debug)]
pub(crate) enum RenderState {
    /// Just started, [`crate::RenderStartup`] will run in this state.
    Initializing,
    /// Everything is okay and we are rendering stuff every frame.
    Ready,
    /// An error was encountered, and we may decide how to handle it.
    Errored(ErrorType, String, Option<WgpuWrapper<ErrorSource>>),
    /// We are recreating the render context after an error to recover.
    Reinitializing,
}

/// Resource to allows polling wgpu error handlers.
#[derive(Resource)]
pub(crate) struct RenderErrorHandler {
    device_lost: Arc<Mutex<Option<(wgpu::DeviceLostReason, String)>>>,
    uncaptured: Arc<Mutex<Option<WgpuWrapper<wgpu::Error>>>>,
}

impl RenderErrorHandler {
    /// Creates and registers error handlers on the given device and stores them to later be polled.
    pub(crate) fn new(device: &RenderDevice) -> Self {

Review comment (suggestion):
It is not clear from the docs how the number of recovery times is counted. Whether it's attempts-per-successful-recovery, or total attempts. (Which I would personally advise against, as it doesn't do a "full" recovery if it succeeds, it does a recovery that burns a finite resource of the app. Something like a fixed amount of recoveries per 5 seconds or whatever interval of time seems better, if the app stabilises again, then it should continue working as normally.)

Code after:
    resource::Resource,
    world::{Mut, World},
};
use std::sync::Mutex;
use wgpu::ErrorSource;
use wgpu_types::error::ErrorType;

use crate::{
    insert_future_resources,
    render_resource::PipelineCache,
    renderer::{RenderDevice, WgpuWrapper},
    settings::RenderCreation,
    FutureRenderResources, RenderStartup,
};

/// Resource to indicate renderer behavior upon error.
pub enum RenderErrorPolicy {
    /// Pretends nothing happened and continues rendering.
    /// This discards the error after logging it to console.
    Ignore,
    /// Keeps the app alive, but stops rendering further.
    /// This keeps the error state, and will continue polling the [`RenderErrorHandler`]
    /// every frame until some other policy is returned.
    StopRendering,
    /// Attempt renderer recovery with the given [`RenderCreation`].
    Recover(RenderCreation),
}

/// Determines what [`RenderErrorPolicy`] should be used to respond to a given [`RenderError`].
///
/// The handler has access to both the main world and the render world in that order.
/// By the time this is invoked, the error has already been logged. The error is provided
/// for the decision-making reason of how to appropriately respond to it. Not all errors
/// are equally severe: validation errors may be ignored for example, while device lost errors
/// require recovery to continue rendering.
#[derive(Resource)]
pub struct RenderErrorHandler(
    pub for<'a> fn(&'a RenderError, &'a mut World, &'a mut World) -> RenderErrorPolicy,
);

impl RenderErrorHandler {
    fn handle(&self, error: &RenderError, main_world: &mut World, render_world: &mut World) {
        match self.0(error, main_world, render_world) {
            RenderErrorPolicy::Ignore => {
                // Pretend that didn't happen.
                render_world.insert_resource(RenderState::Ready);
            }
            RenderErrorPolicy::StopRendering => {
                // do nothing
            }
            RenderErrorPolicy::Recover(render_creation) => {