Skip to content

Commit 35b9701

Browse files
smirashanduur
authored andcommitted
fix: log duplication on log senders
When the buffer Writer is request, code unconditionally started all senders (in our case, this was always JSON network senders). This resulted in log duplication on service restart - each time service is started, the senders goroutine was recreated. Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com> (cherry picked from commit 75e4c4a)
1 parent d00754e commit 35b9701

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

internal/app/machined/pkg/runtime/logging/circular.go

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,28 @@ func (manager *CircularBufferLoggingManager) getSenders() []runtime.LogSender {
115115
}
116116
}
117117

118-
func (manager *CircularBufferLoggingManager) getBuffer(id string, create bool) (*circular.Buffer, error) {
118+
func (manager *CircularBufferLoggingManager) getBuffer(id string, create bool) (*circular.Buffer, bool, error) {
119119
buf, ok := manager.buffers.Load(id)
120-
if !ok {
121-
if !create {
122-
return nil, nil
123-
}
120+
if ok {
121+
return buf.(*circular.Buffer), false, nil
122+
}
124123

125-
b, err := circular.NewBuffer(
126-
circular.WithInitialCapacity(InitialCapacity),
127-
circular.WithMaxCapacity(ChunkCapacity),
128-
circular.WithNumCompressedChunks(NumCompressedChunks, manager.compressor),
129-
circular.WithSafetyGap(SafetyGap))
130-
if err != nil {
131-
return nil, err // only configuration issue might raise error
132-
}
124+
if !create {
125+
return nil, false, nil
126+
}
133127

134-
buf, _ = manager.buffers.LoadOrStore(id, b)
128+
b, err := circular.NewBuffer(
129+
circular.WithInitialCapacity(InitialCapacity),
130+
circular.WithMaxCapacity(ChunkCapacity),
131+
circular.WithNumCompressedChunks(NumCompressedChunks, manager.compressor),
132+
circular.WithSafetyGap(SafetyGap))
133+
if err != nil {
134+
return nil, false, err // only configuration issue might raise error
135135
}
136136

137-
return buf.(*circular.Buffer), nil
137+
buf, _ = manager.buffers.LoadOrStore(id, b)
138+
139+
return buf.(*circular.Buffer), true, nil
138140
}
139141

140142
// RegisteredLogs implements runtime.LoggingManager interface.
@@ -169,24 +171,29 @@ func (nopCloser) Close() error {
169171
// Writer implements runtime.LogHandler interface.
170172
func (handler *circularHandler) Writer() (io.WriteCloser, error) {
171173
if handler.buf == nil {
172-
var err error
174+
var (
175+
created bool
176+
err error
177+
)
173178

174-
handler.buf, err = handler.manager.getBuffer(handler.id, true)
179+
handler.buf, created, err = handler.manager.getBuffer(handler.id, true)
175180
if err != nil {
176181
return nil, err
177182
}
178183

179-
go func() {
180-
defer func() {
181-
if r := recover(); r != nil {
182-
handler.manager.fallbackLogger.Printf("log sender panic: %v", r)
184+
if created {
185+
go func() {
186+
defer func() {
187+
if r := recover(); r != nil {
188+
handler.manager.fallbackLogger.Printf("log sender panic: %v", r)
189+
}
190+
}()
191+
192+
if err := handler.runSenders(); err != nil {
193+
handler.manager.fallbackLogger.Printf("log senders stopped: %s", err)
183194
}
184195
}()
185-
186-
if err := handler.runSenders(); err != nil {
187-
handler.manager.fallbackLogger.Printf("log senders stopped: %s", err)
188-
}
189-
}()
196+
}
190197
}
191198

192199
switch handler.id {
@@ -202,7 +209,7 @@ func (handler *circularHandler) Reader(opts ...runtime.LogOption) (io.ReadCloser
202209
if handler.buf == nil {
203210
var err error
204211

205-
handler.buf, err = handler.manager.getBuffer(handler.id, false)
212+
handler.buf, _, err = handler.manager.getBuffer(handler.id, false)
206213
if err != nil {
207214
return nil, err
208215
}

0 commit comments

Comments
 (0)