瀏覽代碼

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)
Nalin Dahyabhai 9 年之前
父節點
當前提交
4d200cd693
共有 1 個文件被更改,包括 3 次插入3 次删除
  1. 3 3
      daemon/logger/journald/read.go

+ 3 - 3
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) {
 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() {
 	go func() {
 		// Keep copying journal data out until we're notified to stop.
 		// Keep copying journal data out until we're notified to stop.
 		for C.wait_for_data_or_close(j, pfd[0]) == 1 {
 		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)
 		delete(s.readers.readers, logWatcher)
 		s.readers.mu.Unlock()
 		s.readers.mu.Unlock()
 	}()
 	}()
-	s.readers.mu.Lock()
-	s.readers.readers[logWatcher] = logWatcher
-	s.readers.mu.Unlock()
 	// Wait until we're told to stop.
 	// Wait until we're told to stop.
 	select {
 	select {
 	case <-logWatcher.WatchClose():
 	case <-logWatcher.WatchClose():