{"record":{"id":"5926ad0b10c74961","repo":"tracel-ai/burn","slug":"tensor-id-was-never-written-error","errorCode":null,"errorMessage":"Tensor {id:?} was never written: {error}","messagePattern":"Tensor (.+?) was never written: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/burn-ir/src/handle.rs","lineNumber":351,"sourceCode":"            .unwrap_or_else(|| panic!(\"Should have handle for tensor {id:?}\"));\n\n        match handle {\n            Handle::Existing(handle) => match status {\n                TensorStatus::ReadOnly => {\n                    self.put(id, Handle::Existing(handle.clone()));\n                    handle\n                }\n                TensorStatus::ReadWrite => handle,\n                TensorStatus::NotInit => panic!(\n                    \"Cannot get uninitialized tensor {id:?}. Tensor exist but with wrong status\"\n                ),\n            },\n            Handle::NotInit => panic!(\"Cannot get uninitialized handle {id:?}.\"),\n            // The backstop for an `errored` count that has drifted below the\n            // map: the guard above is gated on it, so a drift would let an\n            // errored entry through to here. Not unreachable — reached only\n            // when the invariant `put`/`take` maintain has already broken.\n            Handle::Errored(error) => panic!(\"Tensor {id:?} was never written: {error}\"),\n        }\n    }\n\n    /// Get the tensor handle for the given [tensor intermediate representation](TensorIr).\n    pub fn get_tensor_handle(&mut self, tensor: &TensorIr) -> TensorHandle<H> {\n        TensorHandle {\n            handle: self.get_handle(&tensor.id, &tensor.status),\n            shape: tensor.shape.clone(),\n        }\n    }\n\n    /// Get the [float tensor](burn_backend::backend::BackendTypes::FloatTensorPrimitive) corresponding to the\n    /// given [tensor intermediate representation](TensorIr).\n    pub fn get_float_tensor<B>(&mut self, tensor: &TensorIr) -> B::FloatTensorPrimitive\n    where\n        B: BackendIr<Handle = H>,\n    {\n        B::float_tensor(self.get_tensor_handle(tensor))","sourceCodeStart":333,"sourceCodeEnd":369,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ir/src/handle.rs#L333-L369","documentation":"`HandleStore::get_handle` panics with 'was never written' when the handle for a tensor id is `Handle::Errored(error)` — the operation that was supposed to produce this tensor failed, and the stored error is surfaced when the tensor is consumed. The primary guard at the top of get_handle (handle.rs:325-329) reports this normally; this second panic at line 351 is a backstop reached only if the `errored` counter has drifted out of sync with the map, i.e. the put/take invariant is already broken.","triggerScenarios":"Consuming a tensor whose producing operation failed and registered `Handle::Errored` (the normal path via the top guard); or, for the line-351 backstop specifically, an `errored` count below the number of Errored map entries so the guard is bypassed.","commonSituations":"A GPU/kernel op failed earlier (OOM, invalid args, async launch error) and the failure only surfaces when the result tensor is read; error bookkeeping drift in a custom BackendIr/runtime integration.","solutions":["Read the embedded `{error}` in the message — it names the original failure from the producing operation; fix that error, not this panic site.","Call `take_error(&tensor)` before consuming tensors in fallible integrations so failures are handled before handle reads.","Audit custom backend code that calls `HandleStore::put`/`take`/`register_errored` for count/map drift if this fires without a prior real error.","Re-run with the producing operation isolated (smaller tensor, different backend) to expose the root failure."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// poll the deferred failure before consuming:\nif let Some(err) = store.take_error(&tensor_ir) {\n    return Err(anyhow!(\"tensor {:?} was never written: {err}\", tensor_ir.id));\n}","typeGuard":null,"tryCatchPattern":"match store.take_error(&tensor_ir) {\n    Some(err) => return Err(TensorError::NeverWritten(err)),\n    None => store.get_tensor_handle(&tensor_ir), // safe now\n}","preventionTips":["Always surface the embedded {error} — it is the real root cause from the producing op","Check take_error()/has_errors() at op boundaries instead of relying on get_handle panics","Keep the errored counter and the handle map in sync when customizing put/take","Isolate failing ops (smaller tensors, fallback backend) to expose the original failure"],"tags":["panic","burn","runtime","handle-store","deferred-error"],"backgroundTag":"tensor-never-written","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}