Skip to content

driver/kubernetes: close exec-stream pipes when the stream ends#3966

Open
pgimalac wants to merge 1 commit into
docker:masterfrom
pgimalac:pgimalac/kubernetes-execconn-close-on-stream-end
Open

driver/kubernetes: close exec-stream pipes when the stream ends#3966
pgimalac wants to merge 1 commit into
docker:masterfrom
pgimalac:pgimalac/kubernetes-execconn-close-on-stream-end

Conversation

@pgimalac

@pgimalac pgimalac commented Jul 22, 2026

Copy link
Copy Markdown

Ensure the far ends of the command exec pipes are properly closed when the exec stream ends (due to an error or not), to prevent infinite hangs in reader/writer.

Currently, if the remote kubernetes executor gets killed, buildx just hangs forever because it tries to read from pipes which are never closed. With this change, the pipes are immediately closed when StreamWithContext finishes, and readers/writers are unblocked.

Refs #556.

Added some unit tests to highlight the fix.

Repro / testing

Setup:

# create kind cluster
kind create cluster --name buildkit-repro

# create a builder
docker buildx create --driver kubernetes --name kindbuilder --driver-opt namespace=default,replicas=1 --bootstrap

Create a Dockerfile which takes some time, eg.:

FROM alpine:latest
RUN echo "step1 starting" && sleep 120 && echo "step1 done"

and build it:

docker buildx build --builder kindbuilder --progress=plain -f Dockerfile .

Wait for the log to show that it's running the sleep step, then open a new shell and kill it:

POD=$(kubectl get pods -l app=kindbuilder0 -o jsonpath='{.items[0].metadata.name}')
kubectl delete pod "$POD" --grace-period=0 --force

The build command will then hang forever (in particular won't finish after 2 minutes).

Now build cmd/buildx from this PR, and re-run the same steps using this buildx binary instead of docker buildx, you should observe that the build command logs err="io: read/write on closed pipe" when the pod is killed, and fails with the following error:

ERROR: failed to build: failed to receive status: rpc error: code = Unavailable desc = closing transport due to: connection error: desc = "error reading from server: command terminated with exit code 137", received prior goaway: code: NO_ERROR, debug data: "graceful_stop"`

@pgimalac
pgimalac force-pushed the pgimalac/kubernetes-execconn-close-on-stream-end branch 2 times, most recently from bcca1d6 to 6d0a21d Compare July 22, 2026 15:45
Comment thread driver/kubernetes/execconn/execconn_test.go
@crazy-max

Copy link
Copy Markdown
Member

Fixes #556.

Since #556 originally reports the docker-container driver and this PR fixes the Kubernetes exec path, Refs #556 may be more accurate than Fixes #556 unless the original docker-container hang is known to be covered elsewhere.

@pgimalac

Copy link
Copy Markdown
Author

#556 originally reports the docker-container driver

@crazy-max yeah fair, I figured it was a catch-all for both (in particular considering the last message is about kubernetes), but I'll update the description.

Signed-off-by: Pierre Gimalac <23154723+pgimalac@users.noreply.github.com>
@pgimalac
pgimalac force-pushed the pgimalac/kubernetes-execconn-close-on-stream-end branch from 6d0a21d to c4b1789 Compare July 22, 2026 16:15
@pgimalac
pgimalac marked this pull request as ready for review July 22, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants