Repository navigation
Fix REDIS_OPT_SET_SOCK_CLOEXEC colliding with REDIS_REUSEADDR - #1395
RaphaelFakhri wants to merge 2 commits into
Conversation
redisConnectWithOptions stored the REDIS_OPT_SET_SOCK_CLOEXEC option bit (0x80) directly in c->flags, where 0x80 is already REDIS_REUSEADDR. As a result REDIS_OPT_REUSEADDR also created the socket with SOCK_CLOEXEC, and REDIS_OPT_SET_SOCK_CLOEXEC also set SO_REUSEADDR when binding a source address. Add a dedicated REDIS_SET_SOCK_CLOEXEC context flag and use it when creating sockets.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 43358c885e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| redisFree(c); | ||
|
|
||
| options.options = REDIS_OPT_SET_SOCK_CLOEXEC; | ||
| options.endpoint.tcp.source_addr = cfg.tcp.host; |
There was a problem hiding this comment.
Avoid binding the test client to the server address
When hiredis-test -h <remote-host> is used, the parser at test.c:2722-2724 stores that remote endpoint in cfg.tcp.host, and this line also asks hiredis to bind the client socket to that address. Because the remote address normally is not assigned to the test machine, bind() in _redisContextConnectTcp fails with EADDRNOTAVAIL, so the following assertion aborts before the socket-flag checks; use a known-local source address or make this bind-dependent case conditional instead.
Useful? React with 👍 / 👎.
test_tcp_socket_flags bound the second connection to the server host, which fails with EADDRNOTAVAIL when hiredis-test runs against a remote server. Use the local address of the first connection instead.
redisConnectWithOptionsstored theREDIS_OPT_SET_SOCK_CLOEXECoption bit (0x80) directly inc->flags, where0x80is alreadyREDIS_REUSEADDR. As a result:REDIS_OPT_REUSEADDRalso created the socket withSOCK_CLOEXEC.REDIS_OPT_SET_SOCK_CLOEXECalso setSO_REUSEADDRwhen binding a source address.Changes
REDIS_SET_SOCK_CLOEXEC(0x2000) context flag next to the other context flags inhiredis.h.redisConnectWithOptionsand test it inredisCreateSocketand_redisContextConnectTcp.test_tcp_socket_flags, which checksFD_CLOEXECandSO_REUSEADDRon sockets created with each option. It only runs whereSOCK_CLOEXECis defined.The public
REDIS_OPT_*values do not change.Testing
make hiredis-test && ./test.shon Linux:REDIS_OPT_REUSEADDR does not set FD_CLOEXECandREDIS_OPT_SET_SOCK_CLOEXEC does not set SO_REUSEADDRfail (2 failures).Fixes #1394
Note
Low Risk
Corrects socket flag bookkeeping only; no API or auth changes. Residual risk is limited to connection/socket behavior edge cases on systems with
SOCK_CLOEXEC.Overview
Fix:
redisConnectWithOptionsno longer storesREDIS_OPT_SET_SOCK_CLOEXEConc->flagsusing a bit that overlappedREDIS_REUSEADDR(0x80). A dedicated internal flagREDIS_SET_SOCK_CLOEXEC(0x2000) is defined and set from the option; publicREDIS_OPT_*values are unchanged.Implementation:
redisCreateSocketand_redisContextConnectTcpnow gateSOCK_CLOEXEConREDIS_SET_SOCK_CLOEXECinstead of the opt bit.Tests: On platforms with
SOCK_CLOEXEC,test_tcp_socket_flagsassertsREDIS_OPT_REUSEADDRdoes not setFD_CLOEXEC, andREDIS_OPT_SET_SOCK_CLOEXECsetsFD_CLOEXECwithout enablingSO_REUSEADDR; non-Win includes gainfcntl.hfor those checks.Reviewed by Cursor Bugbot for commit 1d80534. Bugbot is set up for automated code reviews on this repo. Configure here.