Skip to content

Fix state file truncation detection - #1858

Merged
jefchien merged 3 commits into
mainfrom
fix-truncate
Sep 12, 2025
Merged

Fix state file truncation detection#1858
jefchien merged 3 commits into
mainfrom
fix-truncate

Conversation

@jefchien

Copy link
Copy Markdown
Contributor

Description of the issue

The recent state manager refactor introduced an issue where the state file no longer resets when a tailed file is truncated. The problem was that on restart, the state file was read and used to determine where to seek to in the file, but that same offset was not used as a starting point for the state file management. So, it was unable to determine that the new offset was smaller than the restored one.

Steps to reproduce

  1. Write logs
date >> /tmp/log/test
date >> /tmp/log/test
date >> /tmp/log/test
  1. Truncate the logs
truncate /tmp/log/test --size 0
  1. Restart the agent
  2. Write again (the state file will not get updated)
date >> /tmp/log/test
  1. Restart the agent (it will send the same log again as the state will default to 0)

Description of changes

Changes the state file restoration so the restored offset gets tracked.

License

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Tests

Added unit tests. Before the fix, the newly added TestLogsFileTruncateRestart would fail because it would not have used the restored offset from the state file to detect truncation.

Requirements

Before commiting your code, please do the following steps.

  1. Run make fmt and make fmt-sh
  2. Run make lint

Integration Tests

To run integration tests against this PR, add the ready for testing label.

@jefchien
jefchien requested a review from a team as a code owner September 11, 2025 22:33
@jefchien jefchien added the ready for testing Indicates this PR is ready for integration tests to run label Sep 12, 2025
@jefchien
jefchien merged commit 4d30835 into main Sep 12, 2025
193 of 195 checks passed
@jefchien
jefchien deleted the fix-truncate branch September 12, 2025 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for testing Indicates this PR is ready for integration tests to run

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants