[Bug]: `container logs -n N` truncates the oldest line when it exceeds the 1024-byte read chunk

Open Beginner friendly
#1,967 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
swift
Domain
cli

Research direction

Start at Application.ContainerLogs.tail(fh:n:follow:) and reproduce the issue with the supplied container logs -n 1 logbug | wc -c command. Trace the backwards 1024-byte reads and verify that container logs -n N preserves the complete oldest returned line, including when it crosses a chunk boundary.

Written by the indexing model from the issue text.

Description

Steps to reproduce
  1. Start a container that logs a single line longer than 1024 bytes:
    container run -d --name logbug alpine \
      sh -c 'printf "START"; i=0; while [ $i -lt 3000 ]; do printf "X"; i=$((i+1)); done; printf "END\n"; sleep 120'
    
  2. container logs -n 1 logbug | wc -c
Current behavior

Returns 1024 bytes, and the output starts in the middle of the line — the leading START is gone. The oldest (here, only) returned line is truncated.

Expected behavior

Returns the full line (~3009 bytes), matching container logs logbug (without -n), which returns the complete line.

More generally, container logs -n N truncates the oldest of the N returned lines whenever the last N lines span more than one 1024-byte read chunk (e.g. three ~800-byte lines with -n 2).

Root cause

Application.ContainerLogs.tail(fh:n:follow:) reads the file backwards in 1024-byte chunks and stops as soon as lines.count >= n. When the last N lines span more than one chunk, the first line in the buffer is a fragment truncated at the chunk boundary (the buffer starts mid-line while offset > 0), and Array(lines.suffix(n)) keeps it. It should keep reading until it has more than N non-empty lines (so the partial leading line is dropped) or reaches the start of the file.

Environment
  • container 1.1.0
  • macOS 26.5 (Tahoe), Apple Silicon (M-series)
Dominant language
Swift
Stars
50.1k
Forks
1.8k
Avg merge
2d 2h
Merged PRs (30d)
19

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apple/container

All issues in apple/container

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.