{"record":{"id":"4aec781c37ee7491","repo":"FyroxEngine/Fyrox","slug":"attempt-to-get-reference-to-resource-data-while-it-4aec78","errorCode":null,"errorMessage":"Attempt to get reference to resource data while it is unloaded!","messagePattern":"Attempt to get reference to resource data while it is unloaded!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"fyrox-resource/src/lib.rs","lineNumber":636,"sourceCode":"                };\n                panic!(\"Attempt to get reference to resource data which failed to load! Type {}. Path: {path}. Error: {error:?}\", std::any::type_name::<T>())\n            }\n            ResourceState::Ok { ref data } => (data.inner_ref() as &dyn Any)\n                .downcast_ref()\n                .expect(\"Type mismatch!\"),\n        }\n    }\n}\n\nimpl<T> DerefMut for ResourceDataRef<'_, T>\nwhere\n    T: TypedResourceData,\n{\n    fn deref_mut(&mut self) -> &mut Self::Target {\n        let header = &mut *self.guard;\n        match header.state {\n            ResourceState::Unloaded => {\n                panic!(\"Attempt to get reference to resource data while it is unloaded!\")\n            }\n            ResourceState::Pending { .. } => {\n                panic!(\"Attempt to get reference to resource data while it is loading!\")\n            }\n            ResourceState::LoadError { .. } => {\n                panic!(\"Attempt to get reference to resource data which failed to load!\")\n            }\n            ResourceState::Ok { ref mut data, .. } => (data.inner_mut() as &mut dyn Any)\n                .downcast_mut()\n                .expect(\"Type mismatch!\"),\n        }\n    }\n}\n\n/// Collects all resources used by a given entity. Internally, it uses reflection to iterate over\n/// each field of every descendant sub-object of the entity. This function could be used to collect\n/// all resources used by an object, which could be useful if you're building a resource dependency\n/// analyzer.","sourceCodeStart":618,"sourceCodeEnd":654,"githubUrl":"https://github.com/FyroxEngine/Fyrox/blob/76c91aad8eca488ce527b1af707be8b3b24ad72d/fyrox-resource/src/lib.rs#L618-L654","documentation":"DerefMut on Resource<T> panics unless the resource is in the Ok state: Unloaded, Pending, and LoadError states have no (valid) mutable data to expose. Unlike the immutable deref, these panics carry no type/path details. Mutable access additionally requires exclusive access to the resource.","triggerScenarios":"Getting a mutable reference (data_ref_mut / &mut through the guard) to a resource before its load completed or after it failed; attempting to mutate resource data on a just-requested handle.","commonSituations":"Procedurally editing textures/models right after requesting them; retry/migration code that mutates resources on load without checking state first; mutating a resource whose reload is in flight.","solutions":["Check resource state is Ok (state().is_ok()) before any mutable dereference.","Await load completion (or use try_data_ref_mut-style access if available) and handle the error case explicitly.","Avoid mutating loaded resource data at all; build data up front or use separate runtime buffers instead of editing shared resources."],"exampleFix":"// before\nlet tex = rm.request::<Texture>(path)?;\ntex.data_ref_mut().set_pixel(x, y, color); // panics unless Ok\n// after\nlet tex = rm.request::<Texture>(path)?;\nif tex.state().is_ok() {\n    tex.data_ref_mut().set_pixel(x, y, color);\n}","handlingStrategy":"type-guard","validationCode":"if resource.state().is_ok() {\n    let data = resource.data_ref_mut();\n}","typeGuard":"fn mutable_ready<T: TypedResourceData>(r: &mut Resource<T>) -> bool { r.state().is_ok() }","tryCatchPattern":null,"preventionTips":["Gate every data_ref_mut call behind an is_ok state check.","Prefer generating/editeable copies of data over mutating shared loaded resources.","Avoid mutating resources whose reload (re-request) may be in flight."],"tags":["panic","resource-state","mutable-access","loading"],"backgroundTag":"invalid-state-transition","analyzedSha":"76c91aad8eca488ce527b1af707be8b3b24ad72d","analyzedAt":"2026-09-10T16:04:01.633Z","contentChangedAt":"2026-09-10T16:04:01.633Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}