Skip to content
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

Close pipe in overlay2 graphdriver #34863

Merged
merged 1 commit into from
Sep 20, 2017
Merged

Close pipe in overlay2 graphdriver #34863

merged 1 commit into from
Sep 20, 2017

Conversation

keloyang
Copy link
Contributor

@keloyang keloyang commented Sep 15, 2017

If cmd.Start() fail in mountFrom, we will have pipe not closed.

Reported-by:Qiang Ning ningqiang1@huawei.com
Signed-off-by: Shukui Yang yangshukui@huawei.com

thaJeztah
thaJeztah previously approved these changes Sep 15, 2017
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM see #34863 (comment)

ping @dmcgowan

Also, should this be cherry-picked in a release?

Copy link
Member

@yongtang yongtang left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah changed the title Use defer to close pipe. Use defer to close pipe in overlay2 graphdriver Sep 15, 2017
@vieux
Copy link
Contributor

vieux commented Sep 15, 2017

ping @kolyshkin

@kolyshkin
Copy link
Contributor

LGTM

@@ -57,7 +58,6 @@ func mountFrom(dir, device, target, mType string, flags uintptr, label string) e
if err := json.NewEncoder(w).Encode(options); err != nil {
return fmt.Errorf("mountfrom json encode to pipe failed: %v", err)
}
w.Close()
Copy link
Member

@dmcgowan dmcgowan Sep 15, 2017

Choose a reason for hiding this comment

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

The close was explicitly called before Wait to ensure that the process is not waiting on additional input.

Copy link
Member

Choose a reason for hiding this comment

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

In that case, should a w.Close() be put inside the err ifs?

Copy link
Member

Choose a reason for hiding this comment

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

And probably a comment added to prevent someone from changing to a defer

Copy link
Member

Choose a reason for hiding this comment

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

@thaJeztah I think it is more a matter of style, I prefer to have w.Close inside the if statements but calling w.Close multiple times (one before Wait, and one on defer) is not a problem.

Copy link
Member

@yongtang yongtang left a comment

Choose a reason for hiding this comment

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

My fault. Didn't realized w.Close() has to be done before the Wait().

@keloyang Can you update the PR based on @dmcgowan 's comment?

@kolyshkin
Copy link
Contributor

So, before putting my LGTM I wrote a test program to check whether json decoder would wait on input if it's not closed. Apparently as soon as json data goes through it proceeds even if stdin is still open.

@keloyang
Copy link
Contributor Author

keloyang commented Sep 18, 2017

@dmcgowan @kolyshkin @thaJeztah @yongtang @vieux thanks for your reviews.

I see go's code for cmd.Wait(), I think process will not block if reexec process exit, becuase fd.Close() will not block.

func (c *Cmd) Wait() 
	c.Process.Wait()
		syscall.Wait4
	c.closeDescriptors(c.closeAfterWait)
	   fd.Close()

I also have the following code to test,

//add to mount.go
//func MountFrom(dir, device, target, mType string, flags uintptr, label string) error {
//	return mountFrom(dir, device, target, mType, flags, label)
//}

package main

import (
	"fmt"
	"os"
	
	"github.com/docker/docker/pkg/reexec"
	"github.com/docker/docker/daemon/graphdriver/overlay2"
)

func main(){
	if reexec.Init() {
		return
	}
	
	dir, _:=  os.Getwd()
	if err := overlay2.MountFrom(dir, "overlay", "merged", "overlay", 
				0, "lowerdir=lower,upperdir=upper,workdir=work"); err != nil {
		fmt.Printf("overlay mount to [%v]\n", err)
	}
}

the output:

[root@localhost test]# ls 
lower  merged  mountFrom  mountFrom.go  upper  work
[root@localhost test]# ls merged/
[root@localhost test]# time ./mountFrom

real    0m0.013s
user    0m0.006s
sys     0m0.010s
[root@localhost test]# ls merged/
a

So I agree with @kolyshkin, can we still use defer close ,WDYT ?

@kolyshkin
Copy link
Contributor

It works without explicit Close(), as json.Decode() finishes as long as the document is complete (last closing bracket is received). Still, an extra Close() before Wait() looks cleaner to me.

Signed-off-by: Shukui Yang <yangshukui@huawei.com>
@keloyang keloyang changed the title Use defer to close pipe in overlay2 graphdriver Close pipe in overlay2 graphdriver Sep 20, 2017
@keloyang
Copy link
Contributor Author

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@dmcgowan dmcgowan left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@yongtang yongtang left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@thaJeztah
Copy link
Member

all green; merging

@yongtang yongtang merged commit e40d5e6 into moby:master Sep 20, 2017
@thaJeztah
Copy link
Member

LOL looks like we pressed the button at the same time

@yongtang
Copy link
Member

😄 👍 🍺

liusdu pushed a commit to liusdu/moby that referenced this pull request Oct 30, 2017
FIX DTS2017091508576

upstream pr moby#34863

Signed-off-by: yangshukui <yangshukui@huawei.com>
liusdu pushed a commit to liusdu/moby that referenced this pull request Oct 30, 2017
Use defer to close pipe

FIX DTS2017091508576

upstream pr moby#34863

Signed-off-by: yangshukui <yangshukui@huawei.com>



See merge request docker/docker!759
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.

7 participants