Skip to content

Commit

Permalink
Exit program properly when Run is done (#45)
Browse files Browse the repository at this point in the history
Conduit has been running the node indexer, and when `Run` exited on an
error the indexer just hung indefinitely, and needed to be terminated.
This happened because we don't trigger the exiting condition when `Run`
exits with an error.

This PR makes sure the context is cancelled when `Run` exits, and that
`main.go` exits when we either get a signal, or the context has been
cancelled.
  • Loading branch information
vegarsti committed Jun 30, 2024
1 parent 0a34621 commit 627ce96
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {
stdlog.Fatal(err)
}

ctx, cancelFn := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(context.Background())

// Get stored progress unless config indicates we should start from 0
var startBlockNumber int64
Expand Down Expand Up @@ -99,6 +99,7 @@ func main() {
defer wg.Done()
err := ingester.Run(ctx, startBlockNumber, maxCount)
logger.Info("Ingester finished", "err", err)
cancel()
}()

defer ingester.Close()
Expand All @@ -108,10 +109,14 @@ func main() {
quit := make(chan os.Signal, 1)
// handle Interrupt (ctrl-c) Term, used by `kill` et al, HUP which is commonly used to reload configs
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
s := <-quit
logger.Warn("Caught UNIX signal", "signal", s)
cancelFn()
select {
case <-ctx.Done():
logger.Warn("Context done")
case s := <-quit:
logger.Warn("Caught UNIX signal", "signal", s)
cancel()
}

// wait for all goroutines to finish
// wait for Run to finish
wg.Wait()
}

0 comments on commit 627ce96

Please sign in to comment.