Repository: python-trio/trio

Issue:
IOCP polling doesn't work with Astrill VPN LSP
trio 0.16
win10 1803
```
  File "c:/Users/c/Desktop/test2.py", line 37, in <module>
    trio.run(parent)
  File "C:\Program Files\Python38\lib\site-packages\trio\_core\_run.py", line 1879, in run
    runner = setup_runner(
  File "C:\Program Files\Python38\lib\site-packages\trio\_core\_run.py", line 1781, in setup_runner
    io_manager = TheIOManager()
  File "C:\Program Files\Python38\lib\site-packages\trio\_core\_io_windows.py", line 349, in __init__
    base_handle = _get_base_socket(s, which=WSAIoctls.SIO_BASE_HANDLE)
  File "C:\Program Files\Python38\lib\site-packages\trio\_core\_io_windows.py", line 202, in _get_base_socket
    raise_winerror(code)
  File "C:\Program Files\Python38\lib\site-packages\trio\_core\_windows_cffi.py", line 321, in raise_winerror
    raise OSError(0, msg, filename, winerror, filename2)
OSError: [WinError 10014] The system detected an invalid pointer address in attempting to use a pointer argument in a call
```
both `echo-server.py` and `echo-client.py` in last of `https://trio.readthedocs.io/en/stable/tutorial.html` will cause this


Discussion:
Huh. That code runs every time Trio does anything with a socket on Windows, it has lots of tests, no meaningful parameters, and we've never seen that failure or had it reported. And looking at `_get_base_socket`, I don't see any way the pointers could be invalid. So... I'm baffled.

Are you using a 32-bit or 64-bit build of Python?

What do you get if you open a command prompt and run: `netsh winsock show catalog`? (There will be a lot of output.)
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32

<details>

