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

runtime: one strange result for printing different goroutine IDs by runtime.stack method #36079

Closed
TodLiuMeng opened this issue Dec 11, 2019 · 3 comments

Comments

@TodLiuMeng
Copy link

@TodLiuMeng TodLiuMeng commented Dec 11, 2019

What version of Go are you using (go version)?

go version go1.13 windows/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

func GetGoroutineID() uint64 {
	b := make([]byte, 1024)
	runtime.Stack(b, false)
	println(string(b))
	b = bytes.TrimPrefix(b, []byte("goroutine "))
	b = b[:bytes.IndexByte(b, ' ')]
	n, _ := strconv.ParseUint(string(b), 10, 64)
	return n
}

func main() {
	go func() {
		println(1, runTime.GetGoroutineID())
	}()
	time.Sleep(1 * time.Second)
	go println(2, runTime.GetGoroutineID())
	time.Sleep(1 * time.Second)
	println(3, runTime.GetGoroutineID())
	time.Sleep(1 * time.Second)
}

What did you expect to see?

1,goroutineID1
2,goroutineID2
3,1

What did you see instead?

1,goroutineID1
2,1
3,1

More output details:

goroutine 5 [running]:
SimpleGoRepo/src/runTime.GetGoroutineID(0x0)
	D:/GOPATH/src/SimpleGoRepo/src/runTime/routine_id.go:36 +0x85
main.main.func1()
	D:/GOPATH/src/SimpleGoRepo/main.go:186 +0x29
created by main.main
	D:/GOPATH/src/SimpleGoRepo/main.go:185 +0x40
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
1 5

goroutine 1 [running]:
SimpleGoRepo/src/runTime.GetGoroutineID(0x3b9aca00)
	D:/GOPATH/src/SimpleGoRepo/src/runTime/routine_id.go:36 +0x85
main.main()
	D:/GOPATH/src/SimpleGoRepo/main.go:189 +0x52
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
2 1
goroutine 1 [running]:
SimpleGoRepo/src/runTime.GetGoroutineID(0x3b9aca00)
	D:/GOPATH/src/SimpleGoRepo/src/runTime/routine_id.go:36 +0x85
main.main()
	D:/GOPATH/src/SimpleGoRepo/main.go:191 +0x8e
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
3 1
@biter777

This comment has been minimized.

Copy link

@biter777 biter777 commented Dec 11, 2019

https://play.golang.org/p/iPZaigoakwd its the same:

goroutine 5 [running]:
main.GetGoroutineID(0x0, 0x0)
/tmp/sandbox409247425/prog.go:12 +0x80
main.main.func1()
/tmp/sandbox409247425/prog.go:22 +0x20
created by main.main
/tmp/sandbox409247425/prog.go:21 +0x40

1 5
goroutine 1 [running]:
main.GetGoroutineID(0x3b9aca00, 0x0)
/tmp/sandbox409247425/prog.go:12 +0x80
main.main()
/tmp/sandbox409247425/prog.go:25 +0x80

2 1
goroutine 1 [running]:
main.GetGoroutineID(0x3b9aca00, 0x0)
/tmp/sandbox409247425/prog.go:12 +0x80
main.main()
/tmp/sandbox409247425/prog.go:27 +0x100

3 1

@seankhliao

This comment has been minimized.

Copy link
Contributor

@seankhliao seankhliao commented Dec 11, 2019

the arguments to the goroutine are evaluated before being passed to the function/goroutine,

in this case GetGoroutineID is being called from main, before the result is passed to println which is run in the goroutine

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Dec 11, 2019

Working as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.