forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix goroutine leak: make flushDaemon stoppable (#293)
* Make flushDaemon stoppable and testable Refactor flushDaemon function into a seperate struct. Instead of starting the daemon in the init function, it is now started during createFile, where it is only used. Add function to stop daemon. This way, users can gracefully shut down klog and prevent goroutine leakage. Test daemon flushing. * fixup! Make flushDaemon stoppable and testable * fixup! Make flushDaemon stoppable and testable * fixup! Make flushDaemon stoppable and testable * fixup! Make flushDaemon stoppable and testable Co-authored-by: katexochen <katexochen@users.noreply.github.com>
- Loading branch information
1 parent
e0de690
commit 7ea6d6a
Showing
7 changed files
with
260 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
|
||
"go.uber.org/goleak" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func main() { | ||
klog.InitFlags(nil) | ||
|
||
// By default klog writes to stderr. Setting logtostderr to false makes klog | ||
// write to a log file. | ||
flag.Set("logtostderr", "false") | ||
flag.Set("log_file", "myfile.log") | ||
flag.Parse() | ||
|
||
// Info writes the first log message. When the first log file is created, | ||
// a flushDaemon is started to frequently flush bytes to the file. | ||
klog.Info("nice to meet you") | ||
|
||
// klog won't ever stop this flushDaemon. To exit without leaking a goroutine, | ||
// the daemon can be stopped manually. | ||
klog.StopFlushDaemon() | ||
|
||
// After you stopped the flushDaemon, you can still manually flush. | ||
klog.Info("bye") | ||
klog.Flush() | ||
} | ||
|
||
func TestLeakingFlushDaemon(t *testing.T) { | ||
// goleak detects leaking goroutines. | ||
defer goleak.VerifyNone(t) | ||
|
||
// Without calling StopFlushDaemon in main, this test will fail. | ||
main() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= | ||
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE= | ||
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE= | ||
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 h1:HNSDgDCrr/6Ly3WEGKZftiE7IY19Vz2GdbOCyI4qqhc= | ||
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.