瀏覽代碼

logger/journald: follow the thread-safety rules

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider 3 年之前
父節點
當前提交
f7fe2c2290
共有 1 個文件被更改,包括 9 次插入0 次删除
  1. 9 0
      daemon/logger/journald/read.go

+ 9 - 0
daemon/logger/journald/read.go

@@ -106,6 +106,7 @@ import "C"
 
 import (
 	"errors"
+	"runtime"
 	"strings"
 	"time"
 	"unsafe"
@@ -264,6 +265,14 @@ func (s *journald) readLogs(logWatcher *logger.LogWatcher, config logger.ReadCon
 		sinceUnixMicro uint64
 		untilUnixMicro uint64
 	)
+	// Quoting https://www.freedesktop.org/software/systemd/man/sd-journal.html:
+	//     Functions that operate on sd_journal objects are thread
+	//     agnostic — given sd_journal pointer may only be used from one
+	//     specific thread at all times (and it has to be the very same one
+	//     during the entire lifetime of the object), but multiple,
+	//     independent threads may use multiple, independent objects safely.
+	runtime.LockOSThread()
+	defer runtime.UnlockOSThread()
 
 	// Get a handle to the journal.
 	if rc := C.sd_journal_open(&j, C.int(0)); rc != 0 {