```
netsh winsock show catalog

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Chain Entry
Description:                        ASProxy over [MSAFD Tcpip [TCP/IP]]
Provider ID:                        {03A42735-FD62-42D5-8BF8-792564F569F9}
Provider Path:                      C:\Windows\system32\ASProxy64.dll
Catalog Entry ID:                   1020
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x66
Protocol Chain Length:              2
Protocol Chain: 1019 : 1002


Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Chain Entry
Description:                        ASProxy over [MSAFD Tcpip [UDP/IP]]
Provider ID:                        {F1FBF23F-F6C8-49E0-853E-7DA6984FD4E9}
Provider Path:                      C:\Windows\system32\ASProxy64.dll
Catalog Entry ID:                   1021
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x609
Protocol Chain Length:              2
Protocol Chain: 1019 : 1003


Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Chain Entry
Description:                        ASProxy over [MSAFD Tcpip [TCP/IPv6]]
Provider ID:                        {D36F7F23-7B39-4200-9D7B-61891126CC51}
Provider Path:                      C:\Windows\system32\ASProxy64.dll
Catalog Entry ID:                   1022
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x66
Protocol Chain Length:              2
Protocol Chain: 1019 : 1005


Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Chain Entry
Description:                        ASProxy over [MSAFD Tcpip [UDP/IPv6]]
Provider ID:                        {8904A305-06AA-4D91-B8FC-D85C21E6540E}
Provider Path:                      C:\Windows\system32\ASProxy64.dll
Catalog Entry ID:                   1023
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x609
Protocol Chain Length:              2
Protocol Chain: 1019 : 1006


Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        AF_UNIX
Provider ID:                        {A00943D9-9C2E-4633-9B59-0057A3160994}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1001
Version:                            2
Address Family:                     1
Max Address Length:                 110
Min Address Length:                 2
Socket Type:                        1
Protocol:                           0
Service Flags:                      0x20026
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        MSAFD Tcpip [TCP/IP]
Provider ID:                        {E70F1AA0-AB8B-11CF-8CA3-00805F48A192}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1002
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x20066
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        MSAFD Tcpip [UDP/IP]
Provider ID:                        {E70F1AA0-AB8B-11CF-8CA3-00805F48A192}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1003
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x20609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        MSAFD Tcpip [RAW/IP]
Provider ID:                        {E70F1AA0-AB8B-11CF-8CA3-00805F48A192}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1004
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        3
Protocol:                           0
Service Flags:                      0x20609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        MSAFD Tcpip [TCP/IPv6]
Provider ID:                        {F9EAB0C0-26D4-11D0-BBBF-00AA006C34E4}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1005
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x20066
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        MSAFD Tcpip [UDP/IPv6]
Provider ID:                        {F9EAB0C0-26D4-11D0-BBBF-00AA006C34E4}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1006
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x20609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        MSAFD Tcpip [RAW/IPv6]
Provider ID:                        {F9EAB0C0-26D4-11D0-BBBF-00AA006C34E4}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1007
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        3
Protocol:                           0
Service Flags:                      0x20609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        RSVP TCPv6 Service Provider
Provider ID:                        {9D60A9E0-337A-11D0-BD88-0000C082E69A}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1008
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x22066
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        RSVP TCP Service Provider
Provider ID:                        {9D60A9E0-337A-11D0-BD88-0000C082E69A}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1009
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x22066
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        RSVP UDPv6 Service Provider
Provider ID:                        {9D60A9E0-337A-11D0-BD88-0000C082E69A}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1010
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x22609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        RSVP UDP Service Provider
Provider ID:                        {9D60A9E0-337A-11D0-BD88-0000C082E69A}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1011
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x22609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        Hyper-V RAW
Provider ID:                        {1234191B-4BF7-4CA7-86E0-DFD7C32B5445}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1012
Version:                            2
Address Family:                     34
Max Address Length:                 36
Min Address Length:                 36
Socket Type:                        1
Protocol:                           1
Service Flags:                      0x20026
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider
Description:                        MSAFD Irda [IrDA]
Provider ID:                        {3972523D-2AF1-11D1-B655-00805F3642CC}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1013
Version:                            2
Address Family:                     26
Max Address Length:                 32
Min Address Length:                 8
Socket Type:                        1
Protocol:                           1
Service Flags:                      0x20006
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Service Provider
Description:                        ASProxy
Provider ID:                        {E26DCE69-9F96-48B5-9BAC-557D554942DD}
Provider Path:                      C:\Windows\system32\ASProxy64.dll
Catalog Entry ID:                   1019
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        0
Protocol:                           0
Service Flags:                      0x66
Protocol Chain Length:              0

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Chain Entry (32)
Description:                        ASProxy over [MSAFD Tcpip [TCP/IP]]
Provider ID:                        {BA100443-A8EF-4926-8AE4-FEAB70D6277B}
Provider Path:                      C:\Windows\system32\ASProxy.dll
Catalog Entry ID:                   1015
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x66
Protocol Chain Length:              2
Protocol Chain: 1014 : 1002


Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Chain Entry (32)
Description:                        ASProxy over [MSAFD Tcpip [UDP/IP]]
Provider ID:                        {9AB5C2F5-8543-4C5B-A62C-B5DD5B68FE1F}
Provider Path:                      C:\Windows\system32\ASProxy.dll
Catalog Entry ID:                   1016
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x609
Protocol Chain Length:              2
Protocol Chain: 1014 : 1003


Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Chain Entry (32)
Description:                        ASProxy over [MSAFD Tcpip [TCP/IPv6]]
Provider ID:                        {41F7496E-BEFB-4996-8778-CAB9AD35EC28}
Provider Path:                      C:\Windows\system32\ASProxy.dll
Catalog Entry ID:                   1017
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x66
Protocol Chain Length:              2
Protocol Chain: 1014 : 1005


Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Chain Entry (32)
Description:                        ASProxy over [MSAFD Tcpip [UDP/IPv6]]
Provider ID:                        {969B99F2-E54F-4B8C-805D-24983F79CE65}
Provider Path:                      C:\Windows\system32\ASProxy.dll
Catalog Entry ID:                   1018
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x609
Protocol Chain Length:              2
Protocol Chain: 1014 : 1006


Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        AF_UNIX
Provider ID:                        {A00943D9-9C2E-4633-9B59-0057A3160994}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1001
Version:                            2
Address Family:                     1
Max Address Length:                 110
Min Address Length:                 2
Socket Type:                        1
Protocol:                           0
Service Flags:                      0x20026
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        MSAFD Tcpip [TCP/IP]
Provider ID:                        {E70F1AA0-AB8B-11CF-8CA3-00805F48A192}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1002
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x20066
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        MSAFD Tcpip [UDP/IP]
Provider ID:                        {E70F1AA0-AB8B-11CF-8CA3-00805F48A192}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1003
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x20609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        MSAFD Tcpip [RAW/IP]
Provider ID:                        {E70F1AA0-AB8B-11CF-8CA3-00805F48A192}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1004
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        3
Protocol:                           0
Service Flags:                      0x20609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        MSAFD Tcpip [TCP/IPv6]
Provider ID:                        {F9EAB0C0-26D4-11D0-BBBF-00AA006C34E4}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1005
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x20066
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        MSAFD Tcpip [UDP/IPv6]
Provider ID:                        {F9EAB0C0-26D4-11D0-BBBF-00AA006C34E4}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1006
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x20609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        MSAFD Tcpip [RAW/IPv6]
Provider ID:                        {F9EAB0C0-26D4-11D0-BBBF-00AA006C34E4}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1007
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        3
Protocol:                           0
Service Flags:                      0x20609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        RSVP TCPv6 Service Provider
Provider ID:                        {9D60A9E0-337A-11D0-BD88-0000C082E69A}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1008
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x22066
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        RSVP TCP Service Provider
Provider ID:                        {9D60A9E0-337A-11D0-BD88-0000C082E69A}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1009
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        1
Protocol:                           6
Service Flags:                      0x22066
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        RSVP UDPv6 Service Provider
Provider ID:                        {9D60A9E0-337A-11D0-BD88-0000C082E69A}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1010
Version:                            2
Address Family:                     23
Max Address Length:                 28
Min Address Length:                 28
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x22609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        RSVP UDP Service Provider
Provider ID:                        {9D60A9E0-337A-11D0-BD88-0000C082E69A}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1011
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        2
Protocol:                           17
Service Flags:                      0x22609
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        Hyper-V RAW
Provider ID:                        {1234191B-4BF7-4CA7-86E0-DFD7C32B5445}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1012
Version:                            2
Address Family:                     34
Max Address Length:                 36
Min Address Length:                 36
Socket Type:                        1
Protocol:                           1
Service Flags:                      0x20026
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Base Service Provider (32)
Description:                        MSAFD Irda [IrDA]
Provider ID:                        {3972523D-2AF1-11D1-B655-00805F3642CC}
Provider Path:                      %SystemRoot%\system32\mswsock.dll
Catalog Entry ID:                   1013
Version:                            2
Address Family:                     26
Max Address Length:                 32
Min Address Length:                 8
Socket Type:                        1
Protocol:                           1
Service Flags:                      0x20006
Protocol Chain Length:              1

Winsock Catalog Provider Entry
------------------------------------------------------
Entry Type:                         Layered Service Provider (32)
Description:                        ASProxy
Provider ID:                        {E26DCE69-9F96-48B5-9BAC-557D554942DD}
Provider Path:                      C:\Windows\system32\ASProxy.dll
Catalog Entry ID:                   1014
Version:                            2
Address Family:                     2
Max Address Length:                 16
Min Address Length:                 16
Socket Type:                        0
Protocol:                           0
Service Flags:                      0x66
Protocol Chain Length:              0

Name Space Provider Entry
------------------------------------------------------
Description:                        E-mail Naming Shim Provider
Provider ID:                        {964ACBA2-B2BC-40EB-8C6A-A6DB40161CAE}
Name Space:                         37
Active:                             1
Version:                            0


Name Space Provider Entry
------------------------------------------------------
Description:                        PNRP Cloud Namespace Provider
Provider ID:                        {03FE89CE-766D-4976-B9C1-BB9BC42C7B4D}
Name Space:                         39
Active:                             1
Version:                            0


Name Space Provider Entry
------------------------------------------------------
Description:                        PNRP Name Namespace Provider
Provider ID:                        {03FE89CD-766D-4976-B9C1-BB9BC42C7B4D}
Name Space:                         38
Active:                             1
Version:                            0


Name Space Provider Entry
------------------------------------------------------
Description:                        Network Location Awareness Legacy (NLAv1) Namespace
Provider ID:                        {6642243A-3BA8-4AA6-BAA5-2E0BD71FDD83}
Name Space:                         15
Active:                             1
Version:                            0


Name Space Provider Entry
------------------------------------------------------
Description:                        Tcpip
Provider ID:                        {22059D40-7E9E-11CF-AE5A-00AA00A7112B}
Name Space:                         12
Active:                             1
Version:                            0


Name Space Provider Entry
------------------------------------------------------
Description:                        NTDS
Provider ID:                        {3B2637EE-E580-11CF-A555-00C04FD8D4AC}
Name Space:                         32
Active:                             1
Version:                            0


Name Space Provider Entry (32)
------------------------------------------------------
Description:                        E-mail Naming Shim Provider
Provider ID:                        {964ACBA2-B2BC-40EB-8C6A-A6DB40161CAE}
Name Space:                         37
Active:                             1
Version:                            0


Name Space Provider Entry (32)
------------------------------------------------------
Description:                        PNRP Cloud Namespace Provider
Provider ID:                        {03FE89CE-766D-4976-B9C1-BB9BC42C7B4D}
Name Space:                         39
Active:                             1
Version:                            0


Name Space Provider Entry (32)
------------------------------------------------------
Description:                        PNRP Name Namespace Provider
Provider ID:                        {03FE89CD-766D-4976-B9C1-BB9BC42C7B4D}
Name Space:                         38
Active:                             1
Version:                            0


Name Space Provider Entry (32)
------------------------------------------------------
Description:                        Network Location Awareness Legacy (NLAv1) Namespace
Provider ID:                        {6642243A-3BA8-4AA6-BAA5-2E0BD71FDD83}
Name Space:                         15
Active:                             1
Version:                            0


Name Space Provider Entry (32)
------------------------------------------------------
Description:                        Tcpip
Provider ID:                        {22059D40-7E9E-11CF-AE5A-00AA00A7112B}
Name Space:                         12
Active:                             1
Version:                            0


Name Space Provider Entry (32)
------------------------------------------------------
Description:                        NTDS
Provider ID:                        {3B2637EE-E580-11CF-A555-00C04FD8D4AC}
Name Space:                         32
Active:                             1
Version:                            0
```

