Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Wait in the main goroutine #285

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

ramonberrutti
Copy link
Contributor

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

Copy link
Contributor

@piotrpio piotrpio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just one comment.

main.go Outdated
@@ -92,6 +91,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, os.Interrupt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should handle more than just SIGINT here. Instead of os package (which does not have alias for SIGTERM), you could use signals from syscall:

signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)

I know that the old code just handled os.Interrupt but it would be nice to do it properly this time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was curious whether to add SIGTERM, as it isn’t supported in Windows.
But I checked nats-server.
We can capture both signals.

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
Copy link
Contributor

@piotrpio piotrpio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@piotrpio piotrpio merged commit 1f60195 into nats-io:main Apr 9, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants