goroutine-leak | an example for debuging golang goroutine leak | Code Inspection library
kandi X-RAY | goroutine-leak Summary
kandi X-RAY | goroutine-leak Summary
This repo is for debugging goroutine leak.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- GetContainerLogsInfo returns container logs
- Main entry point
- GetContainerLogByID returns container log by ID
- getMachineID returns the machine ID
- debugHttp shows the HTTP server
- timestampSubInterval converts timestamp to a string
- GetContainers gets a list of containers
- getUnixTimestamp returns the Unix timestamp
goroutine-leak Key Features
goroutine-leak Examples and Code Snippets
Community Discussions
Trending Discussions on goroutine-leak
QUESTION
I'm reading this blog, https://medium.com/golangspec/goroutine-leak-400063aef468, and have adapted the following example illustrating a goroutine leak due to receiving from a nil channel:
...ANSWER
Answered 2020-Mar-14 at 17:43When your app starts, there is already one goroutine that runs the main()
function.
If you don't initialize the ch
channel, it will remain its zero value which is nil
for channels. Spec: Receive operator:
Receiving from a
nil
channel blocks forever.
For details, see How does a non initialized channel behave?
So if the channel is nil
, the launched goroutine will never end. So you will have 2 goroutines: the main
goroutine and the one you launched.
If you initialize the channel with 1 buffer and send one value on it, then you will also have 2 goroutines for a "short period of time", but the launched goroutine can receive a value from it and then end immediately. So there will be a single goroutine remaining, the main
goroutine.
QUESTION
Following this post about go-routines leaking, https://www.ardanlabs.com/blog/2018/11/goroutine-leaks-the-forgotten-sender.html, i tried to solve my leaking code. But adding a buffer to the channel it did not do the trick.
My code
...ANSWER
Answered 2019-Oct-28 at 21:46Here, in the Go Playground, is your original code with a few slight modifications:
- the delays are reduced, except for
time.Sleep(5)
which becomestime.Sleep(time.Second)
; - a
return
is removed because it becomes unnecessary; - a
fmt.Println
is commented out, because with both thereturn
and the uncommentedfmt.Println
,go vet
complains about the unreachablefmt.Println
; - the channel stored in
errChang
is changed to unbuffered.
When run, its output is:
QUESTION
I'm writing some golang concurrency codes with goroutines and channels, suspecting that my code may cause goroutine leaks. My situation is similar to the following code, or you can open this go playground link.
...ANSWER
Answered 2019-May-28 at 20:07The problem with your code is twofold.
First, there is, theoretically, a goroutine leak since any attempt to send a value to a channel with zero capacity (an unbuffered channel or a filled up buffered channel) blocks the sending goroutine until a receive operation is done on that channel.
So, yes, by definition of how channels work, all your three goroutines will be blocked in the numChan <- num
statement.
Second, since some revision of Go, an unhandled panic
by default dumps only the stack trace of the panicking goroutine.
If you wish to dump the stacks of all the active goroutines, you'd have to tweak the runtime — from the documentation of the package runtime
:
The
GOTRACEBACK
variable controls the amount of output generated when a Go program fails due to an unrecovered panic or an unexpected runtime condition. By default, a failure prints a stack trace for the current goroutine, eliding functions internal to the run-time system, and then exits with exit code 2. The failure prints stack traces for all goroutines if there is no current goroutine or the failure is internal to the run-time.GOTRACEBACK=none
omits the goroutine stack traces entirely.GOTRACEBACK=single
(the default) behaves as described above.GOTRACEBACK=all
adds stack traces for all user-created goroutines.GOTRACEBACK=system
is like “all” but adds stack frames for run-time functions and shows goroutines created internally by the run-time.GOTRACEBACK=crash
is like “system” but crashes in an operating system-specific manner instead of exiting. For example, on Unix systems, the crash raisesSIGABRT
to trigger a core dump. For historical reasons, theGOTRACEBACK
settings 0, 1, and 2 are synonyms for none, all, and system, respectively.The runtime/debug
package'sSetTraceback
function allows increasing the amount of output at run time, but it cannot reduce the amount below that specified by the environment variable. See https://golang.org/pkg/runtime/debug/#SetTraceback.
Also note that you must not ever use timers for (simulating) synchronization: in a toy example this might work but in real life nothing prevents your three goroutines from not having a chance to be run during the time span your main goroutine spent in the call to time.Sleep
— so the outcome might be that any number of spawned goroutines had run: from 0 to 3.
Add there the fact that when main
exits the runtime merely kills all the outstanding active goroutines, and the result of the test might be surprising at best.
Hence a proper solution would be to
- Merely print the stacks where needed,
- Make sure you synchronize the sends by the matching receives:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install goroutine-leak
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page