Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use select.poll instead of select.select to support fds higher than 1024 #668

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion aiopg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ 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)
poll.poll(0)
poll.unregister(self._fileno)
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)