Title: WinError 10038 is raised when loop.sock_connect is wrapped with asyncio.wait_for
Type: behavior Stage:
Components: asyncio Versions: Python 3.6
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Alisue Lambda, asvetlov, yjqiang, yselivanov
Priority: normal Keywords:

Created on 2018-04-24 16:51 by Alisue Lambda, last changed 2019-03-14 09:05 by yjqiang.

File name Uploaded Description Edit Alisue Lambda, 2018-04-24 16:51 A minimal script to reproduce the issue on Windows 10 Pro
Messages (7)
msg315707 - (view) Author: Alisue Lambda (Alisue Lambda) Date: 2018-04-24 16:51
This is my first time to create an issue on the python bug tracker so let me know if I don't follow the rule which I need to follow.

# Summary

Using 'loop.sock_connect' with 'asyncio.wait_for' raises 'OSError [WinError 10038]' in Windows 10 Pro when timed-out.

# Detail

I use 'loop.sock_connect' to establish a TCP connection for checking if a particular port on a target host is available.
However, when I wrap the coroutine with 'asyncio.wait_for', the following exception is raised when the wrapped coroutine has timed-out.

    Traceback (most recent call last):
      File "C:\Users\alisue/", line 41, in <module>
      File "C:\Python36\lib\asyncio\", line 454, in run_until_complete
      File "C:\Python36\lib\asyncio\", line 421, in run_forever
      File "C:\Python36\lib\asyncio\", line 1395, in _run_once
        event_list =
      File "C:\Python36\lib\", line 323, in select
        r, w, _ = self._select(self._readers, self._writers, [], timeout)
      File "C:\Python36\lib\", line 314, in _select
        r, w, x =, w, w, timeout)
    OSError: [WinError 10038] ...........

While it is raised from 'lib\', I cannot catch this exception so the event loop has halted.

The attached '' is a minimum script to reproduce the error.

msg315708 - (view) Author: Alisue Lambda (Alisue Lambda) Date: 2018-04-24 16:55
I should have mentioned that the script works well on macOS and Linux.
This issue exists only on Windows.
msg315709 - (view) Author: Alisue Lambda (Alisue Lambda) Date: 2018-04-24 17:21
I've found an workaround. The point is that 'with s' should be included in a coroutine which will be timed-out.

    import asyncio
    import socket

    ADDR = ('', 22)

    async def check(loop):
        s = socket.socket(socket.AF_INET,
        with s:
            await loop.sock_connect(s, ADDR)

    async def test(loop):
            await asyncio.wait_for(
        except Exception as e:
            print('Fail: %s' % e)

    if __name__ == '__main__':
        loop = asyncio.get_event_loop()
msg323238 - (view) Author: Alisue Lambda (Alisue Lambda) Date: 2018-08-07 09:24
I use the following patch to fix the behavior in Windows.

import sys

if sys.platform != 'win32':
    def patch():
    def patch():
        """Patch selectors.SelectSelector to fix WinError 10038 in Windows


        import select
        from selectors import SelectSelector

        def _select(self, r, w, _, timeout=None):
                r, w, x =, w, w, timeout)
            except OSError as e:
                if hasattr(e, 'winerror') and e.winerror == 10038:
                    # descriptors may already be closed
                    return [], [], []
                return r, w + x, []

        SelectSelector._select = _select
msg332649 - (view) Author: Alisue Lambda (Alisue Lambda) Date: 2018-12-28 09:23

It seems the PR above which has not merged solve the issue.
msg332650 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-12-28 10:14
What Python version do you use?
msg337905 - (view) Author: yjq (yjqiang) Date: 2019-03-14 09:05
I'm using python 3.7.2. And I met the same problem.
Date User Action Args
2019-03-14 09:05:47yjqiangsetnosy: + yjqiang
messages: + msg337905
2018-12-28 10:14:13asvetlovsetmessages: + msg332650
2018-12-28 09:23:51Alisue Lambdasetmessages: + msg332649
2018-08-07 09:24:40Alisue Lambdasetmessages: + msg323238
2018-04-24 17:21:25Alisue Lambdasetmessages: + msg315709
2018-04-24 16:55:16Alisue Lambdasetmessages: + msg315708
2018-04-24 16:51:15Alisue Lambdacreate