Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: align header while log producers are reading #1085

Merged
merged 4 commits into from
May 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
Podman = "podman"
ReaperDefault = "reaper_default" // Default network name when bridge is not available
packagePath = "github.com/testcontainers/testcontainers-go"

logStoppedForOutOfSyncMessage = "Stopping log consumer: Headers out of sync"
)

// DockerContainer represents a container started using Docker
Expand Down Expand Up @@ -665,17 +667,21 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
return
default:
h := make([]byte, 8)
_, err := r.Read(h)
_, err := io.ReadFull(r, h)
if err != nil {
// proper type matching requires https://go-review.googlesource.com/c/go/+/250357/ (go 1.16)
if strings.Contains(err.Error(), "use of closed network connection") {
now := time.Now()
since = fmt.Sprintf("%d.%09d", now.Unix(), int64(now.Nanosecond()))
goto BEGIN
}
// this explicitly ignores errors
// because we want to keep procesing even if one of our reads fails
continue
if errors.Is(err, context.DeadlineExceeded) {
// Probably safe to continue here
continue
}
_, _ = fmt.Fprintf(os.Stderr, "container log error: %+v. %s", err, logStoppedForOutOfSyncMessage)
// if we would continue here, the next header-read will result into random data...
return
}

count := binary.BigEndian.Uint32(h[4:])
Expand All @@ -693,11 +699,17 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
logTypes := []string{"", StdoutLog, StderrLog}

b := make([]byte, count)
_, err = r.Read(b)
_, err = io.ReadFull(r, b)
if err != nil {
// TODO: add-logger: use logger to log out this error
_, _ = fmt.Fprintf(os.Stderr, "error occurred reading log with known length %s", err.Error())
continue
if errors.Is(err, context.DeadlineExceeded) {
// Probably safe to continue here
continue
}
// we can not continue here as the next read most likely will not be the next header
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
_, _ = fmt.Fprintln(os.Stderr, logStoppedForOutOfSyncMessage)
return
}
for _, c := range c.consumers {
c.Accept(Log{
Expand Down