carlos / platform Public

activator tests: guard finds starters used as values, context.AfterFunc

Astra round 4 P2: the guard matched only direct time.AfterFunc and .Go
calls. It now reports any reference to time.AfterFunc or
context.AfterFunc (resolving each file's import names, dot imports
included) and any .Go selector, called or used as a value: a local, a
package-level variable, a method value, an argument. The self-test
gains a fixture for each of those cases.

Refs #1249
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 33ee6309bace8be01f0ed37265564d8fa1a56889 parent 6a5c905
1 file changed, +120 −24
  • internal/activator/knobguard_test.go +120 −24
diff --git a/internal/activator/knobguard_test.go b/internal/activator/knobguard_test.go
index 473b79b69..a6889cd65 100644
--- a/internal/activator/knobguard_test.go
+++ b/internal/activator/knobguard_test.go
@@ -5,8 +5,10 @@ import (
"go/ast"
"go/parser"
"go/token"
+ pathpkg "path"
"path/filepath"
"slices"
+ "strconv"
"strings"
"testing"
)
@@ -31,8 +33,10 @@ var untrackedStarterExemptions = map[string]string{
// goroutine a test starts can read a setting in production code it calls,
// and only goTracked orders its reads before the restore (#1249). So test
// code a test that changes a setting can reach starts goroutines with
-// goTracked, and timers with afterTracked: no go statement, time.AfterFunc
-// or Go method (sync.WaitGroup, errgroup). Methods of test types are checked too, whoever reaches them:
+// goTracked, and timers with afterTracked: no go statement, and no
+// reference at all, called or used as a value, to time.AfterFunc,
+// context.AfterFunc (under any import name) or a Go method (sync.WaitGroup,
+// errgroup). Methods of test types are checked too, whoever reaches them:
// production calls them, from any test.
func TestKnobTestsStartNoUntrackedGoroutine(t *testing.T) {
paths, err := filepath.Glob("*_test.go")
@@ -73,10 +77,21 @@ func TestKnobTestsStartNoUntrackedGoroutine(t *testing.T) {
// The guard finds a goroutine started directly by a test that changes a
// setting, one started by a helper it calls, one a test reaches through a
-// rig that changes the setting, and one in a test type's method. It passes
-// goTracked and a test that changes no setting.
+// rig that changes the setting, and one in a test type's method. It finds
+// a starter used as a value as well as one called: time.AfterFunc held in
+// a local or a package-level variable, a WaitGroup's Go as a method value,
+// time under another import name or dot-imported, and context.AfterFunc.
+// It passes goTracked, a test that changes no setting, and an AfterFunc
+// that is not time's or context's.
func TestKnobGoroutineGuardFindsUntrackedStarts(t *testing.T) {
- const src = `package activator
+ srcs := map[string]string{
+ "guard_test.go": `package activator
+
+import (
+ "context"
+ "sync"
+ "time"
+)
func rig(t *testing.T) { setKnob(t, &wakeWaitBound, 1) }
func helper() { go work() }
@@ -87,23 +102,92 @@ func TestTimer(t *testing.T) { rig(t); time.AfterFunc(d, func() {}) }
func TestGroup(t *testing.T) { rig(t); var wg sync.WaitGroup; wg.Go(func() {}) }
func TestNoSetting(t *testing.T) { go func() {}() }
func (f *fake) Start() { go f.run() }
-`
+func TestTimerValue(t *testing.T) { rig(t); start := time.AfterFunc; start(d, func() {}) }
+var startLater = time.AfterFunc
+func TestPackageVar(t *testing.T) { rig(t); startLater(d, func() {}) }
+func TestGroupValue(t *testing.T) { rig(t); var wg sync.WaitGroup; g := wg.Go; g(func() {}) }
+func TestContextAfterFunc(t *testing.T) { rig(t); context.AfterFunc(ctx, func() {}) }
+func TestOtherAfterFunc(t *testing.T) { rig(t); clock.AfterFunc(d, func() {}) }
+`,
+ "guard_alias_test.go": `package activator
+
+import tm "time"
+
+func TestAliasedTime(t *testing.T) { rig(t); tm.AfterFunc(d, func() {}) }
+`,
+ "guard_dot_test.go": `package activator
+
+import . "time"
+
+func TestDotTime(t *testing.T) { rig(t); AfterFunc(d, func() {}) }
+`,
+ }
fset := token.NewFileSet()
- f, err := parser.ParseFile(fset, "guard_test.go", src, parser.SkipObjectResolution)
- if err != nil {
- t.Fatal(err)
+ var files []*ast.File
+ for name, src := range srcs {
+ f, err := parser.ParseFile(fset, name, src, parser.SkipObjectResolution)
+ if err != nil {
+ t.Fatal(err)
+ }
+ files = append(files, f)
}
var got []string
- for _, s := range untrackedStarters(fset, []*ast.File{f}) {
- got = append(got, s.fn)
+ for _, s := range untrackedStarters(fset, files) {
+ got = append(got, s.fn+": "+s.kind)
}
slices.Sort(got)
- want := []string{"Start", "TestDirect", "TestGroup", "TestTimer", "helper"}
+ want := []string{
+ "Start: go statement",
+ "TestAliasedTime: time.AfterFunc",
+ "TestContextAfterFunc: context.AfterFunc",
+ "TestDirect: go statement",
+ "TestDotTime: time.AfterFunc",
+ "TestGroup: Go method",
+ "TestGroupValue: Go method",
+ "TestTimer: time.AfterFunc",
+ "TestTimerValue: time.AfterFunc",
+ "helper: go statement",
+ "startLater: time.AfterFunc",
+ }
if !slices.Equal(got, want) {
- t.Fatalf("the guard found starts in %v, want %v", got, want)
+ t.Fatalf("the guard found:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
}
}
+// afterFuncPackages are the standard packages whose AfterFunc runs a
+// callback on a goroutine of its own.
+var afterFuncPackages = map[string]bool{"time": true, "context": true}
+
+// fileImports is how one file names its imports.
+type fileImports struct {
+ // byName maps the name the file uses for an import to its path.
+ byName map[string]string
+ // dotAfterFunc is the path of a dot-imported afterFuncPackages
+ // package, whose AfterFunc the file names bare, or "".
+ dotAfterFunc string
+}
+
+func importsOf(f *ast.File) fileImports {
+ imports := fileImports{byName: map[string]string{}}
+ for _, imp := range f.Imports {
+ path, err := strconv.Unquote(imp.Path.Value)
+ if err != nil {
+ continue
+ }
+ name := pathpkg.Base(path)
+ if imp.Name != nil {
+ name = imp.Name.Name
+ }
+ switch {
+ case name == "." && afterFuncPackages[path]:
+ imports.dotAfterFunc = path
+ case name != "." && name != "_":
+ imports.byName[name] = path
+ }
+ }
+ return imports
+}
+
// goroutineStart is a goroutine start the guard found.
type goroutineStart struct {
fn, kind string
@@ -132,21 +216,32 @@ type guardFunc struct {
func untrackedStarters(fset *token.FileSet, files []*ast.File) []goroutineStart {
byName := map[string][]*guardFunc{}
var all []*guardFunc
- add := func(name string, method bool, body ast.Node) {
+ add := func(name string, method bool, body ast.Node, imports fileImports) {
g := &guardFunc{name: name, method: method, refs: map[string]bool{}}
+ seen := map[token.Pos]bool{}
+ start := func(n ast.Node, kind string) {
+ if !seen[n.Pos()] {
+ seen[n.Pos()] = true
+ g.starts = append(g.starts, goroutineStart{fn: name, kind: kind, pos: fset.Position(n.Pos())})
+ }
+ }
ast.Inspect(body, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.Ident:
g.refs[n.Name] = true
+ if path := imports.dotAfterFunc; path != "" && n.Name == "AfterFunc" {
+ start(n, path+".AfterFunc")
+ }
case *ast.GoStmt:
- g.starts = append(g.starts, goroutineStart{fn: name, kind: "go statement", pos: fset.Position(n.Pos())})
- case *ast.CallExpr:
- if sel, ok := n.Fun.(*ast.SelectorExpr); ok {
- if x, ok := sel.X.(*ast.Ident); ok && x.Name == "time" && sel.Sel.Name == "AfterFunc" {
- g.starts = append(g.starts, goroutineStart{fn: name, kind: "time.AfterFunc", pos: fset.Position(n.Pos())})
- } else if sel.Sel.Name == "Go" {
- g.starts = append(g.starts, goroutineStart{fn: name, kind: "Go method", pos: fset.Position(n.Pos())})
- }
+ start(n, "go statement")
+ case *ast.SelectorExpr:
+ // A reference, called or not: a function value, a
+ // package-level variable's initialiser or an argument
+ // starts the goroutine wherever it is called.
+ if x, ok := n.X.(*ast.Ident); ok && n.Sel.Name == "AfterFunc" && afterFuncPackages[imports.byName[x.Name]] {
+ start(n, imports.byName[x.Name]+".AfterFunc")
+ } else if n.Sel.Name == "Go" {
+ start(n, "Go method")
}
}
return true
@@ -155,18 +250,19 @@ func untrackedStarters(fset *token.FileSet, files []*ast.File) []goroutineStart
all = append(all, g)
}
for _, f := range files {
+ imports := importsOf(f)
for _, d := range f.Decls {
switch d := d.(type) {
case *ast.FuncDecl:
if d.Body != nil {
- add(d.Name.Name, d.Recv != nil, d.Body)
+ add(d.Name.Name, d.Recv != nil, d.Body, imports)
}
case *ast.GenDecl:
for _, spec := range d.Specs {
if vs, ok := spec.(*ast.ValueSpec); ok {
for _, v := range vs.Values {
for _, n := range vs.Names {
- add(n.Name, false, v)
+ add(n.Name, false, v, imports)
}
}
}