Skip to content

Commit

Permalink
HvsockConn shutdown, and .IsClosed() function
Browse files Browse the repository at this point in the history
Corrected `HvsockConn` `CloseRead`/`Write` to check if the socket
has been closed before attempting to shutdown the socket for
reading or writing.
Additionally, `shutdown` was not respecting whether to shutdown reading
or writing, and only shutting down the socket for reading.

Added `IsClosed` function to `win32File` (and therefore `win32Pipe` and
`win32MessageBytePipe`) and `HvsockConn` to check if the socket/pipe
has been closed already.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
  • Loading branch information
helsaawy committed Feb 17, 2022
1 parent 01a3671 commit 0aa6c0a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions file.go
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package winio
Expand Down Expand Up @@ -143,6 +144,11 @@ func (f *win32File) Close() error {
return nil
}

// IsClosed checks if the file has been closed
func (f *win32File) IsClosed() bool {
return f.closing.isSet()
}

// prepareIo prepares for a new IO operation.
// The caller must call f.wg.Done() when the IO is finished, prior to Close() returning.
func (f *win32File) prepareIo() (*ioOperation, error) {
Expand Down
17 changes: 13 additions & 4 deletions hvsock.go
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package winio
Expand Down Expand Up @@ -252,15 +253,23 @@ func (conn *HvsockConn) Close() error {
return conn.sock.Close()
}

func (conn *HvsockConn) IsClosed() bool {
return conn.sock.IsClosed()
}

func (conn *HvsockConn) shutdown(how int) error {
err := syscall.Shutdown(conn.sock.handle, syscall.SHUT_RD)
if conn.IsClosed() {
return ErrFileClosed
}

err := syscall.Shutdown(conn.sock.handle, how)
if err != nil {
return os.NewSyscallError("shutdown", err)
}
return nil
}

// CloseRead shuts down the read end of the socket.
// CloseRead shuts down the read end of the socket, preventing future read operations.
func (conn *HvsockConn) CloseRead() error {
err := conn.shutdown(syscall.SHUT_RD)
if err != nil {
Expand All @@ -269,8 +278,8 @@ func (conn *HvsockConn) CloseRead() error {
return nil
}

// CloseWrite shuts down the write end of the socket, notifying the other endpoint that
// no more data will be written.
// CloseWrite shuts down the write end of the socket, preventing future write operations and
// notifying the other endpoint that no more data will be written.
func (conn *HvsockConn) CloseWrite() error {
err := conn.shutdown(syscall.SHUT_WR)
if err != nil {
Expand Down

0 comments on commit 0aa6c0a

Please sign in to comment.