split: ignore input appended after sizing in -n l/N #16977
a line straddling the end still gets the appended bytes: one\ntwo + appended three\n gives twothree
could we just use reader.take(num_bytes) before the loop, like n_chunks_by_byte does? then no break and no comment are needed
the filter runs asynchronously, so the append will usually happen after the loop has finished
i think this test passes without the fix too, could you please make it deterministic (FIFO like in the issue)?
please also add a test for a last line without newline that gets extended by the append
Fixed. I replaced the early break with reader.take(num_bytes) before split(sep), so a line straddling the sized boundary can no longer read past it. Added test_number_by_lines_ignores_appended_bytes_in_last_line: one\ntwo + appended three\n now yields two instead of twothree. Real cargo test confirms it fails on the old code and passes with the fix.
You were right - the filter append raced the read loop. Both tests now use paired output FIFOs: opening xaa unblocks split after sizing, and opening xab cannot happen until it has written the first chunk, so the append is always between sizing and the read loop. Verified deterministic in a real cargo test run (9/9 split -n l tests pass).
Added test_number_by_lines_ignores_appended_bytes_in_last_line: the input is one\ntwo with no trailing newline and three\n is appended mid-run; the second output must be exactly two.
Addressed all three review points in a force-updated branch (rebased onto current main, commit 95ce4ee759a9):
1. reader.take(num_bytes) before split(sep) replaces the early break, so straddling lines are truncated at the sized byte count.
2. Both regression tests now use FIFOs for deterministic append timing.
3. Added the no-trailing-newline last-line test.
Checks: rustfmt clean; real cargo test --no-default-features --features split --test tests test_number_by_lines = 9/9 pass; new straddling test fails on the unmodified source (twothree) and passes with the fix. Rebased because upstream advanced 12 commits and changed both files since the first commit.
Thanks for the precise review.
sylvestre
TINGyu123644
Fixes #16973
-n l/Nsizes the input before reading. Reading through EOF afterwards let bytes appended to a live log leak into the output, and a line straddling the sized boundary could pick up the appended bytes in full (two+three->twothree).The reader is now bounded with
take(num_bytes)beforesplit(sep), the same approachn_chunks_by_byteuses; the previous earlybreakand its comment are gone.Two regression tests use paired output FIFOs: opening the first unblocks split after sizing but before the read loop, and opening the second is impossible until it has written the first chunk. One appends whole lines to a file ending in a separator; the other appends to a last line without one and checks the appended bytes are dropped.
Validation:
rustfmt --checkclean on both files. Realcargo test --no-default-features --features split --test tests test_number_by_linesagainst current upstream main: 9/9 pass. On the unmodified source the straddling-line test fails withtwothreeinstead oftwo.AI-assisted: the change and tests were drafted with an AI coding agent.
Signed-off-by: TINGyu123644 <TINGyu123644@users.noreply.github.com>