net/connstate: support remote-close detection on Windows
- Dominant language
- Go
- Stars
- 24
- Forks
- 6
- PR merge metrics
- No merged PRs in 30d
Description
## Describe the bug
`net/connstate.ListenConnState` always fails on Windows because `poll_windows.go` returns `connstate not supported for windows` from every control operation.
This also disables client-disconnection sensing in downstream servers. For example, Hertz v0.10.5 uses `connstate` for `WithSenseClientDisconnection`; when registration fails it logs the error and disables connection-close detection instead of falling back to its earlier read probe. A streaming handler waiting on request context cancellation can therefore remain active after the Windows client disconnects.
## To reproduce
On Windows, with gopkg v0.2.0 or current `main`:
```powershell
$env:GOWORK = 'off'
go test ./net/connstate
```
The connection-state tests fail at `ListenConnState` with:
```text
connstate not supported for windows
```
A downstream Hertz v0.10.5 streaming test likewise times out waiting for the request context to be canceled after the client closes the response body.
## Expected behavior
Windows should detect both graceful and abortive remote TCP closure without consuming application data, transition the state to `StateRemoteClosed`, and invoke `WithOnRemoteClosed` once.
## Proposed implementation
Use the Windows `WSAPoll` API with a nonblocking poll:
- request only out-of-band readability so ordinary unread data does not cause a busy loop;
- handle `POLLHUP` and `POLLERR`, which Winsock reports independently of requested read/write readiness;
- serialize socket registration/removal with each nonblocking poll so a socket handle cannot be closed or reused while `WSAPoll` references it;
- add a regression test that closes a connection with unread data and without an application `Read`;
- run the unit-test matrix on `windows-latest`.
Microsoft documentation: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsapoll
No new dependency is required.
## Environment
- OS: Windows 11 amd64
- Go: 1.26.4
- gopkg: v0.2.0 / current `main`
- downstream reproduction: Hertz v0.10.5
Contributor guide
Research direction
Start in net/connstate/poll_windows.go and run go test ./net/connstate on Windows with GOWORK=off to reproduce the failure. Review the proposed nonblocking WSAPoll behavior and add the regression test for unread data and no application Read. Done means graceful and abortive remote TCP closes reach StateRemoteClosed, invoke WithOnRemoteClosed once, and the unit-test matrix runs on windows-latest.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- ci-cd, networking, operating-systems, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100