Skip to content

Commit 9f9fe02

Browse files
committed
Fix: connection_acquisition_timeout now covers TLS handshake
1 parent 7dfc13b commit 9f9fe02

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/neo4j/_async/io/_bolt_socket.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,11 @@ async def connect(
326326
s = None
327327
try:
328328
s = await cls._connect_secure(
329-
resolved_address, tcp_timeout, keep_alive, ssl_context
329+
resolved_address,
330+
tcp_timeout,
331+
deadline,
332+
keep_alive,
333+
ssl_context,
330334
)
331335
agreed_version, handshake, response = await s._handshake(
332336
resolved_address, deadline

src/neo4j/_async_compat/network/_bolt_socket.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,14 @@ def kill(self):
170170

171171
@classmethod
172172
async def _connect_secure(
173-
cls, resolved_address, timeout, keep_alive, ssl_context
173+
cls, resolved_address, timeout, deadline, keep_alive, ssl_context
174174
) -> te.Self:
175175
"""
176176
Connect to the address and return the socket.
177177
178178
:param resolved_address:
179179
:param timeout: seconds
180+
:param deadline: deadline for the whole operation
180181
:param keep_alive: True or False
181182
:param ssl_context: SSLContext or None
182183
@@ -215,8 +216,9 @@ async def _connect_secure(
215216
loop=loop,
216217
)
217218
protocol = asyncio.StreamReaderProtocol(reader, loop=loop)
218-
transport, _ = await loop.create_connection(
219-
lambda: protocol, sock=s, **ssl_kwargs
219+
transport, _ = await wait_for(
220+
loop.create_connection(lambda: protocol, sock=s, **ssl_kwargs),
221+
deadline.to_timeout(),
220222
)
221223
writer = asyncio.StreamWriter(transport, protocol, reader, loop)
222224

@@ -374,13 +376,14 @@ def kill(self):
374376

375377
@classmethod
376378
def _connect_secure(
377-
cls, resolved_address, timeout, keep_alive, ssl_context
379+
cls, resolved_address, timeout, deadline, keep_alive, ssl_context
378380
):
379381
"""
380382
Connect to the address and return the socket.
381383
382384
:param resolved_address:
383385
:param timeout: seconds
386+
:param deadline: deadline for the whole operation
384387
:param keep_alive: True or False
385388
:returns: socket object
386389
"""
@@ -436,7 +439,11 @@ def _connect_secure(
436439
sni_host = hostname if HAS_SNI and hostname else None
437440
log.debug("[#%04X] C: <SECURE> %s", local_port, hostname)
438441
try:
442+
t = s.gettimeout()
443+
if timeout:
444+
s.settimeout(deadline.to_timeout())
439445
s = ssl_context.wrap_socket(s, server_hostname=sni_host)
446+
s.settimeout(t)
440447
except (OSError, SSLError, CertificateError) as cause:
441448
raise BoltSecurityError(
442449
message="Failed to establish encrypted connection.",

src/neo4j/_sync/io/_bolt_socket.py

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)