Why not clean exit when there are no more goroutines?

139 views
Skip to first unread message

twp...@gmail.com

unread,
Oct 29, 2025, 11:01:18 AMOct 29
to golang-nuts
tl;dr Why does Go choose not to exit cleanly when there are no more goroutines to run?

Background:

As part of a Go pub quiz, I asked the question:

  What always happens when this code runs?

Specifically, the code calls runtime.Goexit in the main goroutine. At termination, the program prints:

  fatal error: no goroutines (main called runtime.Goexit) - deadlock!

Why is this considered a fatal error? Obviously there are no more goroutines to run, but why not exit cleanly in this case?

Obviously there's a very good reason for this behavior. I just can't work out what it is. 

Any insights or ideas?

Regards,
Tom

Jan Mercl

unread,
Oct 29, 2025, 11:12:16 AMOct 29
to twp...@gmail.com, golang-nuts
If the runtime cleanly exits on a deadlock then we no longer can
detect unexpected deadlocks. Neither can we say if all "goroutines
terminated" is expected or not. Hence "clean exit" iff main() returns.
(Let's ignore os.Exit(rc) for a second.)

twp...@gmail.com

unread,
Oct 30, 2025, 10:54:33 AM (14 days ago) Oct 30
to golang-nuts
Thank you Jan, that makes sense.
Reply all
Reply to author
Forward
0 new messages