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

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

@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
Vitalymt and others added 3 commits October 7, 2026 14:56
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
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
@DmitryRomanov
DmitryRomanov force-pushed the fix/eof-without-newline-drop branch from 17e94d4 to 51aabdd Compare October 7, 2026 07:56
if isEOFReached {
// 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 len(accumBuf) > 0 {

@DmitryRomanov DmitryRomanov Oct 7, 2026 •

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.

I propose to check skipLine here:

if len(accumBuf) > 0 && !skipLine {

Because it is possible when event isn't completely written

This branch has not been deployed

No deployments
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