</details>
Are you using an Astrill VPN? (www.astrill.com) From some Googling, the `asproxy64.dll` appears to be associated with that product, and it's possible that it's hooking into the Windows networking stack in a buggy way that would interfere with Trio's networking. I'm working on reproducing the issue you're seeing, in order to see if there's anything Trio can do to fix it.

If you don't need the VPN, you might find as a temporary workaround that disabling the VPN makes Trio work.
the process and service were not running and disabling the adapter didn't help. the version 3.5.0.2125.
asyncio didn't get this trouble. I guess trio should use windows' native networking stack instead of the third party's

The weird thing is that the call that's failing is the one that's supposed to let you bypass the third party networking stack and use the native windows one instead...

CC @piscisaureus since you might find this report interesting.
Confirmed: I made a fresh Windows 10 VM, installed Python 3.8 and Trio 0.16 and Astrill VPN free trial, and I now get the same error initially reported here, even without launching the Astrill tunnel.

Continuing to dig as to why.
Additional data: With the Astrill LSP installed, `SIO_BASE_HANDLE` fails with the above error. `SIO_BSP_HANDLE_SELECT` returns the same handle you pass in. If I change `_io_windows` to use the `BSP_HANDLE_SELECT` rather than the `BASE_HANDLE`, then in `_refresh_afd`, the `DeviceIoControl(IOCTL_AFD_POLL)` call then fails with `The handle is invalid`.

