diff --git a/pkg/context/context.go b/pkg/context/context.go index d1e179877..c68a4ffc3 100644 --- a/pkg/context/context.go +++ b/pkg/context/context.go @@ -24,6 +24,7 @@ package context import ( "context" + "sync" "time" "gvisor.dev/gvisor/pkg/log" @@ -162,10 +163,8 @@ type logContext struct { } // bgContext is the context returned by context.Background. -var bgContext Context = &logContext{ - Context: context.Background(), - Logger: log.Log(), -} +var bgContext Context +var bgOnce sync.Once // Background returns an empty context using the default logger. // Generally, one should use the Task as their context when available, or avoid @@ -173,7 +172,15 @@ var bgContext Context = &logContext{ // // Using a Background context for tests is fine, as long as no values are // needed from the context in the tested code paths. +// +// The global log.SetTarget() must be called before context.Background() func Background() Context { + bgOnce.Do(func() { + bgContext = &logContext{ + Context: context.Background(), + Logger: log.Log(), + } + }) return bgContext } diff --git a/pkg/log/log.go b/pkg/log/log.go index 478782a9f..7f8051e57 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -260,6 +260,8 @@ func Log() *BasicLogger { // // This is not thread safe and shouldn't be called concurrently with any // logging calls. +// +// SetTarget should be called before any instances of log.Log() to avoid race conditions func SetTarget(target Emitter) { logMu.Lock() defer logMu.Unlock()