Skip to content

remotes/docker: pushWriter.Commit blocks indefinitely when context is canceled #14358

Open

Description

@DARSHANR007

Description

The Problem

In core/remotes/docker/pusher.go, pushWriter.Commit(ctx context.Context, ...) accepts a context.Context parameter, but its select statement that waits for registry upload responses does not check ctx.Done().
https://github.com/containerd/containerd/blob/main/core/remotes/docker/pusher.go#L529-L545

// TODO: timeout waiting for response
var resp *http.Response
select {
case <-pw.done:
    return io.ErrClosedPipe
case err := <-pw.errC:
    pw.Close()
    return err
case resp = <-pw.respC:
    defer resp.Body.Close()
case p := <-pw.pipeC:
    return pw.replacePipe(p)
}

Impact

If a caller's push operation is cancelled or its context deadline expires while waiting for the registry to complete the upload:

Commit() ignores ctx and blocks indefinitely until one of the internal channels fires or the remote TCP connection drops.
Goroutines and resources waiting on Commit() remain hung.
By comparison, the matching fetcher logic in core/remotes/docker/fetcher.go explicitly listens to case <-ctx.Done(): return ctx.Err().

My Suggestion

  1. Handle Context Cancellation:

    • Add case <-ctx.Done(): to pushWriter.Commit()'s select statement and return ctx.Err().
    • Call pw.Close() to abort active background pipes.
  2. Prevent HTTP Connection Leaks:

    • Drain and close resp.Body (using io.Copy(io.Discard, resp.Body)) if a response was already received, allowing http.Client to reuse the connection.
    • Also drain and close resp.Body in setResponse() if the writer was already closed before the response arrived.

Steps to reproduce the issue

Steps to reproduce

  1. Create a pushWriter via the Docker pusher.
  2. Call pw.Commit(ctx, ...) using a context with a timeout or an already-canceled context:
    ctx, cancel := context.WithCancel(context.Background())
    cancel()
    err := pw.Commit(ctx, 0, "")
    

Describe the results you received and expected

Expected

When pushWriter.Commit(ctx, ...) is called with a canceled or timed-out context:

  • It should immediately return ctx.Err() (context.Canceled or context.DeadlineExceeded).
  • It should close the underlying writer and avoid leaking any received HTTP response bodies.

Received

pushWriter.Commit(ctx, ...) ignores ctx.Done(). It blocks indefinitely waiting on internal response channels (pw.done, pw.errC, pw.respC, pw.pipeC), causing the calling goroutine to hang until the remote TCP connection drops or the process terminates.

What version of containerd are you using?

v2.4.2

Any other relevant information

No response

Show configuration if it is related to CRI plugin.

No response

Activity

  1. samuelkarp commented on Oct 14, 2026

    @samuelkarp
    Member

    Are you seeing an actual problem with this? What situations are you seeing the context be canceled?

  2. DARSHANR007 commented on Oct 15, 2026

    @DARSHANR007
    Author

    Hi @samuelkarp, thankyou for addressing ,

    Hi @samuelkarp,

    The situation comes up when a caller sets a timeout on the push (like context.WithTimeout) or cancels the context when a build/push job is stopped.

    Right now, if the remote registry stalls or drops the connection while finishing the upload, Commit() keeps waiting on its internal channels forever. Because it doesn't check ctx.Done(), it ignores that the context expired or was canceled, leaving the calling goroutine blocked instead of failing fast.

    Adding case <-ctx.Done(): lets it exit immediately with ctx.Err() and close the writer

    Thank you

  3. samuelkarp commented on Oct 15, 2026

    @samuelkarp
    Member

    The situation comes up when a caller sets a timeout on the push (like context.WithTimeout) or cancels the context when a build/push job is stopped.

    Did you do this in practice? Why?

    (I'm trying to tell if you ran into an actual problem, or if this is just an LLM scanning the codebase looking for problems.)

  4. DARSHANR007 commented on Oct 15, 2026

    @DARSHANR007
    Author

    This is not a result of any automated LLM . While going through the pusher code while reading about the remotes package, I came across this TODO comment on line 530 (// TODO: timeout waiting for response).

    While going through the pushWriter.Commit method, I found that unlike the fetcher.go method, which checks ctx.Done(), the Commit method uses a context.Context parameter but does not listen to it in the select statement.

    I have written a test with a cancelled context being passed to pw.Commit() and verified that it gets blocked on the internal channels rather than returning immediately due to the context error

    . This way, even though it happens rarely, we can avoid the goroutine getting stuck in case of caller-set deadlines/aborting of pushes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions