Skip to content

Commit 2eb8533

Browse files
authored
connect_timeout fix (#360)
* fix the python 3.7 compatibility * Fixes #257
1 parent f712904 commit 2eb8533

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

aiomysql/connection.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,21 @@ async def _connect(self):
475475
try:
476476
if self._unix_socket and self._host in ('localhost', '127.0.0.1'):
477477
self._reader, self._writer = await \
478-
asyncio.open_unix_connection(self._unix_socket,
479-
loop=self._loop)
478+
asyncio.wait_for(
479+
asyncio.open_unix_connection(
480+
self._unix_socket,
481+
loop=self._loop),
482+
timeout=self.connect_timeout)
480483
self.host_info = "Localhost via UNIX socket: " + \
481484
self._unix_socket
482485
else:
483486
self._reader, self._writer = await \
484-
asyncio.open_connection(self._host, self._port,
485-
loop=self._loop)
487+
asyncio.wait_for(
488+
asyncio.open_connection(
489+
self._host,
490+
self._port,
491+
loop=self._loop),
492+
timeout=self.connect_timeout)
486493
self._set_keep_alive()
487494
self.host_info = "socket %s:%d" % (self._host, self._port)
488495

tests/test_connection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def fill_my_cnf(self):
2222
with open(path2, 'w') as f2:
2323
f2.write(tmpl.format_map(self.__dict__))
2424

25+
@run_until_complete
26+
def test_connect_timeout(self):
27+
# All exceptions are caught and raised as operational errors
28+
with self.assertRaises(aiomysql.OperationalError):
29+
yield from self.connect(connect_timeout=0.000000000001)
30+
2531
@run_until_complete
2632
def test_config_file(self):
2733
self.fill_my_cnf()

0 commit comments

Comments
 (0)