From 3c7163ef66daa946f960961aeff2f555d753d6e9 Mon Sep 17 00:00:00 2001 From: Pior Bastida Date: Fri, 4 Mar 2022 14:56:05 -0500 Subject: [PATCH] Fix a potential data race on a global variable --- version.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/version.go b/version.go index 590329ef8..7c396736b 100644 --- a/version.go +++ b/version.go @@ -1,11 +1,17 @@ package sarama -import "runtime/debug" +import ( + "runtime/debug" + "sync" +) -var v string +var ( + v string + vOnce sync.Once +) func version() string { - if v == "" { + vOnce.Do(func() { bi, ok := debug.ReadBuildInfo() if ok { v = bi.Main.Version @@ -15,6 +21,6 @@ func version() string { // the version v = "dev" } - } + }) return v }