Random Number Genaration - Golang -- Error/Bug

308 views
Skip to first unread message

Mukund 8kmiles

unread,
Apr 4, 2017, 4:37:59 AM4/4/17
to golang-nuts
Hi,

It is a basic index out of range but inside math.Rand() and not in objects that I create. Could that be a bug ? I have given my source, below. Any help or pointers is appreciated

panic: runtime error: index out of range

goroutine 32 [running]:
panic(0x7d5780, 0xc420010140)
/opt/go/src/runtime/panic.go:500 +0x1a1
math/rand.(*rngSource).Int63(0xc420024a00, 0x694976b867c8d8d7)
/opt/go/src/math/rand/rng.go:243 +0x85
math/rand.(*Rand).Int63(0xc4200ccac0, 0x694976b867c8d8d7)
/opt/go/src/math/rand/rand.go:64 +0x33
math/rand.(*Rand).Int31(0xc4200ccac0, 0x7ffe694976b8)
/opt/go/src/math/rand/rand.go:70 +0x2b
math/rand.(*Rand).Int31n(0xc4200ccac0, 0x64, 0x0)
/opt/go/src/math/rand/rand.go:105 +0x5c
math/rand.(*Rand).Intn(0xc4200ccac0, 0x64, 0x0)
/opt/go/src/math/rand/rand.go:119 +0x45
flowlogs.pickENI(0xc4200110c0, 0xc)
/home/mukund/go/src/flowlogs/vpcflowlogs.go:351 +0x39
flowlogs.GenerateVPCLogData(0xa, 0x0, 0x0, 0x0)
/home/mukund/go/src/flowlogs/vpcflowlogs.go:126 +0x15d
main.ingestRecords(0xc420210000, 0xc420394480, 0xc420394495, 0x1, 0x7ffee087b128, 0x3, 0xa, 0xa)
/home/mukund/go/src/flmain/kinesisproducer.go:288 +0xd4
created by main.main
/home/mukund/go/src/flmain/kinesisproducer.go:108 +0x6ef
exit status 2

*****************************************************************

source:

*****************************************************************



Ian Davis

unread,
Apr 4, 2017, 4:44:14 AM4/4/17
to golan...@googlegroups.com
What version of Go are you using. I ran your code a few times and could not reproduce on my version which is a few commits off of tip.
--
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.

Dave Cheney

unread,
Apr 4, 2017, 5:00:20 AM4/4/17
to golang-nuts
Have you built your program with the -race flag and confirmed there are no data races in your program?

peterGo

unread,
Apr 4, 2017, 11:19:51 AM4/4/17
to golang-nuts, muk...@8kmiles.com
Mukund,

If you suspect a bug in the compiler or the standard library, compile annd run the program using the latest version or, even better, tip so that you include all known bug fixes.

I couldn't reproduce your error at Go tip:

$ go version
go version devel +bebfd4b Tue Apr 4 06:26:11 2017 +0000 linux/amd64
$ go run mukund.go
$ go run -race mukund.go
$

Using the line number, math/rand/rng.go:243, I backtracked to your likely version. I couldn't reproduce your error at go1.7:

$ go version
go version go1.7.5 linux/amd64
$ go run mukund.go
$ go run -race mukund.go
$

I couldn't reproduce your error going as far back as go1.4:

$ go version
go version go1.4-bootstrap-20161024 linux/amd64
$ go run mukund.go
$ go run -race mukund.go
$

mukund.go: https://play.golang.org/p/SGkJyxYPiR

To help you further, we need you to provide us with a small program which we can use to reproduce your error. Also, provide us with the output from the go version and go env commands.

Peter

Dave Cheney

unread,
Apr 4, 2017, 3:11:27 PM4/4/17
to golang-nuts
I think the sample program you provided is not the one which is causing the panic. For example there is no mention of Kenisis provider in the sample code you provided, but it is part of the stack trace you showed.

1. Can you reproduce the crash with the sample code you provided?
2. Can you provide the source code to the program that produced the crash you reported?

Uli Kunitz

unread,
Apr 4, 2017, 3:21:52 PM4/4/17
to golang-nuts, muk...@8kmiles.com
Hi Mukund,

Please recognize that the Source object returned by rand.NewSource is not safe for concurrent use by multiple goroutines. Look at the documentation of NewSource in package math/rand. You are defining r as a global variable and probably call pickENI() from multiple goroutines, which is running into an issue. The example program will not generate a problem, because it doesn't call pickENI() in multiple goroutines.

The easy way out is to use rand.Int63 instead of r.Int63. It uses a source that is protected against use in multiple goroutines. You might want to use Seed to prevent the generation of the same ENI in each run. Alternatively you could write a lock-protected source yourself, it shouldn't be too difficult.

Uli

Mukund 8kmiles

unread,
Apr 5, 2017, 7:18:11 AM4/5/17
to Uli Kunitz, golang-nuts
Thanks a lot Uli,  &Peter,

@Uli
The go routine safe rand.Int63 resolved the problem.


Regards
Mukund

Reply all
Reply to author
Forward
0 new messages