diff --git a/candle-core/src/tensor_cat.rs b/candle-core/src/tensor_cat.rs index be6dfe61cc..20b805c76d 100644 --- a/candle-core/src/tensor_cat.rs +++ b/candle-core/src/tensor_cat.rs @@ -248,6 +248,9 @@ impl Tensor { if !self.is_contiguous() || !src.is_contiguous() { Err(Error::RequiresContiguous { op: "slice-set" }.bt())? } + if self.same_storage(src) { + crate::bail!("cannot use slice_set when self and src share their storage") + } if self.dtype() != src.dtype() { Err(Error::DTypeMismatchBinaryOp { lhs: self.dtype(), diff --git a/candle-core/tests/tensor_tests.rs b/candle-core/tests/tensor_tests.rs index e3246a33a5..17238dcdae 100644 --- a/candle-core/tests/tensor_tests.rs +++ b/candle-core/tests/tensor_tests.rs @@ -729,6 +729,8 @@ fn slice_set(device: &Device) -> Result<()> { .sum_all()? .to_vec0::()?; assert_eq!(diff, 0.); + // This used to create a deadlock rather than returning an actual error. + assert!(cache.slice_set(&cache, 0, 0).is_err()); Ok(()) }