`select.select()` still works with this LSP installed. Curiouser and curiouser.
Back in https://github.com/python-trio/trio/issues/52#issuecomment-425009562, @piscisaureus wrote:

> since windows vista, select() cannot be intercepted by LSPs unless the LSP does _all_ of the following:
> 
>     * It's a non-IFS lsp
> 
>     * it hooks the SIO_BSP_HANDLE_SELECT ioctl
> 
>     * it installs itself over all protocols.
> 
> 
> I have yet to encounter the first LSP that actually does this.

So I guess this means Astrill's actually does do this weird thing? That is pretty annoying.

For a short-term workaround, it sounds like it's pretty easy to uninstall Astrill's LSP, even without removing Astrill itself: you [hold down the control key, click the "help" button in the Astrill app, and then select "uninstall LSP"](https://wiki.astrill.com/Astrill_Application:Other_menus_and_options#Hold_down_CTRL_and_click_on_Help_menu). And apparently after you do this, Astrill [still works fine](https://steamcommunity.com/discussions/forum/1/1473095331494382588/), so..... it is rather unclear to me why they are even doing this. (Also the way LSPs work, they just don't get loaded into some apps, so any effective VPN *can't* use an LSP for anything critical...)

Ideally of course Trio should Just Work on these systems. That may be a pain in the butt, though, like we might need to resurrect some kind of `select` fallback.
> some kind of `select` fallback

I guess another option would be to figure out a way to bypass these LSPs. For example, if we used the AFD driver directly to allocate our socket object in the first place instead of going through winsock, then it might just work?

I'm not sure how possible or desireable this would be.
The problem is that there are apparently some LSPs that intercept SIO_BASE_HANDLE which they really shouldn't.

I'm going to make a guess and say that the Astrill LSP is also based on Komodia.

Background: https://github.com/tokio-rs/mio/issues/1314#issuecomment-626056585, https://github.com/tokio-rs/mio/issues/1314#issuecomment-626654811

Workaround: https://github.com/piscisaureus/wepoll/blob/0598a791bf9cbbf480793d778930fc635b044980/wepoll.c#L2205-L2253
  
@piscisaureus I translated that wepoll logic to Python and it works beautifully. Thank you so much for the pointer! PR coming soon.

Resolving patch:
diff --git a/trio/_core/_io_windows.py b/trio/_core/_io_windows.py
--- a/trio/_core/_io_windows.py
+++ b/trio/_core/_io_windows.py
@@ -185,7 +185,7 @@ def _check(success):
     return success
 
 
-def _get_base_socket(sock, *, which=WSAIoctls.SIO_BASE_HANDLE):
+def _get_underlying_socket(sock, *, which=WSAIoctls.SIO_BASE_HANDLE):
     if hasattr(sock, "fileno"):
         sock = sock.fileno()
     base_ptr = ffi.new("HANDLE *")
@@ -207,6 +207,53 @@ def _get_base_socket(sock, *, which=WSAIoctls.SIO_BASE_HANDLE):
     return base_ptr[0]
 
 
+def _get_base_socket(sock):
+    # There is a development kit for LSPs called Komodia Redirector.
+    # It does some unusual (some might say evil) things like intercepting
+    # SIO_BASE_HANDLE (fails) and SIO_BSP_HANDLE_SELECT (returns the same
+    # socket) in a misguided attempt to prevent bypassing it. It's been used
+    # in malware including the infamous Lenovo Superfish incident from 2015,
+    # but unfortunately is also used in some legitimate products such as
+    # parental control tools and Astrill VPN. Komodia happens to not
+    # block SIO_BSP_HANDLE_POLL, so we'll try SIO_BASE_HANDLE and fall back
+    # to SIO_BSP_HANDLE_POLL if it doesn't work.
+    # References:
+    # - https://github.com/piscisaureus/wepoll/blob/0598a791bf9cbbf480793d778930fc635b044980/wepoll.c#L2223
+    # - https://github.com/tokio-rs/mio/issues/1314
+
+    while True:
+        try:
+            # If this is not a Komodia-intercepted socket, we can just use
+            # SIO_BASE_HANDLE.
+            return _get_underlying_socket(sock)
+        except OSError as ex:
+            if ex.winerror == ErrorCodes.ERROR_NOT_SOCKET:
+                # SIO_BASE_HANDLE might fail even without LSP intervention,
+                # if we get something that's not a socket.
+                raise
+            if hasattr(sock, "fileno"):
+                sock = sock.fileno()
+            sock = _handle(sock)
+            next_sock = _get_underlying_socket(
+                sock, which=WSAIoctls.SIO_BSP_HANDLE_POLL
+            )
+            if next_sock == sock:
+                # If BSP_HANDLE_POLL returns the same socket we already had,
+                # then there's no layering going on and we need to fail
+                # to prevent an infinite loop.
+                raise RuntimeError(
+                    "Unexpected network configuration detected: "
+                    "SIO_BASE_HANDLE failed and SIO_BSP_HANDLE_POLL didn't "
+                    "return a different socket. Please file a bug at "
+                    "https://github.com/python-trio/trio/issues/new, "
+                    "and include the output of running: "
+                    "netsh winsock show catalog"
+                )
+            # Otherwise we've gotten at least one layer deeper, so
+            # loop back around to keep digging.
+            sock = next_sock
+
+
 def _afd_helper_handle():
     # The "AFD" driver is exposed at the NT path "\Device\Afd". We're using
     # the Win32 CreateFile, though, so we have to pass a Win32 path. \\.\ is
@@ -349,19 +396,41 @@ def __init__(self):
         self._completion_key_counter = itertools.count(CKeys.USER_DEFINED)
 
         with socket.socket() as s:
-            # LSPs can't override this.
-            base_handle = _get_base_socket(s, which=WSAIoctls.SIO_BASE_HANDLE)
+            # We assume we're not working with any LSP that changes
+            # how select() is supposed to work. Validate this by
+            # ensuring that the result of SIO_BSP_HANDLE_SELECT (the
+            # LSP-hookable mechanism for "what should I use for
+            # select()?") matches that of SIO_BASE_HANDLE ("what is
+            # the real non-hooked underlying socket here?").
+            #
+            # This doesn't work for Komodia-based LSPs; see the comments
+            # in _get_base_socket() for details. But we have special
+            # logic for those, so we just skip this check if
+            # SIO_BASE_HANDLE fails.
+
             # LSPs can in theory override this, but we believe that it never
-            # actually happens in the wild.
-            select_handle = _get_base_socket(s, which=WSAIoctls.SIO_BSP_HANDLE_SELECT)
-            if base_handle != select_handle:  # pragma: no cover
-                raise RuntimeError(
-                    "Unexpected network configuration detected. "
-                    "Please file a bug at "
-                    "https://github.com/python-trio/trio/issues/new, "
-                    "and include the output of running: "
-                    "netsh winsock show catalog"
-                )
+            # actually happens in the wild (except Komodia)
+            select_handle = _get_underlying_socket(
+                s, which=WSAIoctls.SIO_BSP_HANDLE_SELECT
+            )
+            try:
+                # LSPs shouldn't override this...
+                base_handle = _get_underlying_socket(s, which=WSAIoctls.SIO_BASE_HANDLE)
+            except OSError:
+                # But Komodia-based LSPs do anyway, in a way that causes
+                # a failure with WSAEFAULT. We have special handling for
+                # them in _get_base_socket(). Make sure it works.
+                _get_base_socket(s)
+            else:
+                if base_handle != select_handle:
+                    raise RuntimeError(
+                        "Unexpected network configuration detected: "
+                        "SIO_BASE_HANDLE and SIO_BSP_HANDLE_SELECT differ. "
+                        "Please file a bug at "
+                        "https://github.com/python-trio/trio/issues/new, "
+                        "and include the output of running: "
+                        "netsh winsock show catalog"
+                    )
 
     def close(self):
         try:
diff --git a/trio/_core/_windows_cffi.py b/trio/_core/_windows_cffi.py
--- a/trio/_core/_windows_cffi.py
+++ b/trio/_core/_windows_cffi.py
@@ -244,6 +244,7 @@ class ErrorCodes(enum.IntEnum):
     ERROR_INVALID_HANDLE = 6
     ERROR_INVALID_PARMETER = 87
     ERROR_NOT_FOUND = 1168
+    ERROR_NOT_SOCKET = 10038
 
 
 class FileFlags(enum.IntEnum):
@@ -283,6 +284,7 @@ class AFDPollFlags(enum.IntFlag):
 class WSAIoctls(enum.IntEnum):
     SIO_BASE_HANDLE = 0x48000022
     SIO_BSP_HANDLE_SELECT = 0x4800001C
+    SIO_BSP_HANDLE_POLL = 0x4800001D
 
 
 class CompletionModes(enum.IntFlag):


Test patch:
diff --git a/trio/_core/tests/test_windows.py b/trio/_core/tests/test_windows.py
--- a/trio/_core/tests/test_windows.py
+++ b/trio/_core/tests/test_windows.py
@@ -171,3 +171,47 @@ async def test_too_late_to_cancel():
         # fallback completion that was posted when CancelIoEx failed.
         assert await _core.readinto_overlapped(read_handle, target) == 6
         assert target[:6] == b"test2\n"
+
+
+def test_lsp_that_hooks_select_gives_good_error(monkeypatch):
+    from .._windows_cffi import WSAIoctls, _handle
+    from .. import _io_windows
+
+    def patched_get_underlying(sock, *, which=WSAIoctls.SIO_BASE_HANDLE):
+        if hasattr(sock, "fileno"):  # pragma: no branch
+            sock = sock.fileno()
+        if which == WSAIoctls.SIO_BSP_HANDLE_SELECT:
+            return _handle(sock + 1)
+        else:
+            return _handle(sock)
+
+    monkeypatch.setattr(_io_windows, "_get_underlying_socket", patched_get_underlying)
+    with pytest.raises(
+        RuntimeError, match="SIO_BASE_HANDLE and SIO_BSP_HANDLE_SELECT differ"
+    ):
+        _core.run(sleep, 0)
+
+
+def test_lsp_that_completely_hides_base_socket_gives_good_error(monkeypatch):
+    # This tests behavior with an LSP that fails SIO_BASE_HANDLE and returns
+    # self for SIO_BSP_HANDLE_SELECT (like Komodia), but also returns
+    # self for SIO_BSP_HANDLE_POLL. No known LSP does this, but we want to
+    # make sure we get an error rather than an infinite loop.
+
+    from .._windows_cffi import WSAIoctls, _handle
+    from .. import _io_windows
+
+    def patched_get_underlying(sock, *, which=WSAIoctls.SIO_BASE_HANDLE):
+        if hasattr(sock, "fileno"):  # pragma: no branch
+            sock = sock.fileno()
+        if which == WSAIoctls.SIO_BASE_HANDLE:
+            raise OSError("nope")
+        else:
+            return _handle(sock)
+
+    monkeypatch.setattr(_io_windows, "_get_underlying_socket", patched_get_underlying)
+    with pytest.raises(
+        RuntimeError,
+        match="SIO_BASE_HANDLE failed and SIO_BSP_HANDLE_POLL didn't return a diff",
+    ):
+        _core.run(sleep, 0)