Skip to content
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

syscall: Read on Windows causes internal/syscall/windows.ERROR_INVALID_PARAMETER (87) #49826

Closed
qianxi0410 opened this issue Nov 28, 2021 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@qianxi0410
Copy link

What version of Go are you using (go version)?

$ go version 
go1.17.3 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\qianxi\AppData\Local\go-build
set GOENV=C:\Users\qianxi\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=E:\GOPATH\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\GOPATH
set GOPRIVATE=
set GOPROXY=https://goproxy.io,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\qianxi\AppData\Local\Temp\go-build1867567529=/tmp/go-build -gno-record-gcc-switches

What did you do?

My code is :

func readClosed(conn net.Conn) bool {
	var readClosed bool
	f := func(fd uintptr) bool {
		one := make([]byte, 1024)
		// ERR: will cause error: The parameter is incorrect.
		n, err := syscall.Read(syscall.Handle(fd), one)

		if err != nil && err != syscall.EAGAIN || err == nil && n == 0 {
			readClosed = true
		}

		return true
	}

	conn.SetReadDeadline(time.Time{})

	var rawConn syscall.RawConn
	switch v := conn.(type) {
	case *net.TCPConn:
		rawConn, _ = v.SyscallConn()
	default:
		return false
	}

	// half closed
	if err := rawConn.Read(f); err != nil {
		return true
	}

	return readClosed
}

What did you expect to see?

not cause internal/syscall/windows.ERROR_INVALID_PARAMETER (87)

What did you see instead?

internal/syscall/windows.ERROR_INVALID_PARAMETER (87)

@mknyszek mknyszek changed the title syscall.Read on windows will cause internal/syscall/windows.ERROR_INVALID_PARAMETER (87) syscall: Read on Windows causes internal/syscall/windows.ERROR_INVALID_PARAMETER (87) Nov 30, 2021
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 30, 2021
@mknyszek mknyszek added this to the Backlog milestone Nov 30, 2021
@mknyszek
Copy link
Contributor

CC @bufflig

@mknyszek
Copy link
Contributor

CC @alexbrainman

@mattn
Copy link
Member

mattn commented Nov 30, 2021

The connection should be managed by I/O Completion Port APIs. So it need to wait the completion with Overlapped.

package main

import (
	"log"
	"net"
	"syscall"
	"time"

	"golang.org/x/sys/windows"
)

func newOverlapped() (*windows.Overlapped, error) {
	var ov windows.Overlapped
	h, err := windows.CreateEvent(nil, 0, 1, nil)
	if h == 0 {
		return nil, err
	}
	ov.HEvent = h
	return &ov, nil
}

func readClosed(conn net.Conn) bool {
	var readClosed bool
	f := func(fd uintptr) bool {
		one := make([]byte, 1024)
		var n uint32
		ov, err := newOverlapped()
		if err != nil {
			return false
		}
		defer windows.CloseHandle(ov.HEvent)

		err = windows.ReadFile(windows.Handle(fd), one, &n, ov)
		if err != nil && err == syscall.ERROR_IO_PENDING {
			if err = windows.GetOverlappedResult(windows.Handle(fd), ov, &n, true); err != nil {
				return false
			}
		}

		if err != nil && err != syscall.EAGAIN || err == nil && n == 0 {
			readClosed = true
		}

		return true
	}

	conn.SetReadDeadline(time.Time{})

	var rawConn syscall.RawConn
	var err error
	switch v := conn.(type) {
	case *net.TCPConn:
		rawConn, err = v.SyscallConn()
		if err != nil {
			log.Fatal(err)
		}
	default:
		return false
	}

	// half closed
	if err := rawConn.Read(f); err != nil {
		return true
	}

	return readClosed
}

func main() {
	conn, err := net.Dial("tcp", "localhost:8888")
	if err != nil {
		log.Fatal(err)
	}
	println(readClosed(conn))
}

@qianxi0410
Copy link
Author

thanks :D. But it still does't work for me, so i switch my os to linux, now it works.

@golang golang locked and limited conversation to collaborators Nov 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants