Skip to content

runtime: Stack output is inconsistent with NumGoroutine output #11706

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

Closed
snakeru opened this issue Jul 14, 2015 · 5 comments
Closed

runtime: Stack output is inconsistent with NumGoroutine output #11706

snakeru opened this issue Jul 14, 2015 · 5 comments
Milestone

Comments

@snakeru
Copy link

snakeru commented Jul 14, 2015

While trying to detect goroutines leaks from my program I noticed that runtime.NumGoroutine reports much higher number of goroutines (5) than reported by runtime.Stack (1).
After some poking around I figured that there are, indeed, some system routines running in the background.

Here is the repro: http://play.golang.org/p/WowC44lsh9

A few notes:

  1. This is only reproducible in go 1.5. Go 1.4 works correctly
  2. The behavior of runtime.Stack() depends on the GOTRACEBACK environment variable. If it is set to "2" then runtime.Stack() outputs all 5 goroutines.
    So this looks like runtime.NumGoroutine() should also depend on this environment variable to stay consistent (outputting only user-created goroutines is a feature and is very welcome).
@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Jul 14, 2015
@ianlancetaylor
Copy link
Contributor

Not sure whether we should change the code or simply document it.

@nightlyone
Copy link
Contributor

Documenting would suffice for Go 1.5{.x}, but for Go 1.6 it would be nice to be able to get both, number of goroutines started by the system and number of goroutines started by myself.

@rsc
Copy link
Contributor

rsc commented Aug 21, 2015

Will think more but it seems to me right now that runtime.Stack should just show system goroutines when you ask for all. We omit them from panic crashes to avoid cluttering crash output in simple programs, but if you're asking for 'all goroutines' it seems like probably you should get them all.

Either that or it should never show them, and whichever it is, NumGoroutines should match.

@mikioh mikioh changed the title runtime.Stack output is inconsistent with runtime.NumGoroutine output runtime: Stack output is inconsistent with NumGoroutine output Aug 22, 2015
@gopherbot
Copy link
Contributor

CL https://golang.org/cl/18343 mentions this issue.

@rsc rsc closed this as completed in c5bafc8 Jan 8, 2016
@gopherbot
Copy link
Contributor

CL https://golang.org/cl/18432 mentions this issue.

gopherbot pushed a commit that referenced this issue Jan 13, 2016
…utines

[Repeat of CL 18343 with build fixes.]

Before, NumGoroutine counted system goroutines and Stack (usually) didn't show them,
which was inconsistent and confusing.

To resolve which way they should be consistent, it seems like

	package main
	import "runtime"
	func main() { println(runtime.NumGoroutine()) }

should print 1 regardless of internal runtime details. Make it so.

Fixes #11706.

Change-Id: If26749fec06aa0ff84311f7941b88d140552e81d
Reviewed-on: https://go-review.googlesource.com/18432
Reviewed-by: Austin Clements <austin@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
@golang golang locked and limited conversation to collaborators Jan 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants