summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhagen1778 <hagen1778@gmail.com>2017-09-19 20:28:11 +0300
committerRuss Cox <rsc@golang.org>2017-10-25 20:22:58 +0000
commitbd34e74134645b7a7109dbf0361eb1ceb1c3d1ba (patch)
tree75700ad7599801c9984071d6aba235d118a8e4b3
parent0b55d8dbfc6892433b0329b3834d50f72d90db58 (diff)
downloadgo-git-bd34e74134645b7a7109dbf0361eb1ceb1c3d1ba.tar.gz
[release-branch.go1.9] log: fix data race on log.Output
There was unprotected access to Logger.flag in log.Output which could lead to data race in cases when log.SetFlags called simultaneously. For example, "hot" switching on/off debug-mode for Logger by log.SetFlags while application still writing logs. Fixes #21935 Change-Id: I36be25f23cad44cde62ed1af28a30d276400e1b8 Reviewed-on: https://go-review.googlesource.com/64710 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-on: https://go-review.googlesource.com/70976 Run-TryBot: Russ Cox <rsc@golang.org>
-rw-r--r--src/log/log.go6
-rw-r--r--src/log/log_test.go11
2 files changed, 12 insertions, 5 deletions
diff --git a/src/log/log.go b/src/log/log.go
index 587904b11c..e8e0c96636 100644
--- a/src/log/log.go
+++ b/src/log/log.go
@@ -147,11 +147,7 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
// provided for generality, although at the moment on all pre-defined
// paths it will be 2.
func (l *Logger) Output(calldepth int, s string) error {
- // Get time early if we need it.
- var now time.Time
- if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
- now = time.Now()
- }
+ now := time.Now() // get this early.
var file string
var line int
l.mu.Lock()
diff --git a/src/log/log_test.go b/src/log/log_test.go
index 966fdf306b..adc15e7e8e 100644
--- a/src/log/log_test.go
+++ b/src/log/log_test.go
@@ -88,6 +88,17 @@ func TestOutput(t *testing.T) {
}
}
+func TestOutputRace(t *testing.T) {
+ var b bytes.Buffer
+ l := New(&b, "", 0)
+ for i := 0; i < 100; i++ {
+ go func() {
+ l.SetFlags(0)
+ }()
+ l.Output(0, "")
+ }
+}
+
func TestFlagAndPrefixSetting(t *testing.T) {
var b bytes.Buffer
l := New(&b, "Test:", LstdFlags)