Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions input/system/selfhosted/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,16 @@ func tailFile(ctx context.Context, path string, out chan<- SelfHostedLogStreamIt
TailLoop:
for {
select {
case line := <-t.Lines():
out <- SelfHostedLogStreamItem{Line: line.String()}
case line, ok := <-t.Lines():
if !ok {
break TailLoop

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Lines channel could close due to an internal error. In that case we should break out of the loop to free the file descriptor

}
select {
case out <- SelfHostedLogStreamItem{Line: line.String()}:
case <-ctx.Done():

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for context cancellation before writing to the out channel to avoid a possible deadlock

@lfittl lfittl Jul 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense especially since that channel is currently unbuffered (and it seems beneficial to keep that, or at most use a low capacity, so that we backpressure on the actual file read in go-tail to avoid high memory use with large log lines).

prefixedLogger.PrintVerbose("Stopping log tail for %s (stop requested)", path)
break TailLoop
}
case <-ctx.Done():
prefixedLogger.PrintVerbose("Stopping log tail for %s (stop requested)", path)
break TailLoop
Expand Down Expand Up @@ -329,6 +337,12 @@ func setupLogLocationTail(ctx context.Context, logLocation string, out chan<- Se
return fmt.Errorf("fsnotify new: %s", err)
}

err = watcher.Add(logLocation)
if err != nil {
watcher.Close()
return fmt.Errorf("fsnotify add \"%s\": %s", logLocation, err)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

watcher.Add is now before the goroutine launch so Add failures don't orphan the watcher goroutine and its open file tails.

}

go func() {
defer watcher.Close()
for {
Expand Down Expand Up @@ -358,7 +372,7 @@ func setupLogLocationTail(ctx context.Context, logLocation string, out chan<- Se
}
}
}
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename || event.Op&fsnotify.Chmod == fsnotify.Chmod {
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the rest of the changes, but I'm not sure it makes sense to close the tail if the permissions change. At the very least we can end up losing in-progress logs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable.

tailCancel, ok := openFiles[event.Name]
if ok {
tailCancel()
Expand All @@ -371,8 +385,6 @@ func setupLogLocationTail(ctx context.Context, logLocation string, out chan<- Se
case <-ctx.Done():
prefixedLogger.PrintVerbose("Log file fsnotify watcher received stop signal")
for fileName, tailCancel := range openFiles {
// TODO: This cancel might actually not be necessary since we are
// already canceling the parent context?
tailCancel()
delete(openFiles, fileName)
}
Expand All @@ -382,11 +394,6 @@ func setupLogLocationTail(ctx context.Context, logLocation string, out chan<- Se
}
}()

err = watcher.Add(logLocation)
if err != nil {
return fmt.Errorf("fsnotify add \"%s\": %s", logLocation, err)
}

return nil
}

Expand Down
Loading