Skip to content

Commit 2b91d4e

Browse files
author
Niv Yehezkel
committed
connection: replace select by poll to support fds higher than 1024
1 parent 3fb3256 commit 2b91d4e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

aiopg/connection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ def _ready(weak_self):
110110
except (psycopg2.Warning, psycopg2.Error) as exc:
111111
if self._fileno is not None:
112112
try:
113-
select.select([self._fileno], [], [], 0)
113+
poll = select.poll()
114+
poll.register(self._fileno)
115+
poll.poll(0)
116+
poll.unregister()
114117
except OSError as os_exc:
115118
if _is_bad_descriptor_error(os_exc):
116119
with contextlib.suppress(OSError):

tests/test_connection.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,25 @@ async def test_connection_on_server_restart(connect, pg_server, docker):
642642
delay *= 2
643643
else:
644644
pytest.fail("Cannot connect to the restarted server")
645+
646+
647+
async def test_no_poll_error_on_high_fd(connect):
648+
# The connection file descriptor when higher than 1024 should not raise any
649+
# further exception when OperationalError is raised
650+
conn = await connect()
651+
high_fd = 1025
652+
653+
impl = mock.Mock()
654+
exc = psycopg2.OperationalError('Test')
655+
impl.poll.side_effect = exc
656+
conn._conn = impl
657+
conn._fileno = high_fd
658+
659+
m_remove_reader = mock.Mock()
660+
conn._loop.remove_reader = m_remove_reader
661+
662+
conn._ready(conn._weakref)
663+
assert not m_remove_reader.called
664+
665+
conn.close()
666+
assert m_remove_reader.called_with(high_fd)

0 commit comments

Comments
 (0)