Fix logs -n N truncating the oldest line at the read-chunk boundary#2000
Open
chengboonrong wants to merge 1 commit into
Open
Fix logs -n N truncating the oldest line at the read-chunk boundary#2000chengboonrong wants to merge 1 commit into
chengboonrong wants to merge 1 commit into
Conversation
chengboonrong
force-pushed
the
fix/logs-tail-partial-line
branch
from
July 23, 2026 13:25
a6ca3d7 to
ed87b89
Compare
`tail` reads the log file backward in 1024-byte chunks and stopped as soon as `lines.count >= n`. While `offset > 0` the buffer starts in the middle of a line, so the first line in the buffer is a fragment truncated at the chunk boundary. When the last N lines span more than one chunk, `Array(lines.suffix(n))` kept that fragment, so the oldest returned line was truncated -- e.g. `logs -n 1` on a single line longer than 1024 bytes returned only its trailing 1024 bytes. Keep reading until there are *more* than N non-empty lines (so the first of the N kept lines is guaranteed complete) or the start of the file is reached. Extracted the backward read into a testable `lastLines(fh:n:)`. Fixes apple#1967 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Chris Cheng <chengchris8715@gmail.com>
chengboonrong
force-pushed
the
fix/logs-tail-partial-line
branch
from
July 23, 2026 13:38
ed87b89 to
0a23438
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
container logs -n Ntruncated the oldest of the N returned lines whenever thoselines spanned more than one 1024-byte read chunk. For example, a single line
longer than 1024 bytes:
Root cause
Application.ContainerLogs.tail(fh:n:follow:)reads the file backward in1024-byte chunks and stopped as soon as
lines.count >= n. Whileoffset > 0the buffer starts in the middle of a line, so the first line in the buffer is a
fragment truncated at the chunk boundary — and
Array(lines.suffix(n))kept it.Fix
Keep reading until there are more than
nnon-empty lines (so the first ofthe
nkept lines is guaranteed complete) or the start of the file is reached.The backward read is extracted into a testable
lastLines(fh:n:); thefollowand full-file paths are unchanged.
Tests
Adds
ContainerLogsTestscovering a single line longer than one chunk, N linesspanning multiple chunks (the general case), the ordinary short-line case, and
Nlarger than the file. Verified locally:swift build,swift format lint --strict, and the new tests all pass.Fixes #1967
🤖 Generated with Claude Code