Skip to content

Commit c6808ca

Browse files
committed
Fix #837
1 parent d719317 commit c6808ca

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

executor.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type executor struct {
3636

3737
// used by the executor to receive a stop signal from the scheduler
3838
stopCh chan struct{}
39+
// ensure that stop runs before the next call to start and only runs once
40+
stopOnce *sync.Once
3941
// the timeout value when stopping
4042
stopTimeout time.Duration
4143
// used to signal that the executor has completed shutdown
@@ -88,6 +90,7 @@ func (e *executor) start() {
8890
// any other uses within the executor should create a context
8991
// using the executor context as parent.
9092
e.ctx, e.cancel = context.WithCancel(context.Background())
93+
e.stopOnce = &sync.Once{}
9194

9295
// the standardJobsWg tracks
9396
standardJobsWg := &waitGroupWithMutex{}
@@ -111,7 +114,9 @@ func (e *executor) start() {
111114
case jIn := <-e.jobsIn:
112115
select {
113116
case <-e.stopCh:
114-
e.stop(standardJobsWg, singletonJobsWg, limitModeJobsWg)
117+
e.stopOnce.Do(func() {
118+
e.stop(standardJobsWg, singletonJobsWg, limitModeJobsWg)
119+
})
115120
return
116121
default:
117122
}
@@ -131,7 +136,7 @@ func (e *executor) start() {
131136

132137
// spin off into a goroutine to unblock the executor and
133138
// allow for processing for more work
134-
go func() {
139+
go func(executorCtx context.Context) {
135140
// make sure to cancel the above context per the docs
136141
// // Canceling this context releases resources associated with it, so code should
137142
// // call cancel as soon as the operations running in this Context complete.
@@ -211,8 +216,7 @@ func (e *executor) start() {
211216
}
212217
} else {
213218
select {
214-
case <-e.stopCh:
215-
e.stop(standardJobsWg, singletonJobsWg, limitModeJobsWg)
219+
case <-executorCtx.Done():
216220
return
217221
default:
218222
}
@@ -228,9 +232,11 @@ func (e *executor) start() {
228232
}(*j)
229233
}
230234
}
231-
}()
235+
}(e.ctx)
232236
case <-e.stopCh:
233-
e.stop(standardJobsWg, singletonJobsWg, limitModeJobsWg)
237+
e.stopOnce.Do(func() {
238+
e.stop(standardJobsWg, singletonJobsWg, limitModeJobsWg)
239+
})
234240
return
235241
}
236242
}

0 commit comments

Comments
 (0)