Curious about this snippet in /src/runtime/proc.go

420 views
Skip to first unread message

goodwin...@gmail.com

unread,
Jun 15, 2017, 10:23:36 AM6/15/17
to golang-nuts
Hey,

Learning golang at the moment and came across this at the end of func main() in proc.go in the golang source tree.

exit(0)
for {
var x *int32
*x = 0
}


How is the for loop ever reached and what's the purpose of the infinite loop, zeroing memory?

Thanks,

Goodwin

Aldrin Leal

unread,
Jun 15, 2017, 12:56:30 PM6/15/17
to goodwin...@gmail.com, golang-nuts
Force a panic in case exit fails?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tyler Compton

unread,
Jun 15, 2017, 1:29:23 PM6/15/17
to Aldrin Leal, goodwin...@gmail.com, golang-nuts
Why not just panic, though? And why the infinite loop, I wonder?

On Thu, Jun 15, 2017 at 9:56 AM Aldrin Leal <ald...@leal.eng.br> wrote:
Force a panic in case exit fails?
On Thu, Jun 15, 2017 at 4:54 AM, <goodwin...@gmail.com> wrote:
Hey,

Learning golang at the moment and came across this at the end of func main() in proc.go in the golang source tree.

exit(0)
for {
var x *int32
*x = 0
}


How is the for loop ever reached and what's the purpose of the infinite loop, zeroing memory?

Thanks,

Goodwin

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Tyler Compton
Software Engineering

Aldrin Leal

unread,
Jun 15, 2017, 1:31:16 PM6/15/17
to Tyler Compton, Goodwin Lawlor, golang-nuts
I don't know. 

On Jun 15, 2017 12:29, "Tyler Compton" <xav...@gmail.com> wrote:
Why not just panic, though? And why the infinite loop, I wonder?

On Thu, Jun 15, 2017 at 9:56 AM Aldrin Leal <ald...@leal.eng.br> wrote:
Force a panic in case exit fails?
On Thu, Jun 15, 2017 at 4:54 AM, <goodwin...@gmail.com> wrote:
Hey,

Learning golang at the moment and came across this at the end of func main() in proc.go in the golang source tree.

exit(0)
for {
var x *int32
*x = 0
}


How is the for loop ever reached and what's the purpose of the infinite loop, zeroing memory?

Thanks,

Goodwin

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Ian Lance Taylor

unread,
Jun 15, 2017, 1:35:02 PM6/15/17
to Tyler Compton, Aldrin Leal, goodwin...@gmail.com, golang-nuts
On Thu, Jun 15, 2017 at 10:28 AM, Tyler Compton <xav...@gmail.com> wrote:
> Why not just panic, though? And why the infinite loop, I wonder?

The runtime is a special case in many ways, and this is among the more
special parts. This loop exists to catch problems while testing new
ports. If that loop is ever reached, something has gone badly wrong:
the exit call should have caused the program to exit. We can't assume
that panic is working. We can't really assume that anything is
working. What we want to do is stop the program. Since exit failed,
it's possible that a nil dereference will succeed. If that fails too,
we still have to do something, so we just loop. We can't return
because this is the main function that started the program; there is
nothing to return to.

Ian

Tyler Compton

unread,
Jun 15, 2017, 1:49:52 PM6/15/17
to Ian Lance Taylor, Aldrin Leal, goodwin...@gmail.com, golang-nuts
Very interesting. I'm not used to looking at Go code from the perspective of an implementer of Go.

Somebody should contribute a comment about this upstream :)

Goodwin Lawlor

unread,
Jun 15, 2017, 2:25:18 PM6/15/17
to Ian Lance Taylor, Aldrin Leal, golang-nuts, Tyler Compton
Thanks for the explanation.

My random guess was that it's the compiler version of deleting your browser cache... If exit fails, blank as much memory as we can get our hands on. 😁


>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an

>> For more options, visit https://groups.google.com/d/optout.
>
> --
> Tyler Compton
> Software Engineering
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Rob Pike

unread,
Jun 15, 2017, 5:16:51 PM6/15/17
to Goodwin Lawlor, Ian Lance Taylor, Aldrin Leal, golang-nuts, Tyler Compton
Code comments can be helpful with unusual situations like this.

-rob

Thomas Bushnell, BSG

unread,
Jun 19, 2017, 1:30:42 PM6/19/17
to Rob Pike, Goodwin Lawlor, Ian Lance Taylor, Aldrin Leal, golang-nuts, Tyler Compton
Something similar at https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/abort.c;h=19882f3e3dc1ab830431506329c94dcf1d7cc252;hb=HEAD#l138 in the GNU C library. Sometimes you just need to be absolutely certain that a function doesn't return.


>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an

>> For more options, visit https://groups.google.com/d/optout.
>
> --
> Tyler Compton
> Software Engineering
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an

> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

n.s....@gmail.com

unread,
Jun 19, 2017, 1:56:20 PM6/19/17
to golang-nuts
It won't blank as much as it can get it's hands on. The default value for the pointer will be nil so it will try and assign (causing a panic() hopefully) to force an exit

Tyler Compton

unread,
Jun 21, 2017, 1:56:34 AM6/21/17
to n.s....@gmail.com, golang-nuts

On Mon, Jun 19, 2017 at 10:56 AM <n.s....@gmail.com> wrote:
It won't blank as much as it can get it's hands on. The default value for the pointer will be nil so it will try and assign (causing a panic() hopefully) to force an exit

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages