From ecc2f43e0e164e86c69a41503079e5a6dce1d769 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Thu, 17 Mar 2016 16:17:11 -0400 Subject: [PATCH] Fix a race in maintaining the journald reader list The journald log reader keeps a map of following readers so that it can close them properly when the journald reader object itself is closed, but it was possible for its worker goroutine to be scheduled so that the worker attempted to remove a reader from the map before the reader had been added to the map. This patch adds the item to the map before starting the goroutine which is expected to eventually remove it. Signed-off-by: Nalin Dahyabhai (github: nalind) Upstream-commit: 4d200cd6938c1416e34bf43576b0d528b73e8ba3 Component: engine --- components/engine/daemon/logger/journald/read.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/engine/daemon/logger/journald/read.go b/components/engine/daemon/logger/journald/read.go index 8d94c302fb..fb95ea479e 100644 --- a/components/engine/daemon/logger/journald/read.go +++ b/components/engine/daemon/logger/journald/read.go @@ -173,6 +173,9 @@ drain: } func (s *journald) followJournal(logWatcher *logger.LogWatcher, config logger.ReadConfig, j *C.sd_journal, pfd [2]C.int, cursor string) { + s.readers.mu.Lock() + s.readers.readers[logWatcher] = logWatcher + s.readers.mu.Unlock() go func() { // Keep copying journal data out until we're notified to stop. for C.wait_for_data_or_close(j, pfd[0]) == 1 { @@ -184,9 +187,6 @@ func (s *journald) followJournal(logWatcher *logger.LogWatcher, config logger.Re delete(s.readers.readers, logWatcher) s.readers.mu.Unlock() }() - s.readers.mu.Lock() - s.readers.readers[logWatcher] = logWatcher - s.readers.mu.Unlock() // Wait until we're told to stop. select { case <-logWatcher.WatchClose():