Skip to content

fix: container stream should not be terminated by ctx - #5247

Merged
vvoland merged 3 commits into
docker:masterfrom
Benehiko:hotfix-sigterm-container
Jul 12, 2024
Merged

fix: container stream should not be terminated by ctx#5247
vvoland merged 3 commits into
docker:masterfrom
Benehiko:hotfix-sigterm-container

Conversation

@Benehiko

@BenehikoBenehiko commented Jul 8, 2024

Copy link
Copy Markdown
Member

fixes: #5241

- What I did
Prevent the hijacked stream from exiting on main ctx cancellation. When the user sends a termination signal to the CLI while attached to a container, the signal should be forwarded to the container and handled by the process inside the container. The CLI should wait for the container to gracefully exit.

If the user wishes to exit without waiting, sending more termination requests will still forcefully exit the CLI.

- How I did it

- How to verify it

handle_sigterm.sh

#!/bin/sh# Function to handle SIGTERMhandle_sigterm() {
echo"Received SIGTERM, exiting..."exit 0
}
trap'handle_sigterm' TERM
whiletrue;doecho"Waiting for sigterm"
sleep 10
done
⋊> ~/G/cli on hotfix-sigterm-container ◦ build/docker run -i \  -v $HOME/Downloads/:/home:ro \ alpine:latest \ /home/handle_sigterm.shWaiting for sigtermWaiting for sigtermReceived SIGTERM, exiting...
⋊> ~ ps -ef | grep "handle_sigterm.sh" | grep "docker run"benehiko 279458 277233 1 17:48 pts/5 00:00:00 build/docker run -i -v /home/benehiko/Downloads/:/home:ro alpine:latest /home/handle_sigterm.sh⋊> ~ kill -15 279458 

- Description for the changelog

sending a termination request to the CLI while attached to a container, will wait for the container to exit before closing the stream.

- A picture of a cute animal (not mandatory but encouraged)

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
@Benehiko
Benehiko requested a review from a teamJuly 8, 2024 15:48
@codecov-commenter

codecov-commenter commented Jul 8, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.49%. Comparing base (9bb1a62) to head (991b130).
Report is 3 commits behind head on master.

Additional details and impacted files
@@ Coverage Diff @@## master #5247 +/- ##
==========================================
+ Coverage 61.48% 61.49% +0.01% 
==========================================
Files 298 298 Lines 20811 20812 +1 ==========================================
+ Hits 12795 12798 +3 + Misses 7103 7099 -4 - Partials 913 915 +2 

Comment threadcli/command/container/run.go Outdated
Comment threadcli/command/container/run.go Outdated
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
@Benehiko

Copy link
Copy Markdown
MemberAuthor

hmm... latest commit causing some strange test failures. Don't merge this yet since I've been able to get the same error locally.

51.13 === Failed51.13 === FAIL: cli/command/container TestRunCommandWithContentTrustErrors (unknown)51.13 --- PASS: TestRunCommandWithContentTrustErrors/offline-notary-server (0.00s)51.13 Error: write /dev/pts/0: file already closed51.13 --- PASS: TestRunCommandWithContentTrustErrors/uninitialized-notary-server (0.00s)51.13 panic: Fail in goroutine after TestRunAttachTermination has completed51.13 51.13 goroutine 71 [running]:51.13 testing.(*common).Fail(0xc000780340)51.13 /usr/local/go/src/testing/testing.go:952 +0xd451.13 testing.(*common).FailNow(0xc000780340)51.13 /usr/local/go/src/testing/testing.go:975 +0x2651.13 github.com/docker/cli/vendor/gotest.tools/v3/assert.ErrorIs({0x1030a50, 0xc000780340}, {0x1027d40?, 0xc0003e6090}, {0x1027e20?, 0x16ddf30}, {0x0, 0x0, 0x0})51.13 /go/src/github.com/docker/cli/vendor/gotest.tools/v3/assert/assert.go:311 +0x15951.13 github.com/docker/cli/cli/command/container.TestRunAttachTermination.func6()51.13 /go/src/github.com/docker/cli/cli/command/container/run_test.go:85 +0x8551.13 created by github.com/docker/cli/cli/command/container.TestRunAttachTermination in goroutine 20951.13 /go/src/github.com/docker/cli/cli/command/container/run_test.go:84 +0x486

@Benehiko

Copy link
Copy Markdown
MemberAuthor

hmm... latest commit causing some strange test failures. Don't merge this yet since I've been able to get the same error locally.

51.13 === Failed
51.13 === FAIL: cli/command/container TestRunCommandWithContentTrustErrors (unknown)
51.13 --- PASS: TestRunCommandWithContentTrustErrors/offline-notary-server (0.00s)
51.13 Error: write /dev/pts/0: file already closed
51.13 --- PASS: TestRunCommandWithContentTrustErrors/uninitialized-notary-server (0.00s)
51.13 panic: Fail in goroutine after TestRunAttachTermination has completed
51.13 51.13 goroutine 71 [running]:
51.13 testing.(*common).Fail(0xc000780340)
51.13 /usr/local/go/src/testing/testing.go:952 +0xd4
51.13 testing.(*common).FailNow(0xc000780340)
51.13 /usr/local/go/src/testing/testing.go:975 +0x26
51.13 github.com/docker/cli/vendor/gotest.tools/v3/assert.ErrorIs({0x1030a50, 0xc000780340}, {0x1027d40?, 0xc0003e6090}, {0x1027e20?, 0x16ddf30}, {0x0, 0x0, 0x0})
51.13 /go/src/github.com/docker/cli/vendor/gotest.tools/v3/assert/assert.go:311 +0x159
51.13 github.com/docker/cli/cli/command/container.TestRunAttachTermination.func6()
51.13 /go/src/github.com/docker/cli/cli/command/container/run_test.go:85 +0x85
51.13 created by github.com/docker/cli/cli/command/container.TestRunAttachTermination in goroutine 209
51.13 /go/src/github.com/docker/cli/cli/command/container/run_test.go:84 +0x486

So I've been running tests locally quite a few times and could not reproduce this again... So maybe just a flake.

Sign up for freeto 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.

SIGTERM sent to docker run command doesn't send a SIGTERM to the main process running inside the container anymore.

5 participants

@Benehiko@codecov-commenter@vvoland@krissetto@thaJeztah