Skip to content
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
8 changes: 6 additions & 2 deletions runtime/kubernetes/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ func (c *client) setupContainerEnvironment(ctn *pipeline.Container) error {
func (c *client) TailContainer(ctx context.Context, ctn *pipeline.Container) (io.ReadCloser, error) {
c.Logger.Tracef("tailing output for container %s", ctn.ID)

// create a logsContext that will be canceled at the end of this
logsContext, logsDone := context.WithCancel(ctx)
defer logsDone()

Comment on lines +241 to +244
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aargh. This is wrong.

We can't create a context here because we expect (at least part of) logsFunc to continue running after TailContainer returns. Then StreamStep/StreamService/etc read from the returned ReadCloser until the end of the stream. If the context gets canceled at the end of TailContainer, then the streaming is stopped before Stream* has read anything from it.

// create object to store container logs
var logs io.ReadCloser

Expand All @@ -261,7 +265,7 @@ func (c *client) TailContainer(ctx context.Context, ctn *pipeline.Container) (io
stream, err := c.Kubernetes.CoreV1().
Pods(c.config.Namespace).
GetLogs(c.Pod.ObjectMeta.Name, opts).
Stream(context.Background())
Stream(logsContext)
if err != nil {
c.Logger.Errorf("%v", err)
return false, nil
Expand Down Expand Up @@ -305,7 +309,7 @@ func (c *client) TailContainer(ctx context.Context, ctn *pipeline.Container) (io
// perform the function to capture logs with periodic backoff
//
// https://pkg.go.dev/k8s.io/apimachinery/pkg/util/wait?tab=doc#ExponentialBackoff
err := wait.ExponentialBackoff(backoff, logsFunc)
err := wait.ExponentialBackoffWithContext(logsContext, backoff, logsFunc)
if err != nil {
return nil, err
}
Expand Down