While looking into Hyper-V socket implementation for using it within a service from vm, I have found out that implementation from HvSocketConn and HvSocketListener uses windows.Close to close a created socket, which calls windows.CloseHandle internally.
|
// HvsockListener is a socket listener for the AF_HYPERV address family. |
|
type HvsockListener struct { |
|
sock *win32File |
|
addr HvsockAddr |
|
} |
|
func (l *HvsockListener) Close() error { |
|
return l.sock.Close() |
|
} |
|
// HvsockConn is a connected socket of the AF_HYPERV address family. |
|
type HvsockConn struct { |
|
sock *win32File |
|
local, remote HvsockAddr |
|
} |
|
func (conn *HvsockConn) Close() error { |
|
return conn.sock.Close() |
|
} |
|
// closeHandle closes the resources associated with a Win32 handle. |
|
func (f *win32File) closeHandle() { |
|
f.wgLock.Lock() |
|
// Atomically set that we are closing, releasing the resources only once. |
|
if !f.closing.Swap(true) { |
|
f.wgLock.Unlock() |
|
// cancel all IO and wait for it to complete |
|
_ = cancelIoEx(f.handle, nil) |
|
f.wg.Wait() |
|
// at this point, no new IO can start |
|
windows.Close(f.handle) |
|
f.handle = 0 |
|
} else { |
|
f.wgLock.Unlock() |
|
} |
|
} |
|
|
|
// Close closes a win32File. |
|
func (f *win32File) Close() error { |
|
f.closeHandle() |
|
return nil |
|
} |
from "golang.org/x/sys/windows..
func Close(fd Handle) (err error) {
return CloseHandle(fd)
}
However it is stated that in CloseHandle
Do not use the CloseHandle function to close a socket. Instead, use the closesocket function, which releases all resources associated with the socket including the handle to the socket object. For more information, see Socket Closure.
which makes this look like doing something like
// closeHandle closes the resources associated with a Win32 handle.
func (f *win32File) closeHandle() {
f.wgLock.Lock()
// Atomically set that we are closing, releasing the resources only once.
if !f.closing.Swap(true) {
f.wgLock.Unlock()
// cancel all IO and wait for it to complete
_ = cancelIoEx(f.handle, nil)
f.wg.Wait()
// at this point, no new IO can start
if f.socket {
windows.Closesocket(f.handle)
} else {
windows.Close(f.handle)
}
f.handle = 0
} else {
f.wgLock.Unlock()
}
}
would be required to close a socket instead of a regular windows file?
Is using windows.CloseHandle a valid approach for closing a Hyper-V socket as it is not a common network socket or is this something needs to be fixed?
While looking into Hyper-V socket implementation for using it within a service from vm, I have found out that implementation from
HvSocketConnandHvSocketListeneruseswindows.Closeto close a created socket, which callswindows.CloseHandleinternally.go-winio/hvsock.go
Lines 166 to 170 in bdc6c11
go-winio/hvsock.go
Lines 284 to 286 in bdc6c11
go-winio/hvsock.go
Lines 174 to 178 in bdc6c11
go-winio/hvsock.go
Lines 507 to 509 in bdc6c11
go-winio/file.go
Lines 113 to 134 in bdc6c11
from "golang.org/x/sys/windows..
However it is stated that in CloseHandle
which makes this look like doing something like
would be required to close a socket instead of a regular windows file?
Is using
windows.CloseHandlea valid approach for closing a Hyper-V socket as it is not a common network socket or is this something needs to be fixed?