Skip to content

Commit 4347cf4

Browse files
committed
If tail.File have been stopped all calls to Next are now returning
context.Canceled
1 parent 4011dad commit 4347cf4

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

internal/component/loki/source/file/internal/tail/file.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ type File struct {
7777
func (f *File) Next() (*Line, error) {
7878
f.mu.Lock()
7979
defer f.mu.Unlock()
80+
81+
if f.ctx.Err() != nil {
82+
return nil, f.ctx.Err()
83+
}
84+
8085
read:
8186
text, err := f.readLine()
82-
8387
if err != nil {
8488
if errors.Is(err, io.EOF) {
8589
if err := f.wait(text != ""); err != nil {

internal/component/loki/source/file/internal/tail/file_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ func TestFile(t *testing.T) {
216216
verify(t, file, &Line{Text: "2025-03-11 11:11:02.71 Server (c) Microsoft Corporation.", Offset: 819}, nil)
217217
verify(t, file, &Line{Text: "2025-03-11 11:11:02.72 Server All rights reserved.", Offset: 876}, nil)
218218
})
219+
220+
t.Run("calls to next after stop", func(t *testing.T) {
221+
name := createFile(t, "stopped", "hello\n")
222+
defer removeFile(t, name)
223+
224+
file, err := NewFile(log.NewNopLogger(), &Config{
225+
Offset: 0,
226+
Filename: name,
227+
})
228+
require.NoError(t, err)
229+
file.Stop()
230+
231+
verify(t, file, nil, context.Canceled)
232+
})
219233
}
220234

221235
func createFile(t *testing.T, name, content string) string {

0 commit comments

Comments
 (0)