atharv1205 opened a new pull request, #941: URL: https://github.com/apache/commons-io/pull/941
Jira: https://issues.apache.org/jira/browse/IO-591 ## Problem `Tailer` silently stops delivering lines when the tailed file is rotated to an empty file twice without any write in between (for example, `mv app.log app.log.1; touch app.log` twice), and the newest file is then written to. Reproduction on master (844a19b42) with a real file: tail `app.log`, write `first`, rotate (move, create empty) twice without writing, then write `after-two-rotations` to the new `app.log`. - Actual: the listener receives `[first]` only, and nothing more is ever read. - Expected: `[first, after-two-rotations]`. ## Root cause In `Tailer.run()`, a rotation is detected only when `length < position`. After the first rotation, the position is reset to 0 on the new, empty file. The second empty replacement also has length 0, so the second rotation is never detected and the reader stays open on the renamed file. When data is later written to the path, `length > position` holds and `readLines(reader)` is called on the stale reader, which is at end of file forever. ## Fix In the `length > position` branch, the open reader is probed for one byte at the current position. Only if it is at end of file, a new reader is opened on the path to check for data at the same position. If the new reader has data, the path names another file, so the existing rotation handling runs: the old reader is drained and closed, and the replacement is read from the beginning. - Normal tailing is unchanged apart from a one byte read and a seek per poll that has new data. The extra open happens only when the open reader is at end of file while the path is longer. - A size that is ahead of the readable data (for example on a caching network file system, or a custom `Tailable`) is not treated as a rotation, because the new reader is also at end of file. This avoids replaying the whole file or reopening in a tight loop. - No public API change. ## Tests Added to `TailerRotationTest`, deterministic with in-memory `Tailable` implementations in the style of the existing tests: - `testRotationToEmptyFileTwiceWithoutWrites`: the IO-591 scenario. Fails on master with `expected: <[first, after]> but was: <[first]>`, passes with the fix. - `testPartialLineIsNotRotation`: a trailing partial line is not mistaken for a rotation. - `testSizeAheadOfDataIsNotRotation`: a size ahead of the readable data is not mistaken for a rotation. `mvn` (default goal) passes: 7664 tests, 0 failures; Checkstyle, PMD, SpotBugs, japicmp and RAT are clean. Before you push a pull request, review this list: - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project. - [x] Read the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) if you use Artificial Intelligence (AI). - [x] I used AI for assistance. - [x] Run a successful build using the default [Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command line by itself. - [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice. - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why. - [x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
