Skip to content

Commit

Permalink
Revert "fix: don't panic when logs waits for more than 5 seconds (tes…
Browse files Browse the repository at this point in the history
…tcontainers#947)"

This reverts commit 5185956.
  • Loading branch information
mdelapenya committed May 10, 2023
1 parent 6c3b1e5 commit 19ffe9c
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type DockerContainer struct {
terminationSignal chan bool
consumers []LogConsumer
raw *types.ContainerJSON
stopProducer context.CancelFunc
stopProducer chan bool
logger Logging
lifecycleHooks []ContainerLifecycleHooks
}
Expand Down Expand Up @@ -630,9 +630,9 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
return errors.New("log producer already started")
}

ctx, c.stopProducer = context.WithCancel(ctx)
c.stopProducer = make(chan bool)

go func() {
go func(stop <-chan bool) {
since := ""
// if the socket is closed we will make additional logs request with updated Since timestamp
BEGIN:
Expand All @@ -643,22 +643,20 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
Since: since,
}

ctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()

r, err := c.provider.client.ContainerLogs(ctx, c.GetContainerID(), options)
if err != nil {
// if we can't get the logs, retry in one second.
c.logger.Printf("cannot get logs for container %q: %v", c.ID, err)
if ctx.Err() != nil {
// context done.
return
}
time.Sleep(1 * time.Second)
goto BEGIN
// if we can't get the logs, panic, we can't return an error to anything
// from within this goroutine
panic(err)
}
defer c.provider.Close()

for {
select {
case <-ctx.Done():
case <-stop:
err := r.Close()
if err != nil {
// we can't close the read closer, this should never happen
Expand Down Expand Up @@ -709,7 +707,7 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
}
}
}
}()
}(c.stopProducer)

return nil
}
Expand All @@ -718,8 +716,7 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
// and sending them to each added LogConsumer
func (c *DockerContainer) StopLogProducer() error {
if c.stopProducer != nil {
// Cancel the producer's context.
c.stopProducer()
c.stopProducer <- true
c.stopProducer = nil
}
return nil
Expand Down

0 comments on commit 19ffe9c

Please sign in to comment.