fix(server/v2): respect context cancellation in start command (#22346)

This commit is contained in:
Mark Rushakoff 2024-10-23 16:11:06 -04:00 committed by GitHub
parent 864ae34378
commit bf127028b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -120,9 +120,15 @@ func createStartCommand[T transaction.Tx](
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigCh
cancelFn()
cmd.Printf("caught %s signal\n", sig.String())
select {
case sig := <-sigCh:
cancelFn()
cmd.Printf("caught %s signal\n", sig.String())
case <-ctx.Done():
// If the root context is canceled (which is likely to happen in tests involving cobra commands),
// don't block waiting for the OS signal before stopping the server.
cancelFn()
}
if err := server.Stop(ctx); err != nil {
cmd.PrintErrln("failed to stop servers:", err)