Skip to content

Commit 9ec60a0

Browse files
committed
Diff between signal and run ctx
Signed-off-by: Bryan Frimin <bryan@probo.com>
1 parent 7fe670c commit 9ec60a0

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

‎unit/unit.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (u *Unit) RunContext(parentCtx context.Context) error {
161161
)
162162
logger := u.logger.Named("unit")
163163

164-
ctx, cancel := context.WithCancelCause(parentCtx)
164+
runCtx, cancel := context.WithCancelCause(parentCtx)
165165
defer cancel(context.Canceled)
166166

167167
wg := sync.WaitGroup{}
@@ -199,35 +199,39 @@ func (u *Unit) RunContext(parentCtx context.Context) error {
199199

200200
select {
201201
case registry = <-metricsInitialized:
202-
case <-ctx.Done():
203-
return context.Cause(ctx)
202+
case <-runCtx.Done():
203+
return context.Cause(runCtx)
204204
}
205205

206206
select {
207207
case traceProvider = <-tracingInitialized:
208-
case <-ctx.Done():
209-
return context.Cause(ctx)
208+
case <-runCtx.Done():
209+
return context.Cause(runCtx)
210210
}
211211

212212
wg.Go(
213213
func() {
214214

215-
if err := u.main.Run(ctx, u.logger, registry, traceProvider); err != nil {
215+
if err := u.main.Run(runCtx, u.logger, registry, traceProvider); err != nil {
216216
cancel(err)
217217
}
218218
},
219219
)
220220

221-
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
221+
signalCtx, stop := signal.NotifyContext(runCtx, os.Interrupt, syscall.SIGTERM)
222222
defer stop()
223223

224-
<-ctx.Done()
224+
<-signalCtx.Done()
225225

226226
stopMetricsServer()
227227
stopTracingExporter()
228228

229229
wg.Wait()
230230

231+
if cause := context.Cause(runCtx); cause != nil {
232+
return cause
233+
}
234+
231235
return context.Canceled
232236
}
233237

0 commit comments

Comments
 (0)