From b1228b51ef6360f1a0410a08d4d50bbaee1659b6 Mon Sep 17 00:00:00 2001 From: Phi-rjan Date: Fri, 3 Nov 2023 14:39:28 +0100 Subject: [PATCH] fix: worker: listen for interrupt signals in GetStorageMinerAPI loop (#11309) - 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, 18 insertions(+), 6 deletions(-) diff --git a/cmd/lotus-worker/main.go b/cmd/lotus-worker/main.go index 873dada4732..257dac800c2 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,13 @@ 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) - defer cancel() - // Register all metric views if err := view.Register( metrics.DefaultViews...,