Skip to content

Commit f244a1a

Browse files
authored
Merge pull request #1396 from k1LoW/fix
fix: prevent CDP timeout goroutine leak using done channel and timer Stop
2 parents b7ebeb1 + 7912f63 commit f244a1a

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

cdp.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"reflect"
99
"sync"
10-
"sync/atomic"
1110
"time"
1211

1312
"github.com/chromedp/cdproto/target"
@@ -153,15 +152,15 @@ func (rnr *cdpRunner) run(ctx context.Context, cas CDPActions, s *step) error {
153152
defer o.capturers.captureCDPEnd(rnr.name)
154153

155154
// Set a timeout (cdpTimeoutByStep) for each step because Chrome operations may get stuck depending on the actions: specified.
156-
called := atomic.Bool{}
157-
defer func() {
158-
called.Store(true)
159-
}()
155+
done := make(chan struct{})
156+
defer close(done)
160157
timer := time.NewTimer(rnr.timeoutByStep)
158+
defer timer.Stop()
161159
go func() {
162-
<-timer.C
163-
if !called.Load() {
160+
select {
161+
case <-timer.C:
164162
rnr.Close()
163+
case <-done:
165164
}
166165
}()
167166

0 commit comments

Comments
 (0)