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 <nalin@redhat.com> (github: nalind)
Upstream-commit: 4d200cd6938c1416e34bf43576b0d528b73e8ba3
Component: engine
This commit is contained in:
Nalin Dahyabhai
2016-03-17 16:17:11 -04:00
parent 4d8c3b87c5
commit ecc2f43e0e

View File

@ -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():