8000 Fix tainted stderr on docker exec by fornellas · Pull Request #5852 · docker/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix tainted stderr on docker exec #5852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

fornellas
Copy link
Contributor
@fornellas fornellas commented Feb 20, 2025

- What I did

#5229 (cc @thaJeztah) seems to have introduced a bug, that makes docker exec taint stderr, by printing exit status X when a command is run. This was not the case before, and introduces issues with anything that depends on using docker exec to run commands and process the output (something I do a lot when running tests). So, this new behavior breaks this previous contract, and goes against how analog *nix commands work (eg: sh -c false).

Here's an example. Given we have a container started with docker run -ti --rm --name ubuntu ubuntu, before we could do:

$ docker exec ubuntu ls / /bad-path
ls: cannot access '/bad-path': No such file or directory
/:
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

and it'd output correct stdout & stderr and exit 2 (coming from ls).

After #5229, things changed to:

$ docker exec ubuntu ls / /bad-path
ls: cannot access '/bad-path': No such file or directory                              
/:
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
exit status 2

so there's an extra line on stderr exit status 2.

- How I did it

This PR restores the previous functional behavior, by adding an extra if to handle this case, exactly it was handled before, so I'd not expect it to have any unintended side effects.

- How to verify it

I've bisected the change to the bad commit #5229, validated the before / after with the commands above, and that this PR fixes the issue.

- Human readable description for the release notes

Fix unintentionally printing exit status to stderr when `docker exec` returns a non-zero status

image

Signed-off-by: Fabio Pugliese Ornellas <fabio.ornellas@gmail.com>
@codecov-commenter
Copy link
codecov-commenter commented Feb 20, 2025

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Project coverage is 58.98%. Comparing base (2493a96) to head (96bee0f).
Report is 7 commits behind head on master.

❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (50.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5852      +/-   ##
==========================================
+ Coverage   58.86%   58.98%   +0.12%     
==========================================
  Files         349      343       -6     
  Lines       29562    29303     -259     
==========================================
- Hits        17401    17285     -116     
+ Misses      11180    11043     -137     
+ Partials      981      975       -6     

Copy link
Member
@neersighted neersighted left a comment

Choose a reason for hiding this comment

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

Discussed in the maintainer's call; this behavior is unintended and was flagged as a regression after merging the last PR. The implementation here is a bit messy (the StatusError/exitCode abstraction is leaking badly), but it's no worse than it was before @thaJeztah attempted to refactor here.

Let's take this now to fix the obvious change in behavior, and flag this area of the code for another round of clean-up later.

@@ -43,7 +43,10 @@ func main() {
}

if err != nil && !errdefs.IsCancelled(err) {
_, _ = fmt.Fprintln(os.Stderr, err)
var stErr cli.StatusError
if errors.As(err, &stErr) && stErr.Status != "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is filtering all non-StatusErrors now 😅 I think it needs to be something like this:

Suggested change
if errors.As(err, &stErr) && stErr.Status != "" {
if !errors.As(err, &stErr) || stErr.Status != "" {

Copy link
Member

Choose a reason for hiding this comment

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

Good catch; this looked plausible at first glance as it was identical to the removed code, but at this spot in the code we need to adjust the conditional.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this isn't quite it, as we seem to have actually added the exit status as expected to some tests. I'll probably have to spend a bit of time figuring out how much of that is intended/what the expected behavior is here.

I suspect it is to print 'exit status Y' for CLI plugins, but not for attach/exec; however that might require a new type as both current are a StatusError...

Copy link
Member

Choose a reason for hiding this comment

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

I'll take a look at it soon.
After a quick look, we shouldn't update the code here. We'll break too many other things in the process.

Copy link
Member

Choose a reason for hiding this comment

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

@neersighted please see this PR #5854

@thaJeztah
Copy link
Member

Thanks @fornellas ! We merged a fix in #5854, but added you as "co-author" on the commit; thank you so much for contributing!

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.

6 participants
0