Skip to content

Fix container logs -n truncating the oldest line past a read chunk#1968

Open
OrtegaMatias wants to merge 1 commit into
apple:mainfrom
OrtegaMatias:fix/logs-tail-truncation
Open

Fix container logs -n truncating the oldest line past a read chunk#1968
OrtegaMatias wants to merge 1 commit into
apple:mainfrom
OrtegaMatias:fix/logs-tail-truncation

Conversation

@OrtegaMatias

Copy link
Copy Markdown

Type of Change

  • Bug fix

Motivation and Context

container logs -n N truncated the oldest returned line whenever the last N log lines spanned more than one 1024-byte backward-read chunk. The clearest case: a single log line longer than 1024 bytes with -n 1 returns only its last 1024 bytes — the beginning of the line is cut off. The full-log path (container logs without -n) is unaffected.

Fixes #1967.

Description

ContainerLogs.tail reads the log file backwards in 1024-byte chunks and stopped at lines.count < n. When a read landed on exactly N non-empty segments whose first was a fragment (buffer starting mid-line, offset > 0), that fragment was returned as the oldest line.

  • Extracted the backward-read logic into a testable lastLines(fh:n:) -> [String].
  • Changed the loop bound from lines.count < n to lines.count <= n, so it always reads one line past N (or to the start of the file). The extra, possibly-partial leading line is then dropped by suffix(n), guaranteeing the oldest returned line is complete.

No behavior change for the common case (last N lines within one chunk); the full-log path is untouched.

Testing

Added Tests/ContainerCommandsTests/ContainerLogsTailTests.swift covering:

  • a single line longer than the 1024-byte chunk (-n 1),
  • multiple long lines spanning several chunks (-n 2),
  • short input, and
  • n greater than the number of lines.

Transparency note: I was unable to run the in-repo test suite locally — this machine only has an Xcode 27 beta toolchain (which fails building a transitive dependency under strict concurrency) and the Command Line Tools, which lack the test frameworks. I verified the fix logic in isolation (old logic truncates the oldest line; new logic returns it in full for all four cases above) and confirmed the change compiles as part of the ContainerCommands target. CI runs the added tests.

`container logs -n N` read the log file backwards in 1024-byte chunks and
stopped at `lines.count < n`. When the last N lines spanned more than one
chunk, the oldest line in the buffer was a fragment truncated at the chunk
boundary and was returned as-is — e.g. a single log line longer than 1024
bytes with `-n 1` returned only its last 1024 bytes.

Extract the backward-read into a testable `lastLines(fh:n:)` and read one
line past N (`<= n`) so the partial leading line is dropped by `suffix(n)`,
or stop at the start of the file. The full-log path is unchanged.
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]: container logs -n N truncates the oldest line when it exceeds the 1024-byte read chunk

1 participant