From 0e9522565575bf4aa69d56fa5654e72fd9189229 Mon Sep 17 00:00:00 2001 From: pjongy Date: Mon, 23 Oct 2023 13:40:30 +0000 Subject: [PATCH] Fix concurrency bug in generating tensor name TensorCount should be wrapped by lock - Tensor name came from {PREFIX}_{CURRENT_TENSOR_COUNT} - atomic Addint64 is good but generating name from this is quite bug prone --- ts/tensor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts/tensor.go b/ts/tensor.go index 2bc73da..61af2a0 100644 --- a/ts/tensor.go +++ b/ts/tensor.go @@ -65,6 +65,7 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor { x.ctensor = ctensor x.d = new(bigStruct) + lock.Lock() atomic.AddInt64(&TensorCount, 1) if gotch.Debug { nbytes := x.nbytes() @@ -72,7 +73,6 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor { log.Printf("INFO: Added tensor %q - Allocated memory: %d bytes.\n", x.name, nbytes) } - lock.Lock() name := newName(nameOpt...) if _, ok := ExistingTensors[name]; ok { name = fmt.Sprintf("%s_%09d", name, TensorCount)