-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
vsock: Use net.FileConn to create VirtioSocketConnection #63
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ea8461c
vsock: Use net.FileConn to create VirtioSocketConnection
cfergeau 8dc8720
vsock: Remove ports from VirtioSocketConnection
cfergeau ff58ded
vsock: Simplify VirtioSocketConnection implementation
cfergeau 12bf645
vsock: Remove dup/dupCh
cfergeau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is unnecessary because of closed the file descriptor in the virtualization framework.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't explicitly close the file, go garbage collector may do it for us: https://pkg.go.dev/os#File.Fd
This is problematic if the following sequence happens:
file := os.NewFile((uintptr)(vzVirtioSocketConnection.fileDescriptor), "")
vzVirtioSocketConnection.fileDescriptor
vzVirtioSocketConnection.fileDescriptor
file
, which is no longer the expected one and will cause unexpected issuesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why needs to call close method from your comment.
I think garbage collection is unrelated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://gist.githubusercontent.com/cfergeau/46a4a4fda70a83dbe2a9bf519cbabd28/raw/530c7d9a97e7a823fb324552bda470ad11fbbab9/fd.go
This shows the race which can happen when you call
os.NewFile()
on a file descriptor, and then other code closes the file descriptor.By calling Close() in
newVirtioSocketConnection
, we know we are closing the right file descriptor. The garbage collector won't callClose()
on it later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
vzVirtioSocketConnection.fileDescriptor
is owned by the virtualization framework, the code below would even be more correct. Sincenet.FileConn()
will alsoDup()
the file descriptor, this feels a bit overkill :-/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
net.FileConn will dup file descriptor. https://pkg.go.dev/net#FileConn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that when
os.NewFile(fd)
is used, go will always callClose(fd)
. If our code don't callClose()
, then the go runtime/go garbage collector will callClose()
automatically. If the go garbage collector closes the file, we don't control when it will do so.Are you referring to
net.FileConn(file)
? Or to another file descriptor?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done here: https://github.com/golang/go/blob/4bcf94b0232db65ed5df47e0127cdbc8866aec64/src/os/file_unix.go#L196
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cfergeau I know the Go call Close method in finalizer. Bu the timing is different with using defer.
Yes. about net.FileConn which dup from original file descriptor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind either now. Think about it after we have a problem.