Skip to content

Commit f7bb39e

Browse files
committed
PYTHON-5021 - Fix usages of getaddrinfo to be non-blocking
1 parent ecf7ac7 commit f7bb39e

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

pymongo/asynchronous/auth.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Authentication helpers."""
1616
from __future__ import annotations
1717

18+
import asyncio
1819
import functools
1920
import hashlib
2021
import hmac
@@ -177,15 +178,28 @@ def _auth_key(nonce: str, username: str, password: str) -> str:
177178
return md5hash.hexdigest()
178179

179180

180-
def _canonicalize_hostname(hostname: str, option: str | bool) -> str:
181+
async def _canonicalize_hostname(hostname: str, option: str | bool) -> str:
181182
"""Canonicalize hostname following MIT-krb5 behavior."""
182183
# https://github.com/krb5/krb5/blob/d406afa363554097ac48646a29249c04f498c88e/src/util/k5test.py#L505-L520
183184
if option in [False, "none"]:
184185
return hostname
185186

186-
af, socktype, proto, canonname, sockaddr = socket.getaddrinfo(
187-
hostname, None, 0, 0, socket.IPPROTO_TCP, socket.AI_CANONNAME
188-
)[0]
187+
if not _IS_SYNC:
188+
loop = asyncio.get_event_loop()
189+
af, socktype, proto, canonname, sockaddr = (
190+
await loop.getaddrinfo(
191+
hostname,
192+
None,
193+
family=0,
194+
type=0,
195+
proto=socket.IPPROTO_TCP,
196+
flags=socket.AI_CANONNAME,
197+
)
198+
)[0] # type: ignore[index]
199+
else:
200+
af, socktype, proto, canonname, sockaddr = socket.getaddrinfo(
201+
hostname, None, 0, 0, socket.IPPROTO_TCP, socket.AI_CANONNAME
202+
)[0]
189203

190204
# For forward just to resolve the cname as dns.lookup() will not return it.
191205
if option == "forward":
@@ -213,7 +227,7 @@ async def _authenticate_gssapi(credentials: MongoCredential, conn: AsyncConnecti
213227
# Starting here and continuing through the while loop below - establish
214228
# the security context. See RFC 4752, Section 3.1, first paragraph.
215229
host = props.service_host or conn.address[0]
216-
host = _canonicalize_hostname(host, props.canonicalize_host_name)
230+
host = await _canonicalize_hostname(host, props.canonicalize_host_name)
217231
service = props.service_name + "@" + host
218232
if props.service_realm is not None:
219233
service = service + "@" + props.service_realm

pymongo/asynchronous/pool.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ def __repr__(self) -> str:
783783
)
784784

785785

786-
def _create_connection(address: _Address, options: PoolOptions) -> socket.socket:
786+
async def _create_connection(address: _Address, options: PoolOptions) -> socket.socket:
787787
"""Given (host, port) and PoolOptions, connect and return a socket object.
788788
789789
Can raise socket.error.
@@ -814,7 +814,14 @@ def _create_connection(address: _Address, options: PoolOptions) -> socket.socket
814814
family = socket.AF_UNSPEC
815815

816816
err = None
817-
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
817+
if not _IS_SYNC:
818+
loop = asyncio.get_event_loop()
819+
results = await loop.getaddrinfo( # type: ignore[assignment]
820+
host, port, family=family, type=socket.SOCK_STREAM
821+
)
822+
else:
823+
results = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM) # type: ignore[assignment]
824+
for res in results: # type: ignore[attr-defined]
818825
af, socktype, proto, dummy, sa = res
819826
# SOCK_CLOEXEC was new in CPython 3.2, and only available on a limited
820827
# number of platforms (newer Linux and *BSD). Starting with CPython 3.4
@@ -863,7 +870,7 @@ async def _configured_socket(
863870
864871
Sets socket's SSL and timeout options.
865872
"""
866-
sock = _create_connection(address, options)
873+
sock = await _create_connection(address, options)
867874
ssl_context = options._ssl_context
868875

869876
if ssl_context is None:

pymongo/synchronous/auth.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Authentication helpers."""
1616
from __future__ import annotations
1717

18+
import asyncio
1819
import functools
1920
import hashlib
2021
import hmac
@@ -180,9 +181,22 @@ def _canonicalize_hostname(hostname: str, option: str | bool) -> str:
180181
if option in [False, "none"]:
181182
return hostname
182183

183-
af, socktype, proto, canonname, sockaddr = socket.getaddrinfo(
184-
hostname, None, 0, 0, socket.IPPROTO_TCP, socket.AI_CANONNAME
185-
)[0]
184+
if not _IS_SYNC:
185+
loop = asyncio.get_event_loop()
186+
af, socktype, proto, canonname, sockaddr = (
187+
loop.getaddrinfo(
188+
hostname,
189+
None,
190+
family=0,
191+
type=0,
192+
proto=socket.IPPROTO_TCP,
193+
flags=socket.AI_CANONNAME,
194+
)
195+
)[0] # type: ignore[index]
196+
else:
197+
af, socktype, proto, canonname, sockaddr = socket.getaddrinfo(
198+
hostname, None, 0, 0, socket.IPPROTO_TCP, socket.AI_CANONNAME
199+
)[0]
186200

187201
# For forward just to resolve the cname as dns.lookup() will not return it.
188202
if option == "forward":

pymongo/synchronous/pool.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,14 @@ def _create_connection(address: _Address, options: PoolOptions) -> socket.socket
812812
family = socket.AF_UNSPEC
813813

814814
err = None
815-
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
815+
if not _IS_SYNC:
816+
loop = asyncio.get_event_loop()
817+
results = loop.getaddrinfo( # type: ignore[assignment]
818+
host, port, family=family, type=socket.SOCK_STREAM
819+
)
820+
else:
821+
results = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM) # type: ignore[assignment]
822+
for res in results: # type: ignore[attr-defined]
816823
af, socktype, proto, dummy, sa = res
817824
# SOCK_CLOEXEC was new in CPython 3.2, and only available on a limited
818825
# number of platforms (newer Linux and *BSD). Starting with CPython 3.4

test/asynchronous/test_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ async def test_gssapi_threaded(self):
275275
async def test_gssapi_canonicalize_host_name(self):
276276
# Test the low level method.
277277
assert GSSAPI_HOST is not None
278-
result = _canonicalize_hostname(GSSAPI_HOST, "forward")
278+
result = await _canonicalize_hostname(GSSAPI_HOST, "forward")
279279
if "compute-1.amazonaws.com" not in result:
280280
self.assertEqual(result, GSSAPI_HOST)
281-
result = _canonicalize_hostname(GSSAPI_HOST, "forwardAndReverse")
281+
result = await _canonicalize_hostname(GSSAPI_HOST, "forwardAndReverse")
282282
self.assertEqual(result, GSSAPI_HOST)
283283

284284
# Use the equivalent named CANONICALIZE_HOST_NAME.

0 commit comments

Comments
 (0)