Repository navigation
Description
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
-
Handle Context Cancellation:
- Add
case <-ctx.Done():topushWriter.Commit()'s select statement and returnctx.Err(). - Call
pw.Close()to abort active background pipes.
- Add
-
Prevent HTTP Connection Leaks:
- Drain and close
resp.Body(usingio.Copy(io.Discard, resp.Body)) if a response was already received, allowinghttp.Clientto reuse the connection. - Also drain and close
resp.BodyinsetResponse()if the writer was already closed before the response arrived.
- Drain and close
Steps to reproduce the issue
Steps to reproduce
- Create a
pushWritervia the Docker pusher. - 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.Canceledorcontext.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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTodo
Activity
samuelkarp commentedon Oct 14, 2026
Are you seeing an actual problem with this? What situations are you seeing the context be canceled?
DARSHANR007 commentedon Oct 15, 2026
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
samuelkarp commentedon Oct 15, 2026
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.)
DARSHANR007 commentedon Oct 15, 2026
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.Commitmethod, I found that unlike thefetcher.gomethod, which checksctx.Done(), theCommitmethod uses acontext.Contextparameter 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.