Skip to content

fix: flush accumulated buffer on EOF without trailing newline#976

Open
Vitalymt wants to merge 3 commits into
ozontech:masterfrom
Vitalymt:fix/eof-without-newline-drop
Open

fix: flush accumulated buffer on EOF without trailing newline#976
Vitalymt wants to merge 3 commits into
ozontech:masterfrom
Vitalymt:fix/eof-without-newline-drop

Conversation

@Vitalymt

Copy link
Copy Markdown

Problem

When a file doesn't end with a newline character, the last line is silently dropped.

The read loop in worker.go accumulates data in accumBuf until it finds a \n. At EOF, the remaining data is saved to job.tail without ever being emitted through controller.In(). If the file is never written to again, that data is lost permanently.

Steps to reproduce:

  1. Create a file with content that doesn't end with \n: echo -n "hello" > /tmp/test.txt
  2. Configure file.d to read this file
  3. The "hello" event is never emitted

Fix

Emit accumBuf as a final event before calling processEOF when EOF is reached and the buffer is non-empty. Clear both accumBuf and job.tail afterward to prevent re-emission on file truncation.

Changes:

  • plugin/input/file/worker.go: +8 lines — flush buffer on EOF
  • plugin/input/file/worker_test.go: updated existing test expectation + added multi-line test case

Fixes #912

When a file doesn't end with a newline character, the last line is
silently dropped. The read loop accumulates data in accumBuf until it
finds a '\n', but at EOF the remaining data is saved to job.tail
without ever being emitted through controller.In().

If the file is never written to again, that data is lost permanently.

Fix: emit accumBuf as a final event before calling processEOF when
EOF is reached and the buffer is non-empty. Clear both accumBuf and
job.tail afterward to prevent re-emission on file truncation.

Fixes ozontech#912

@DmitryRomanov DmitryRomanov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Comment thread plugin/input/file/worker.go Outdated

// Flush remaining data when EOF is reached and there's no trailing newline.
// Without this, the last line of a file that doesn't end with '\n' is silently dropped.
if isEOFReached && len(accumBuf) > 0 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

isEOFReached is checked twice.

It looks cleaner:

if isEOFReached {
    // Flush remaining data when EOF is reached and there's no trailing newline.
    if len(accumBuf) > 0 {
        // ...
    }
    err := w.processEOF(...)
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, done. Nested the flush inside the single isEOFReached block.

Nest the accumBuf flush inside the existing isEOFReached check
instead of testing the flag twice. Functionally identical, just
cleaner structure.
- Remove should_ok_and_empty_when_read_not_ready_line (duplicates should_emit_last_line_without_trailing_newline)
- Update no_new_line test: expect data to be emitted at EOF even without trailing newline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: skips last line without new line

3 participants