Skip to content

Commit

Permalink
connection: replace select by poll to support fds higher than 1024
Browse files Browse the repository at this point in the history
  • Loading branch information
Niv Yehezkel committed Apr 5, 2020
1 parent 3fb3256 commit 8f597ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion aiopg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ def _ready(weak_self):
except (psycopg2.Warning, psycopg2.Error) as exc:
if self._fileno is not None:
try:
select.select([self._fileno], [], [], 0)
poll = select.poll()
poll.register(self._fileno, select.POLLIN |
select.POLLPRI | select.POLLOUT)
poll.poll()
poll.unregister()
except OSError as os_exc:
if _is_bad_descriptor_error(os_exc):
with contextlib.suppress(OSError):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,25 @@ async def test_connection_on_server_restart(connect, pg_server, docker):
delay *= 2
else:
pytest.fail("Cannot connect to the restarted server")


async def test_no_poll_error_on_high_fd(connect):
# The connection file descriptor when higher than 1024 should not raise any
# further exception when OperationalError is raised
conn = await connect()
high_fd = 1025

impl = mock.Mock()
exc = psycopg2.OperationalError('Test')
impl.poll.side_effect = exc
conn._conn = impl
conn._fileno = high_fd

m_remove_reader = mock.Mock()
conn._loop.remove_reader = m_remove_reader

conn._ready(conn._weakref)
assert not m_remove_reader.called

conn.close()
assert m_remove_reader.called_with(high_fd)

0 comments on commit 8f597ab

Please sign in to comment.