Skip to content

Commit

Permalink
[FIX] Wait in the main goroutine
Browse files Browse the repository at this point in the history
Wait in the main routine for the interrupt signal instead of exiting the main routine.
We are facing some crash when we try to gracefuly shoutdown the container.

From the Official Documentation:
Calling Goexit from the main goroutine terminates that goroutine without func main returning.
Since func main has not returned, the program continues execution of other goroutines.
If all other goroutines exit, the program crashes.

https://pkg.go.dev/runtime#Goexit
  • Loading branch information
ramonberrutti authored and piotrpio committed Apr 9, 2024
1 parent 128e036 commit 1f60195
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"net/url"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"

"github.com/nats-io/prometheus-nats-exporter/collector"
Expand Down Expand Up @@ -92,6 +92,10 @@ func main() {
var retryInterval int
var printVersion bool

// Setup the interrupt handler to gracefully exit.
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)

opts := exporter.GetDefaultExporterOptions()

// Parse flags
Expand Down Expand Up @@ -200,14 +204,6 @@ necessary.`)
collector.Fatalf("error starting the exporter: %v\n", err)
}

// Setup the interrupt handler to gracefully exit.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
exp.Stop()
os.Exit(0)
}()

runtime.Goexit()
<-c
exp.Stop()
}

0 comments on commit 1f60195

Please sign in to comment.