FyroxEngine/Fyrox · critical

Attempt to get reference to resource data while it is…

Error message

Attempt to get reference to resource data while it is loading!

What it means

Fyrox resources are shared handles whose data is guarded by ResourceState. deref_mut() must only hand out &mut T when the resource is in ResourceState::Ok. If the resource is still Pending (being loaded asynchronously), the guard panics instead of returning a reference to uninitialized or in-flight data.

Solutions

  1. Wait until the resource is loaded before mutating: loop on resource.state() or use resource.clone().await / event-driven resource loading (ResourceEventBindings) before calling deref_mut.
  2. Check state explicitly: only enter the mutation path when matches!(resource.state(), ResourceState::Ok{..}).
  3. Use try_data_ref / data_ref-style non-panicking accessors where available and handle the None/loading case.
  4. Ensure the resource actually has a registered loader so it transitions to Ok instead of staying in a load pipeline; check logs for load errors.

Example fix

// before
let mut texture_data = texture.data_mut(); // panics while loading
texture_data.set_minification_filter(FilterMode::Trilinear);
// after
if let ResourceState::Ok(_) = &*resource.state() {
    let mut texture_data = resource.data_mut();
    texture_data.set_minification_filter(FilterMode::Trilinear);
}
Defensive patterns

Strategy: validation

Validate before calling

fn is_loaded<T: ResourceData>(r: &Resource<T>) -> bool {
    matches!(&*r.state(), ResourceState::Ok { .. })
}
// call r.data_mut() only when is_loaded(&r)

Type guard

fn loaded_data<T: ResourceData>(r: &Resource<T>) -> Option<ResourceDataRef<'_, T>> {
    match &*r.state() { ResourceState::Ok { .. } => Some(r.data_ref()), _ => None }
}

Prevention

When it happens

Trigger: Calling deref_mut() (e.g. via DerefMut on ResourceDataRef or explicit data access) on a resource whose state() is ResourceState::Pending — i.e. the resource was requested with request_resource/try_data_ref before its async load finished.

Common situations: Accessing a texture/model/sound buffer on the same frame it was requested; game code that dereferences resources in init() before the asset loader thread completes; blocking on data only in one thread while another thread mutates through deref_mut during load.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of FyroxEngine/Fyrox@76c91aad8e (2026-09-10). Data as JSON: /api/errors/51b110006023199e. Report an issue: GitHub.

Appendix: source

Thrown at fyrox-resource/src/lib.rs:639

            ResourceState::Ok { ref data } => (data.inner_ref() as &dyn Any)
                .downcast_ref()
                .expect("Type mismatch!"),
        }
    }
}

impl<T> DerefMut for ResourceDataRef<'_, T>
where
    T: TypedResourceData,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        let header = &mut *self.guard;
        match header.state {
            ResourceState::Unloaded => {
                panic!("Attempt to get reference to resource data while it is unloaded!")
            }
            ResourceState::Pending { .. } => {
                panic!("Attempt to get reference to resource data while it is loading!")
            }
            ResourceState::LoadError { .. } => {
                panic!("Attempt to get reference to resource data which failed to load!")
            }
            ResourceState::Ok { ref mut data, .. } => (data.inner_mut() as &mut dyn Any)
                .downcast_mut()
                .expect("Type mismatch!"),
        }
    }
}

/// Collects all resources used by a given entity. Internally, it uses reflection to iterate over
/// each field of every descendant sub-object of the entity. This function could be used to collect
/// all resources used by an object, which could be useful if you're building a resource dependency
/// analyzer.
pub fn collect_used_resources(
    entity: &dyn Reflect,
    resources_collection: &mut FxHashSet<UntypedResource>,

View on GitHub (pinned to 76c91aad8e)