Skip to content

Fix fd leak in vsock.Dial in case of connect errors #65

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

Merged
merged 1 commit into from
Mar 26, 2022

Conversation

xelez
Copy link
Contributor

@xelez xelez commented Mar 24, 2022

Fixes #64

Before commit when trying to connect in cycle with no valid endpoint:

$ lsof -p 874838 | grep AF_VSOCK
lsof: WARNING: can't stat() nsfs file system /run/docker/netns/default
      Output information may be incomplete.
servicevm 874838 xelez    9u     sock                0,9      0t0  8504215 protocol: AF_VSOCK
servicevm 874838 xelez   11u     sock                0,9      0t0  8504763 protocol: AF_VSOCK
servicevm 874838 xelez   12u     sock                0,9      0t0  8505726 protocol: AF_VSOCK
servicevm 874838 xelez   13u     sock                0,9      0t0  8504774 protocol: AF_VSOCK
servicevm 874838 xelez   14u     sock                0,9      0t0  8507619 protocol: AF_VSOCK
servicevm 874838 xelez   15u     sock                0,9      0t0  8500873 protocol: AF_VSOCK
servicevm 874838 xelez   16u     sock                0,9      0t0  8502193 protocol: AF_VSOCK
servicevm 874838 xelez   17u     sock                0,9      0t0  8507691 protocol: AF_VSOCK
servicevm 874838 xelez   18u     sock                0,9      0t0  8504845 protocol: AF_VSOCK
servicevm 874838 xelez   19u     sock                0,9      0t0  8510828 protocol: AF_VSOCK

After this patch no FD are leaking:

$ lsof -p 876844 | grep AF_VSOCK
lsof: WARNING: can't stat() nsfs file system /run/docker/netns/default
      Output information may be incomplete.

Signed-off-by: Alex Ankudinov xelez0@gmail.com

@@ -39,6 +53,8 @@ func Dial(cid, port uint32) (Conn, error) {
if errno, ok := err.(syscall.Errno); ok && errno == syscall.EINTR {
continue
}
// Trying not to leak fd here
closeFD(fd)
Copy link
Member

Choose a reason for hiding this comment

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

Hey, thanks for reporting this and providing a patch.

I'd be happy to merge bar this minor nit. This ignores the return value, which is fine since we want the error from the Connect() call. However this may trip some linters and the canonical way would be to write:

_ := closeFD(fd)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! Good point.

:= gives me an error:

# github.com/linuxkit/virtsock/pkg/vsock
pkg/vsock/vsock_linux.go:57:6: no new variables on left side of :=

So I fixed it with just _ = .

Signed-off-by: Alex Ankudinov <xelez0@gmail.com>
Copy link
Member

@rn rn left a comment

Choose a reason for hiding this comment

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

Great, thanks for the fix.

@rn rn merged commit 1c88ab3 into linuxkit:master Mar 26, 2022
@xelez xelez deleted the fix_fd_leak branch March 26, 2022 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File descriptor leak in Dial function on Linux
2 participants