Skip to content

Commit 77f0172

Browse files
authored
Ensure coroutine exits
1 parent de05a2c commit 77f0172

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

internal/sync/coroutine.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,25 @@ func NewCoroutine(ctx Context, fn func(ctx Context) error) Coroutine {
7777
return s
7878
}
7979

80+
// var i = 0
81+
8082
func newState() *coState {
83+
// i++
84+
8185
return &coState{
8286
blocking: make(chan bool, 1),
8387
unblock: make(chan bool),
8488
logger: log.New(io.Discard, "[co]", log.LstdFlags),
85-
//logger: log.New(os.Stderr, fmt.Sprintf("[co %v]", i), log.Lmsgprefix|log.Ltime),
89+
// logger: log.New(os.Stderr, fmt.Sprintf("[co %v]", i), log.Lmsgprefix|log.Ltime),
8690
deadlockDetection: DeadlockDetection,
8791
}
8892
}
8993

9094
func (s *coState) finish() {
9195
s.finished.Store(true)
9296
s.blocking <- true
97+
98+
s.logger.Println("finish")
9399
}
94100

95101
func (s *coState) SetScheduler(scheduler Scheduler) {
@@ -137,8 +143,10 @@ func (s *coState) yield(markBlocking bool) {
137143

138144
<-s.unblock
139145
if s.shouldExit.Load() != nil {
146+
s.logger.Println("shouldExit")
147+
s.blocking <- true
148+
s.logger.Println("goexit")
140149
runtime.Goexit()
141-
s.logger.Println("exit")
142150
}
143151

144152
s.blocked.Store(false)
@@ -171,6 +179,8 @@ func (s *coState) Execute() {
171179
}
172180

173181
func (s *coState) Exit() {
182+
s.logger.Println("exit")
183+
174184
if s.Finished() {
175185
return
176186
}

0 commit comments

Comments
 (0)