Skip to content

Commit fe4afca

Browse files
authored
refactor: handle context done as sigint signal (#2019)
Signed-off-by: Artur Troian <[email protected]> Co-authored-by: Artur Troian <[email protected]>
1 parent 141d74d commit fe4afca

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

cmd/akash/cmd/testnetify/testnetify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ you want to test the upgrade handler itself.
185185
getCtx := func(svrCtx *sdksrv.Context, block bool) (*errgroup.Group, context.Context) {
186186
g, ctx := errgroup.WithContext(ctx)
187187
// listen for quit signals so the calling parent process can gracefully exit
188-
server.ListenForQuitSignals(g, block, cancelFn, svrCtx.Logger)
188+
server.ListenForQuitSignals(ctx, cancelFn, g, block, svrCtx.Logger)
189189
return g, ctx
190190
}
191191

@@ -238,7 +238,7 @@ you want to test the upgrade handler itself.
238238
case <-ticker.C:
239239
status, err := cctx.Client.Status(ctx)
240240
if err == nil && status != nil {
241-
if status.SyncInfo.LatestBlockHeight > h+1 {
241+
if status.SyncInfo.LatestBlockHeight > h+2 {
242242
return
243243
}
244244
}

cmd/akash/cmd/testnetify/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func getCtx(sctx *sdksrv.Context, block bool) (*errgroup.Group, context.Context)
5656
ctx, cancelFn := context.WithCancel(context.Background())
5757
g, ctx := errgroup.WithContext(ctx)
5858
// listen for quit signals so the calling parent process can gracefully exit
59-
server.ListenForQuitSignals(g, block, cancelFn, sctx.Logger)
59+
server.ListenForQuitSignals(ctx, cancelFn, g, block, sctx.Logger)
6060

6161
return g, ctx
6262
}

util/server/utils.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ import (
1616
//
1717
// Note, the blocking behavior of this depends on the block argument.
1818
// The caller must ensure the corresponding context derived from the cancelFn is used correctly.
19-
func ListenForQuitSignals(g *errgroup.Group, block bool, cancelFn context.CancelFunc, logger log.Logger) {
19+
func ListenForQuitSignals(ctx context.Context, cancelFn context.CancelFunc, g *errgroup.Group, block bool, logger log.Logger) {
2020
sigCh := make(chan os.Signal, 1)
2121
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
2222

2323
f := func() {
24-
sig := <-sigCh
25-
cancelFn()
24+
select {
25+
case sig := <-sigCh:
26+
logger.Info("caught signal", "signal", sig.String())
27+
case <-ctx.Done():
28+
logger.Info("context canceled")
29+
}
2630

27-
logger.Info("caught signal", "signal", sig.String())
31+
cancelFn()
2832
}
2933

3034
if block {

0 commit comments

Comments
 (0)