@@ -36,6 +36,8 @@ type executor struct {
36
36
37
37
// used by the executor to receive a stop signal from the scheduler
38
38
stopCh chan struct {}
39
+ // ensure that stop runs before the next call to start and only runs once
40
+ stopOnce * sync.Once
39
41
// the timeout value when stopping
40
42
stopTimeout time.Duration
41
43
// used to signal that the executor has completed shutdown
@@ -88,6 +90,7 @@ func (e *executor) start() {
88
90
// any other uses within the executor should create a context
89
91
// using the executor context as parent.
90
92
e .ctx , e .cancel = context .WithCancel (context .Background ())
93
+ e .stopOnce = & sync.Once {}
91
94
92
95
// the standardJobsWg tracks
93
96
standardJobsWg := & waitGroupWithMutex {}
@@ -111,7 +114,9 @@ func (e *executor) start() {
111
114
case jIn := <- e .jobsIn :
112
115
select {
113
116
case <- e .stopCh :
114
- e .stop (standardJobsWg , singletonJobsWg , limitModeJobsWg )
117
+ e .stopOnce .Do (func () {
118
+ e .stop (standardJobsWg , singletonJobsWg , limitModeJobsWg )
119
+ })
115
120
return
116
121
default :
117
122
}
@@ -131,7 +136,7 @@ func (e *executor) start() {
131
136
132
137
// spin off into a goroutine to unblock the executor and
133
138
// allow for processing for more work
134
- go func () {
139
+ go func (executorCtx context. Context ) {
135
140
// make sure to cancel the above context per the docs
136
141
// // Canceling this context releases resources associated with it, so code should
137
142
// // call cancel as soon as the operations running in this Context complete.
@@ -211,8 +216,7 @@ func (e *executor) start() {
211
216
}
212
217
} else {
213
218
select {
214
- case <- e .stopCh :
215
- e .stop (standardJobsWg , singletonJobsWg , limitModeJobsWg )
219
+ case <- executorCtx .Done ():
216
220
return
217
221
default :
218
222
}
@@ -228,9 +232,11 @@ func (e *executor) start() {
228
232
}(* j )
229
233
}
230
234
}
231
- }()
235
+ }(e . ctx )
232
236
case <- e .stopCh :
233
- e .stop (standardJobsWg , singletonJobsWg , limitModeJobsWg )
237
+ e .stopOnce .Do (func () {
238
+ e .stop (standardJobsWg , singletonJobsWg , limitModeJobsWg )
239
+ })
234
240
return
235
241
}
236
242
}
0 commit comments