Skip to content

Commit 7c8ce46

Browse files
authored
fix: panic: close of closed channel (#5929)
1 parent 059cc52 commit 7c8ce46

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ require (
134134
go.augendre.info/fatcontext v0.8.0
135135
go.uber.org/automaxprocs v1.6.0
136136
golang.org/x/mod v0.26.0
137+
golang.org/x/sync v0.15.0
137138
golang.org/x/sys v0.34.0
138139
golang.org/x/tools v0.34.0
139140
gopkg.in/yaml.v3 v3.0.1
@@ -213,7 +214,6 @@ require (
213214
go.uber.org/zap v1.24.0 // indirect
214215
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
215216
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
216-
golang.org/x/sync v0.15.0 // indirect
217217
golang.org/x/text v0.26.0 // indirect
218218
google.golang.org/protobuf v1.36.6 // indirect
219219
gopkg.in/ini.v1 v1.67.0 // indirect

pkg/goanalysis/runner_action.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goanalysis
22

33
import (
4+
"context"
45
"fmt"
56
"runtime/debug"
67

@@ -28,12 +29,14 @@ func (actAlloc *actionAllocator) alloc() *action {
2829
return act
2930
}
3031

31-
func (act *action) waitUntilDependingAnalyzersWorked(stopChan chan struct{}) {
32+
func (act *action) waitUntilDependingAnalyzersWorked(ctx context.Context, stopChan chan struct{}) {
3233
for _, dep := range act.Deps {
3334
if dep.Package == act.Package {
3435
select {
3536
case <-stopChan:
3637
return
38+
case <-ctx.Done():
39+
return
3740
case <-dep.analysisDoneCh:
3841
}
3942
}

pkg/goanalysis/runner_loadingpackage.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goanalysis
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"go/ast"
@@ -14,6 +15,7 @@ import (
1415
"sync"
1516
"sync/atomic"
1617

18+
"golang.org/x/sync/errgroup"
1719
"golang.org/x/tools/go/gcexportdata"
1820
"golang.org/x/tools/go/packages"
1921

@@ -83,36 +85,30 @@ func (lp *loadingPackage) analyze(stopChan chan struct{}, loadMode LoadMode, loa
8385
return
8486
}
8587

86-
var actsWg sync.WaitGroup
87-
88-
actsWg.Add(len(lp.actions))
88+
actsWg, ctx := errgroup.WithContext(context.Background())
8989

9090
for _, act := range lp.actions {
91-
go func(act *action) {
92-
defer actsWg.Done()
93-
94-
act.waitUntilDependingAnalyzersWorked(stopChan)
91+
actsWg.Go(func() error {
92+
act.waitUntilDependingAnalyzersWorked(ctx, stopChan)
9593

9694
select {
9795
case <-stopChan:
98-
return
96+
return nil
97+
case <-ctx.Done():
98+
return nil
9999
default:
100100
}
101101

102102
act.analyzeSafe()
103103

104-
select {
105-
case <-stopChan:
106-
return
107-
default:
108-
if act.Err != nil {
109-
close(stopChan)
110-
}
111-
}
112-
}(act)
104+
return act.Err
105+
})
113106
}
114107

115-
actsWg.Wait()
108+
err := actsWg.Wait()
109+
if err != nil {
110+
close(stopChan)
111+
}
116112
}
117113

118114
func (lp *loadingPackage) loadFromSource(loadMode LoadMode) error {

0 commit comments

Comments
 (0)