Skip to content

html/template: deadlock if "dot" method is used to execute template from same set #43855

Closed
@ianlancetaylor

Description

@ianlancetaylor

The fix for #39807 has introduced a potential deadlock if a method of the "dot" value in an html/template is used to execute another template from the same template set.

This code, written in the form of a test, demonstrates the problem. If the test is run, it hangs or dies with fatal error: all goroutines are asleep - deadlock!.

// recursiveInvoker is for TestRecursiveExecuteViaMethod.
type recursiveInvoker struct{
	t    *testing.T
	tmpl *Template
}

func (r *recursiveInvoker) Recur() (string, error) {
	var sb strings.Builder
	if err := r.tmpl.ExecuteTemplate(&sb, "subroutine", nil); err != nil {
		r.t.Fatal(err)
	}
	return sb.String(), nil
}

func TestRecursiveExecuteViaMethod(t *testing.T) {
	tmpl := New("")
	top, err := tmpl.New("x.html").Parse(`{{.Recur}}`)
	if err != nil {
		t.Fatal(err)
	}
	_, err = tmpl.New("subroutine").Parse(`<a href="/x?p={{"'a<b'"}}">`)
	if err != nil {
		t.Fatal(err)
	}
	r := &recursiveInvoker{
		t:    t,
		tmpl: tmpl,
	}
	if err := top.Execute(io.Discard, r); err != nil {
		t.Fatal(err)
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions