Avoid per-check fd allocation in hiredis _socket_can_read() — use poll() instead of a per-call selector#4118
Open
violuke wants to merge 1 commit into
Open
Avoid per-check fd allocation in hiredis _socket_can_read() — use poll() instead of a per-call selector#4118violuke wants to merge 1 commit into
_socket_can_read() — use poll() instead of a per-call selector#4118violuke wants to merge 1 commit into
Conversation
…_read()` — use `poll()` instead of a per-call selector
|
Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset. In case there are security findings, they will be communicated to you as a comment inside the PR. Hope you’ll enjoy using Jit. Questions? Comments? Want to learn more? Get in touch with us. |
🛡️ Jit Security Scan Results✅ No security findings were detected in this PR
Security scan by Jit
|
Collaborator
|
Hi @violuke, thanks for the contribution! I'll take a closer look on Monday. I had completely missed the fact that Thanks for pushing this forward! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #4115, which fixed the
ValueError: filedescriptor out of range in select()in the hiredis parser by replacingselect.select()withselectors.DefaultSelector(). This relates also to #4117The selector approach has a residual failure mode:
DefaultSelectorresolves toEpollSelectoron Linux andKqueueSelectoron macOS, both of which allocate a new file descriptor on every call._socket_can_read()constructs a fresh selector per readiness check, andConnection.can_read()runs on every connection acquisition — so every command now allocates (and frees) an fd just to check readiness.This matters because fd pressure is exactly the condition that triggers this bug class: a socket only lands on fd >= 1024 in a process holding many descriptors. In a process at or near
RLIMIT_NOFILE, the readiness check itself now fails withOSError: [Errno 24] Too many open files(raised fromepoll_create/kqueueinsideselectors.py), so a worker that runs out of fds loses the ability to talk to Redis entirely even though its connection socket is healthy.This PR switches
_socket_can_read()toselect.poll(), which:FD_SETSIZElimit (the original bug stays fixed);Behaviour is preserved:
timeoutseconds is converted to poll's milliseconds,timeout=0remains a non-blocking check andtimeout=Noneblocks forever;poll()always reportsPOLLHUP/POLLERR/POLLNVALregardless of the registered mask, so a closed or errored socket still counts as "readable", matching the previousselect()/selector semantics. On Windows, whereselect.pollis unavailable, theselectors.DefaultSelectorpath from #4115 is retained as the fallback (Windows is not subject to the UnixFD_SETSIZEvalue limit).The async hiredis parser is unaffected — it reads via asyncio streams and performs no
select/pollcalls.Changes
redis/_parsers/hiredis.py:_socket_can_read()prefersselect.poll()when available; the selector-based check remains as the Windows fallback.tests/test_connection.py:test_hiredis_socket_can_read_uses_poll(parametrized) — verifies the poll path and the seconds-to-milliseconds timeout conversion for0,0.001, andNone.test_hiredis_socket_can_read_falls_back_to_default_selector— the existing selector test from Fix hiredis readiness checks for high file descriptors #4115, updated to patch_HAS_POLL = Falseso it exercises the fallback path.test_hiredis_socket_can_read_handles_high_file_descriptor— kept from Fix hiredis readiness checks for high file descriptors #4115; skip condition updated for the new branch order.test_hiredis_socket_can_read_under_fd_exhaustion(new) — lowersRLIMIT_NOFILE, fills the fd table viaos.dup()untilEMFILE, then asserts readiness checks still work for both a readable and an empty socket pair. This test fails withOSError: [Errno 24]against currentmasterand passes with this change.How tested
can_readunit tests pass (none require a running server).redis/_parsers/hiredis.pyreverted tomaster, it fails withOSError: [Errno 24] Too many open filesraised fromselectors.py; with this change it passes.ruff check,ruff format --check, andvulture redis whitelist.py --min-confidence 80are clean.Note
Medium Risk
Touches the hot-path readiness check on every hiredis
can_read(); behavior is intended to match prior semantics but poll vs epoll edge cases and Windows fallback warrant careful review.Overview
Hiredis socket readiness now uses
select.poll()on platforms where it exists, instead of creating a freshselectors.DefaultSelector()on every_socket_can_read()call. That keeps the #4115 fix (noFD_SETSIZE/ high-fdselectfailures) while avoiding epoll/kqueue per-check fd allocation, which could raiseEMFILEwhen the process is already fd-starved—the same situation that pushes Redis sockets onto high fds. Windows still uses the selector fallback via_HAS_POLL.Tests now assert the poll path (including timeout → ms conversion), force the selector path with
_HAS_POLL = False, and addtest_hiredis_socket_can_read_under_fd_exhaustionto prove readiness checks still work when the fd table is full.Reviewed by Cursor Bugbot for commit 5347b79. Bugbot is set up for automated code reviews on this repo. Configure here.