Skip to content

Commit

Permalink
Raise an exception if the loop is the wrong type on Windows
Browse files Browse the repository at this point in the history
Fixes: #86
Closes: #105
  • Loading branch information
saghul committed Oct 7, 2023
1 parent 414b525 commit 7569a4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ The API is pretty simple, three functions are provided in the ``DNSResolver`` cl
``ARES_ECANCELLED`` errno.


Note for Windows users
======================

This library requires the asyncio loop to be a `SelectorEventLoop`, which is not the default on Windows since
Python 3.8.

The default can be changed as follows (do this very early in your application):

.. code:: python
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
This may have other implications for the rest of your codebase, so make sure to test thoroughly.


Running the test suite
======================

Expand Down
6 changes: 5 additions & 1 deletion aiodns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import functools
import pycares
import socket
import sys

from typing import (
Any,
List,
Optional,
Set,
Sequence
Expand Down Expand Up @@ -50,6 +50,10 @@ def __init__(self, nameservers: Optional[Sequence[str]] = None,
**kwargs: Any) -> None:
self.loop = loop or asyncio.get_event_loop()
assert self.loop is not None
if sys.platform == 'win32':
if not isinstance(self.loop, asyncio.SelectorEventLoop):
raise RuntimeError(
'aiodns needs a SelectorEventLoop on Windows. See more: https://github.com/saghul/aiodns/issues/86')
kwargs.pop('sock_state_cb', None)
self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb, **kwargs)
if nameservers:
Expand Down

0 comments on commit 7569a4b

Please sign in to comment.