From bf127028b22c15e7f5903e3e9f2d30d943fc2867 Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Wed, 23 Oct 2024 16:11:06 -0400 Subject: [PATCH] fix(server/v2): respect context cancellation in start command (#22346) --- server/v2/commands.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/server/v2/commands.go b/server/v2/commands.go index d5c202ade9f7..c64fbc1f0de4 100644 --- a/server/v2/commands.go +++ b/server/v2/commands.go @@ -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)