Skip to content

Commit 370cadd

Browse files
hopehookgopherbot
authored andcommitted
cmd/compile: add a test case and some comments for deadlock on syntax error
After CL 398014 fixed a compiler deadlock on syntax errors, this CL adds a test case and more details for that. How it was fixed: CL 57751 introduced a channel "sem" to limit the number of simultaneously open files. Unfortunately, when the number of syntax processing goroutines exceeds this limit, will easily trigger deadlock. In the original implementation, "sem" only limited the number of open files, not the number of concurrent goroutines, which will cause extra goroutines to block on "sem". When the p.err of the following iteration happens to be held by the blocking goroutine, it will fall into a circular wait, which is a deadlock. CL 398014 fixed the above deadlock, also see issue golang#52127. First, move "sem <- struct{}{}" to the outside of the syntax processing goroutine, so that the number of concurrent goroutines does not exceed the number of open files, to ensure that all goroutines in execution can eventually write to p.err. Second, move the entire syntax processing logic into a separate goroutine to avoid blocking on the producer side. Change-Id: I1bb89bfee3d2703784f0c0d4ded82baab2ae867a Reviewed-on: https://go-review.googlesource.com/c/go/+/399054 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Auto-Submit: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
1 parent be0262a commit 370cadd

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/cmd/compile/internal/noder/noder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func LoadPackage(filenames []string) {
4040
noders[i] = &p
4141
}
4242

43+
// Move the entire syntax processing logic into a separate goroutine to avoid blocking on the "sem".
4344
go func() {
4445
for i, filename := range filenames {
4546
filename := filename

test/fixedbugs/issue52127.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// run
2+
//go:build !js
3+
// +build !js
4+
5+
// Copyright 2022 The Go Authors. All rights reserved.
6+
// Use of this source code is governed by a BSD-style
7+
// license that can be found in the LICENSE file.
8+
9+
// Issue 52127: Too many syntax errors in many files can
10+
// cause deadlocks instead of displaying error messages
11+
// correctly.
12+
13+
package main
14+
15+
import (
16+
"bytes"
17+
"fmt"
18+
"os"
19+
"os/exec"
20+
"path/filepath"
21+
)
22+
23+
func main() {
24+
dir, err := os.MkdirTemp("", "issue52127")
25+
if err != nil {
26+
panic(err)
27+
}
28+
defer os.RemoveAll(dir)
29+
30+
args := []string{"go", "build"}
31+
write := func(prefix string, i int, data string) {
32+
filename := filepath.Join(dir, fmt.Sprintf("%s%d.go", prefix, i))
33+
if err := os.WriteFile(filename, []byte(data), 0o644); err != nil {
34+
panic(err)
35+
}
36+
args = append(args, filename)
37+
}
38+
39+
for i := 0; i < 100; i++ {
40+
write("a", i, `package p
41+
`)
42+
}
43+
for i := 0; i < 100; i++ {
44+
write("b", i, `package p
45+
var
46+
var
47+
var
48+
var
49+
var
50+
`)
51+
}
52+
53+
cmd := exec.Command(args[0], args[1:]...)
54+
output, err := cmd.CombinedOutput()
55+
if err == nil {
56+
panic("compile succeeded unexpectedly")
57+
}
58+
if !bytes.Contains(output, []byte("syntax error:")) {
59+
panic(fmt.Sprintf(`missing "syntax error" in compiler output; got:
60+
%s`, output))
61+
}
62+
}

0 commit comments

Comments
 (0)