Skip to content

logs: wait for logging binary before reading log file#4785

Draft
AkihiroSuda wants to merge 5 commits intocontainerd:mainfrom
AkihiroSuda:fix-4782-attempt-2
Draft

logs: wait for logging binary before reading log file#4785
AkihiroSuda wants to merge 5 commits intocontainerd:mainfrom
AkihiroSuda:fix-4782-attempt-2

Conversation

@AkihiroSuda
Copy link
Member

When a container has exited, nerdctl logs now calls WaitForLogger() before reading the JSON log file. This ensures the logging binary (a separate process) has finished writing all log entries to disk.

Previously, WaitForLogger was only called in the follow (-f) code path. For non-follow reads like nerdctl logs --since 60s, the log file was read immediately without waiting, causing flaky test failures when the logging binary hadn't finished processing the final container output.

This fixes TestLogs/since_60s and TestLogs/until_60s which failed intermittently because the log file was empty or incomplete at read time.


Attempt to fix #4782

NOTE: used Claude Code

When a container has exited, `nerdctl logs` now calls WaitForLogger()
before reading the JSON log file. This ensures the logging binary (a
separate process) has finished writing all log entries to disk.

Previously, WaitForLogger was only called in the follow (-f) code path.
For non-follow reads like `nerdctl logs --since 60s`, the log file was
read immediately without waiting, causing flaky test failures when the
logging binary hadn't finished processing the final container output.

This fixes TestLogs/since_60s and TestLogs/until_60s which failed
intermittently because the log file was empty or incomplete at read time.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@AkihiroSuda AkihiroSuda reopened this Mar 10, 2026
The logging binary had a goroutine that closed internal pipe writers
immediately upon container exit (via gRPC notification). This raced
with copyStream, which was still draining data from the external pipe.
When the gRPC notification arrived before copyStream finished, the
pipe writer was closed mid-copy, causing remaining data to be lost.

Fix by making copyStream responsible for closing the pipe writers
after all data has been copied. The container-exit goroutine (and its
getContainerWait infrastructure) is removed — pipe closure now happens
naturally when the parent process exits or the shim closes the pipe.

Fixes containerd#4782

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
AkihiroSuda and others added 3 commits March 10, 2026 20:17
…e reading logs

Two fixes for flaky TestLogs/since_60s and TestLogs/until_60s:

1. In loggingProcessAdapter, the container-exit goroutine closed internal
   pipe writers immediately on container exit. This raced with copyStream
   goroutines still draining data from external pipes, causing output to
   be lost. Fix: after container exit, cancel the external readers to
   unblock copyStream, then wait for copyStream to finish before closing
   the pipe writers. This ensures all buffered data is processed.

2. In Logs(), call WaitForLogger() before reading log files when the
   container is not running, ensuring the logging binary has finished
   writing all entries.

Fixes containerd#4782

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Flaky tests: TestLogs/since_60s TestLogs/until_60s

1 participant