@@ -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." ,
0 commit comments