Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

qemu: Don't leak file descriptors in case of error#2684

Merged
jcvenegas merged 1 commit into
kata-containers:masterfrom
c3d:github-2683-fd-leak
May 14, 2020
Merged

qemu: Don't leak file descriptors in case of error#2684
jcvenegas merged 1 commit into
kata-containers:masterfrom
c3d:github-2683-fd-leak

Conversation

@c3d
Copy link
Copy Markdown
Member

@c3d c3d commented May 13, 2020

If we take one of the error paths from setupVirtiofsd() after
opening the fd variable, the fd.Close() function is not called.

Fixes: #2683

Signed-off-by: Christophe de Dinechin dinechin@redhat.com

If we take one of the error paths from setupVirtiofsd() after
opening the fd variable, the fd.Close() function is not called.

Fixes: kata-containers#2683

Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
Copy link
Copy Markdown

@jodh-intel jodh-intel left a comment

Choose a reason for hiding this comment

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

Good catch @c3d, and thanks!

lgtm

@jodh-intel
Copy link
Copy Markdown

/test-ubuntu

Copy link
Copy Markdown
Member

@jcvenegas jcvenegas left a comment

Choose a reason for hiding this comment

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

I think you need to remove line

fd.Close()

Comment thread virtcontainers/qemu.go
if err != nil {
return err
}
defer fd.Close()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't we remove the fd.Close() call a few lines below?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

OOI, I had wondered if we needed to drop the later fd.Close() as well - but, hmm, as it is it means we close the fd after its last use. If we drop that, it will stay open for longer when not needed. On the flip side, we can only leave it there if calling fd.Close() on an already closed fd is safe - as it will still be called by the defer...

Copy link
Copy Markdown
Member Author

@c3d c3d May 14, 2020

Choose a reason for hiding this comment

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

The intent of that fd.Close() is clearly to close the fd before entering the loop, so I left it there intentionally, after checking that closing twice was OK.

Close closes the File, rendering it unusable for I/O. On files that support SetDeadline, any pending I/O operations will be canceled and return immediately with an error. Close will return an error if it has already been called.

(Emphasis mine)

Reference: https://golang.org/pkg/os/#File.Close

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, just to confirm, the final action on that is to remove the fd.Close() line mentioned, considering "Close will return an error if it has already been called". is it?

(Christophe's Emphasis ;-))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I see @c3d's point after a private conversation.

fd.Close() will error out with "already closed", but won't crash, so that may be fine as we don't check / report that error.

I'm fine with this as is now. @jcvenegas, do you agree? If so, feel free to remove the "do-not-merge" label and merge it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hey thanks for the clarifications, I agree that will not be harmful in the case if it is called two times. I think is just a matter of different styles but the same result. let's merge it :)

@fidencio
Copy link
Copy Markdown
Member

@jcvenegas was faster than me! ;-)
Adding the DNM label till the fd.Close() comment is addressed.

@fidencio fidencio added the do-not-merge PR has problems or depends on another label May 13, 2020
@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2020

Codecov Report

Merging #2684 into master will increase coverage by 4.55%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master    #2684      +/-   ##
==========================================
+ Coverage   45.93%   50.48%   +4.55%     
==========================================
  Files         118      118              
  Lines       17653    17139     -514     
==========================================
+ Hits         8109     8653     +544     
+ Misses       8628     7428    -1200     
- Partials      916     1058     +142     

@c3d
Copy link
Copy Markdown
Member Author

c3d commented May 14, 2020

I think you need to remove line

fd.Close()

I disagree. First, it's safe to fd.Close() twice, see other comment above. Second, the existing fd.Close() is clearly intended to not leave a useless fd open while you run the loop following it.

@jcvenegas jcvenegas removed the do-not-merge PR has problems or depends on another label May 14, 2020
@jcvenegas jcvenegas merged commit 444c5fa into kata-containers:master May 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File descriptor not closed properly in setupVirtiofsd

6 participants