Skip to content

text/template: data race in template parsing #53234

@eliben

Description

@eliben

What version of Go are you using (go version)?

tip

Issue description

Uncovered while working on https://go-review.googlesource.com/c/go/+/410296

Tree.Parse in text/template/parse/parse.go invokes:

  t.startParse(funcs, lex(t.Name, text, leftDelim, rightDelim, emitComment), treeSet)

Where startParse is:

func (t *Tree) startParse(funcs []map[string]any, lex *lexer, treeSet map[string]*Tree) {
	t.Root = nil
	t.lex = lex
	t.vars = []string{"$"}
	t.funcs = funcs
	t.treeSet = treeSet
	lex.breakOK = !t.hasFunction("break")
	lex.continueOK = !t.hasFunction("continue")
}

The lexer passed into startParse starts a goroutine which runs and accesses breakOK and continueOK, and potentially races with their initialization in startParse.

Before https://go-review.googlesource.com/c/go/+/410296 this issue doesn't manifest because the lexer's items channel is blocking and the lexer won't get to an identifier until the reader pulled at least one token. Once a buffer is added to the channel, however, the lexer runs ahead a bit and triggers the race.

The fix should be to remove the racing access to these fields.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions