From d33af527d374027209523da5cac294a0e073586b Mon Sep 17 00:00:00 2001 From: Phi Date: Tue, 3 Oct 2023 10:44:32 +0100 Subject: [PATCH 1/3] Listen for interrupt signals - Added a goroutine to listen for interrupt signals, which will cancel the current context when an interrupt signal is received. This allows for graceful shutdown of ongoing operations. --- cmd/lotus-worker/main.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/cmd/lotus-worker/main.go b/cmd/lotus-worker/main.go index 873dada4732..2eefdfe6236 100644 --- a/cmd/lotus-worker/main.go +++ b/cmd/lotus-worker/main.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "os" + "os/signal" "path/filepath" "reflect" "strings" @@ -348,6 +349,18 @@ var runCmd = &cli.Command{ // Connect to storage-miner ctx := lcli.ReqContext(cctx) + // Create a new context with cancel function + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + // Listen for interrupt signals + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + cancel() + }() + var nodeApi api.StorageMiner var closer func() for { @@ -359,14 +372,15 @@ var runCmd = &cli.Command{ } } fmt.Printf("\r\x1b[0KConnecting to miner API... (%s)", err) - time.Sleep(time.Second) - continue + select { + case <-ctx.Done(): + return xerrors.New("Interrupted by user") + case <-time.After(time.Second): + } } - defer closer() - ctx, cancel := context.WithCancel(ctx) + ctx, cancel = context.WithCancel(ctx) defer cancel() - // Register all metric views if err := view.Register( metrics.DefaultViews..., From f759f4d600bfb92551aa133474272b51363ad00a Mon Sep 17 00:00:00 2001 From: Phi Date: Tue, 3 Oct 2023 12:22:03 +0100 Subject: [PATCH 2/3] Remove redundant ctx cancellation Remove redundant context cancellation in lotus worker --- cmd/lotus-worker/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/lotus-worker/main.go b/cmd/lotus-worker/main.go index 2eefdfe6236..d9ffaf999da 100644 --- a/cmd/lotus-worker/main.go +++ b/cmd/lotus-worker/main.go @@ -379,7 +379,6 @@ var runCmd = &cli.Command{ } } defer closer() - ctx, cancel = context.WithCancel(ctx) defer cancel() // Register all metric views if err := view.Register( From 7385e0049b88aeae7eb8f3497b2913766efe7f6d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 3 Nov 2023 06:28:10 -0700 Subject: [PATCH 3/3] remove extra cancel --- cmd/lotus-worker/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/lotus-worker/main.go b/cmd/lotus-worker/main.go index d9ffaf999da..257dac800c2 100644 --- a/cmd/lotus-worker/main.go +++ b/cmd/lotus-worker/main.go @@ -379,7 +379,6 @@ var runCmd = &cli.Command{ } } defer closer() - defer cancel() // Register all metric views if err := view.Register( metrics.DefaultViews...,