From c764d65454a9aea2fca0ac9def18162876dd03fb Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Fri, 18 Mar 2022 23:05:29 +0800 Subject: [PATCH] fix: starter parent ctx (#1895) * fix: starter parent ctx * fix stop ctx --- app.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/app.go b/app.go index ae93ab1af..7a1ed4463 100644 --- a/app.go +++ b/app.go @@ -84,27 +84,25 @@ func (a *App) Run() error { if err != nil { return err } - ctx := NewContext(a.ctx, a) - eg, ctx := errgroup.WithContext(ctx) + eg, ctx := errgroup.WithContext(NewContext(a.ctx, a)) wg := sync.WaitGroup{} for _, srv := range a.opts.servers { srv := srv eg.Go(func() error { <-ctx.Done() // wait for stop signal - sctx, cancel := context.WithTimeout(NewContext(context.Background(), a), a.opts.stopTimeout) + stopCtx, cancel := context.WithTimeout(NewContext(a.opts.ctx, a), a.opts.stopTimeout) defer cancel() - return srv.Stop(sctx) + return srv.Stop(stopCtx) }) wg.Add(1) eg.Go(func() error { wg.Done() - tctx := NewContext(context.Background(), a) - return srv.Start(tctx) + return srv.Start(NewContext(a.opts.ctx, a)) }) } wg.Wait() if a.opts.registrar != nil { - rctx, rcancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout) + rctx, rcancel := context.WithTimeout(ctx, a.opts.registrarTimeout) defer rcancel() if err := a.opts.registrar.Register(rctx, instance); err != nil { return err @@ -121,8 +119,7 @@ func (a *App) Run() error { case <-ctx.Done(): return ctx.Err() case <-c: - err := a.Stop() - if err != nil { + if err := a.Stop(); err != nil { a.opts.logger.Errorf("failed to stop app: %v", err) return err } @@ -141,7 +138,7 @@ func (a *App) Stop() error { instance := a.instance a.lk.Unlock() if a.opts.registrar != nil && instance != nil { - ctx, cancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout) + ctx, cancel := context.WithTimeout(NewContext(a.ctx, a), a.opts.registrarTimeout) defer cancel() if err := a.opts.registrar.Deregister(ctx, instance); err != nil { return err