-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Data race when calling funcs.Create*Funcs functions concurrently #1126
Comments
Hi @nandu-vinodan, thanks for logging this issue! In theory, Your global It can be reproduced simply with a test like this: package main
import (
"testing"
)
var n int
func doSomething() {
n = 42
}
func TestSomething(t *testing.T) {
for i := 0; i < 5; i++ {
go doSomething()
}
} $ go test -race race_test.go
==================
WARNING: DATA RACE
Write at 0x000001333890 by goroutine 9:
command-line-arguments.doSomething()
/tmp/race_test.go:10 +0x3a
Previous write at 0x000001333890 by goroutine 8:
command-line-arguments.doSomething()
/tmp/race_test.go:10 +0x3a
Goroutine 9 (running) created at:
command-line-arguments.TestSomething()
/tmp/race_test.go:15 +0x4b
testing.tRunner()
/usr/local/go/src/testing/testing.go:1193 +0x202
Goroutine 8 (finished) created at:
command-line-arguments.TestSomething()
/tmp/race_test.go:15 +0x4b
testing.tRunner()
/usr/local/go/src/testing/testing.go:1193 +0x202
==================
--- FAIL: TestSomething (0.00s)
testing.go:1092: race detected during execution of test
FAIL
FAIL command-line-arguments 0.110s
FAIL You shouldn't need to call The one exception is the I hope that helps! I'm going to assume that this solves your issue, and so I'm going to close this issue. If there's something I've missed please feel free to re-open it. Also if you have any general questions around using gomplate programatically (or otherwise!), please feel free to start a discussion! |
@hairyhenderson Thank you for responding! I was able to reproduce the issue even without a global variable:
Thanks for confirming this. I'll invoke once and reuse the funcMap. |
Ahh, my apologies @nandu-vinodan! The issue is indeed in the Line 34 in 8eeedae
Even though Because there is indeed a race condition, and even though there's no reason for |
One more thing I should mention which I had forgotten - the functions in the If I'd known about the All that to say - I would recommend only calling |
Thanks for the quick turnaround @hairyhenderson. #1127 fixes this issue. |
I am currently working on using gomplate’s
CreateFuncs()
to run template execution withtext/template.
I am running into DATA_RACE warnings when multiple go routines execute the
CreateFuncs
function concurrently.Sample code to reproduce the issue:
Running
go run -race main.go
gives:I am able to work-around this using a
sync.Mutex
variable for thesetFuncMap()
function.Would like to know if we can also use
sync.Once
to create once and reuse it everywhere. Wondering if gomplate stores some state information internally which prohibits reusinggomplate.CreateFuncs()
's return value multiple times.The text was updated successfully, but these errors were encountered: