Program fails to exit when main() exits

114 views
Skip to first unread message

Dmitry Vyukov

unread,
Sep 25, 2011, 1:36:12 AM9/25/11
to golan...@googlegroups.com
The following program does not terminate:

package main
import "runtime"
import "time"

func main() {
        go func() {
                for {
                        time.Sleep(1e9)
                }
        }()
        runtime.Goexit()
}

Bug or feature? I expected it to exit.

Ian Lance Taylor

unread,
Sep 25, 2011, 2:00:56 AM9/25/11
to golan...@googlegroups.com
Dmitry Vyukov <dvy...@google.com> writes:

Returning from main.main is equivalent to calling os.Exit--this is in
the spec. Calling runtime.Goexit from the main goroutine is not
equivalent to os.Exit--the spec doesn't say anything about this. So
this is a feature.

Ian

Russ Cox

unread,
Sep 26, 2011, 11:38:04 AM9/26/11
to Ian Lance Taylor, golan...@googlegroups.com
Maybe we should remove runtime.Goexit.
It was necessary mainly when we were using
goroutines and channels to simulate panic.

Russ

Ian Lance Taylor

unread,
Sep 26, 2011, 11:55:44 AM9/26/11
to r...@golang.org, golan...@googlegroups.com
Russ Cox <r...@golang.org> writes:

I think runtime.Goexit definitely has its uses and I think the cost of
providing it is low. I think more people are surprised by the fact that
returning from main.main terminates their program even if there are
running goroutines than are surprised by the fact that calling
runtime.Goexit from a callee of main.main does not terminate their
program.

Ian

Andrew Gerrand

unread,
Sep 28, 2011, 6:39:12 PM9/28/11
to golan...@googlegroups.com, r...@golang.org
I would be more concerned with the effects it might have on the way people write Go code.

For instance, a common idiom is to use an empty select (select{}) at the end of a main function to block indefinitely while other goroutines run. One might instead  use runtime.Goexit to tear down the main goroutine without exiting the program.

The select trick is an idiom of the language, while Goexit is a feature of the runtime library. It seems preferable to use the language where possible (select{} in this case, and panic/recover in the testing framework where we currently use runtime.Goexit), without having to instruct the runtime directly. 

Andrew

Russ Cox

unread,
Sep 28, 2011, 7:19:55 PM9/28/11
to golan...@googlegroups.com
On Wed, Sep 28, 2011 at 15:39, Andrew Gerrand <a...@golang.org> wrote:
> One might instead  use runtime.Goexit to tear down the main goroutine
> without exiting the program.

Not after I fix Goexit.

Reply all
Reply to author
Forward
0 new messages