aymerick / raymond

Handlebars for golang
MIT License
610 stars 101 forks source link

raymond isn't thread safe #3

Closed markbates closed 8 years ago

markbates commented 8 years ago

If you run the tests with the -race flag you'll find that raymond does not deal with it very well:

$ go test -race ./...

Produces the following output

==================
WARNING: DATA RACE
Write by goroutine 19:
  github.com/aymerick/raymond/lexer.(*Lexer).next()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/lexer/lexer.go:160 +0x14f
  github.com/aymerick/raymond/lexer.lexContent()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/lexer/lexer.go:315 +0x160
  github.com/aymerick/raymond/lexer.(*Lexer).run()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/lexer/lexer.go:147 +0xa1

Previous read by goroutine 18:
  github.com/aymerick/raymond/parser.(*parser).parseProgram()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/parser/parser.go:107 +0x5e
  github.com/aymerick/raymond/parser.Parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/parser/parser.go:54 +0xa4
  github.com/aymerick/raymond.(*Template).parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/template.go:70 +0x86
  github.com/aymerick/raymond.Parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/template.go:37 +0x1a9
  github.com/aymerick/raymond.Render()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/raymond.go:9 +0x60
  github.com/aymerick/raymond.TestHelperCtx()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/helper_test.go:189 +0x13b
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:473 +0xdc

Goroutine 19 (running) created at:
  github.com/aymerick/raymond/lexer.scanWithName()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/lexer/lexer.go:103 +0x166
  github.com/aymerick/raymond/lexer.Scan()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/lexer/lexer.go:89 +0x44
  github.com/aymerick/raymond/parser.new()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/parser/parser.go:42 +0x3c
  github.com/aymerick/raymond/parser.Parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/parser/parser.go:51 +0x91
  github.com/aymerick/raymond.(*Template).parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/template.go:70 +0x86
  github.com/aymerick/raymond.Parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/template.go:37 +0x1a9
  github.com/aymerick/raymond.Render()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/raymond.go:9 +0x60
  github.com/aymerick/raymond.TestHelperCtx()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/helper_test.go:189 +0x13b
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:473 +0xdc

Goroutine 18 (running) created at:
  testing.RunTests()
      /usr/local/go/src/testing/testing.go:582 +0xae2
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:515 +0x11d
  main.main()
      github.com/aymerick/raymond/_test/_testmain.go:132 +0x210
==================
==================
WARNING: DATA RACE
Write by goroutine 20:
  github.com/aymerick/raymond/lexer.(*Lexer).next()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/lexer/lexer.go:160 +0x14f
  github.com/aymerick/raymond/lexer.lexContent()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/lexer/lexer.go:315 +0x160
  github.com/aymerick/raymond/lexer.(*Lexer).run()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/lexer/lexer.go:147 +0xa1

Previous read by goroutine 18:
  github.com/aymerick/raymond/parser.(*parser).parseProgram()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/parser/parser.go:107 +0x5e
  github.com/aymerick/raymond/parser.Parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/parser/parser.go:54 +0xa4
  github.com/aymerick/raymond.(*Template).parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/template.go:70 +0x86
  github.com/aymerick/raymond.Parse()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/template.go:37 +0x1a9
  github.com/aymerick/raymond.Render()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/raymond.go:9 +0x60
  github.com/aymerick/raymond.TestHelperCtx.func1()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/helper_test.go:181 +0xca
  runtime.call64()
      /usr/local/go/src/runtime/asm_amd64.s:473 +0x3d
  reflect.Value.Call()
      /usr/local/go/src/reflect/value.go:303 +0xcd
  github.com/aymerick/raymond.(*evalVisitor).callFunc()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/eval.go:621 +0xd6b
  github.com/aymerick/raymond.(*evalVisitor).callHelper()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/eval.go:628 +0xbb
  github.com/aymerick/raymond.(*evalVisitor).VisitExpression()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/eval.go:895 +0xa81
  github.com/aymerick/raymond/ast.(*Expression).Accept()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/ast/node.go:400 +0x4a
  github.com/aymerick/raymond.(*evalVisitor).VisitMustache()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/eval.go:777 +0x139
  github.com/aymerick/raymond/ast.(*MustacheStatement).Accept()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/ast/node.go:221 +0x4a
  github.com/aymerick/raymond.(*evalVisitor).VisitProgram()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/eval.go:762 +0x206
  github.com/aymerick/raymond/ast.(*Program).Accept()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/ast/node.go:181 +0x4a
  github.com/aymerick/raymond.(*Template).ExecWith()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/template.go:220 +0x176
  github.com/aymerick/raymond.(*Template).Exec()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/template.go:194 +0x77
  github.com/aymerick/raymond.Render()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/raymond.go:15 +0xc8
  github.com/aymerick/raymond.TestHelperCtx()
      /Users/markbates/Dropbox/development/gocode/src/github.com/aymerick/raymond/helper_test.go:189 +0x13b
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:473 +0xdc

...

Goroutine 8 (running) created at:
  testing.RunTests()
      /usr/local/go/src/testing/testing.go:582 +0xae2
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:515 +0x11d
  main.main()
      github.com/aymerick/raymond/parser/_test/_testmain.go:56 +0x210
==================
PASS
Found 50 data race(s)
FAIL    github.com/aymerick/raymond/parser  1.086s
aymerick commented 8 years ago

Thanks for reporting that issue.

I just published raymond 2.0.1 with a fix.

markbates commented 8 years ago

Thanks so much for the quick turn around on fixing that.