Skip to content

Commit 41bd52b

Browse files
committed
os: remove NewFile socket detection on Windows
NewFile was recently updated (in CL 668195) to detect whether the handle is a socket or not. This special case is not really necessary, given that socket handles can be used as if they were normal file handles on all functions supported by os.File (see https://learn.microsoft.com/en-us/windows/win32/winsock/socket-handles-2). Not only is not necessary, but is can also be problematic, as there is no way to reliably detect whether a handle is a socket or not. For example, the test failure reported in #73630 is caused by a named pipe wrongly detected as a socket. This aligns with the Unix NewFile behavior of returning an os.File that identifies itself as a file handle even if it is a socket. This makes os.File.Close to always return os.ErrClosed in case of multiple calls rather than sometimes returning "use of closed network connection". Updates #10350. Fixes #73630. Change-Id: Ia8329783d5c8ef6dac34ef69ed1ce9d2a9862e11 Reviewed-on: https://go-review.googlesource.com/c/go/+/671455 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 176a215 commit 41bd52b

File tree

1 file changed

+1
-36
lines changed

1 file changed

+1
-36
lines changed

src/os/file_windows.go

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -82,49 +82,14 @@ func newConsoleFile(h syscall.Handle, name string) *File {
8282
return newFile(h, name, "console", false)
8383
}
8484

85-
var wsaLoaded atomic.Bool
86-
87-
// isWSALoaded returns true if the ws2_32.dll module is loaded.
88-
func isWSALoaded() bool {
89-
// ws2_32.dll may be delay loaded, we can only short-circuit
90-
// if we know it is loaded.
91-
if wsaLoaded.Load() {
92-
return true
93-
}
94-
var ws2_32_dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0}
95-
_, err := windows.GetModuleHandle(unsafe.SliceData(ws2_32_dll[:]))
96-
wsaLoaded.Store(err == nil)
97-
return err == nil
98-
}
99-
10085
// newFileFromNewFile is called by [NewFile].
10186
func newFileFromNewFile(fd uintptr, name string) *File {
10287
h := syscall.Handle(fd)
10388
if h == syscall.InvalidHandle {
10489
return nil
10590
}
106-
kind := "file"
107-
var sotype int
108-
if t, err := syscall.GetFileType(h); err == nil && t == syscall.FILE_TYPE_PIPE {
109-
kind = "pipe"
110-
// Windows reports sockets as FILE_TYPE_PIPE.
111-
// We need to call getsockopt and check the socket type to distinguish between sockets and pipes.
112-
// If the call fails, we assume it's a pipe.
113-
// Avoid calling getsockopt if the WSA module is not loaded, it is a heavy dependency
114-
// and sockets can only be created using that module.
115-
if isWSALoaded() {
116-
if sotype, err = syscall.GetsockoptInt(h, syscall.SOL_SOCKET, windows.SO_TYPE); err == nil {
117-
kind = "net"
118-
}
119-
}
120-
}
12191
nonBlocking, _ := windows.IsNonblock(syscall.Handle(fd))
122-
f := newFile(h, name, kind, nonBlocking)
123-
if kind == "net" {
124-
f.pfd.IsStream = sotype == syscall.SOCK_STREAM
125-
f.pfd.ZeroReadIsEOF = sotype != syscall.SOCK_DGRAM && sotype != syscall.SOCK_RAW
126-
}
127-
return f
92+
return newFile(h, name, "file", nonBlocking)
12893
}
12994

13095
func epipecheck(file *File, e error) {

0 commit comments

Comments
 (0)