Skip to content

Fix REDIS_OPT_SET_SOCK_CLOEXEC colliding with REDIS_REUSEADDR - #1395

Open
RaphaelFakhri wants to merge 2 commits into
redis:masterfrom
RaphaelFakhri:fix-sock-cloexec-flag-collision
Open

RaphaelFakhri wants to merge 2 commits into
redis:masterfrom
RaphaelFakhri:fix-sock-cloexec-flag-collision

Conversation

@RaphaelFakhri

@RaphaelFakhri RaphaelFakhri commented Nov 5, 2026 •

Copy link
Copy Markdown

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.
  • REDIS_OPT_SET_SOCK_CLOEXEC also set SO_REUSEADDR when binding a source address.

Changes

  • Add a dedicated REDIS_SET_SOCK_CLOEXEC (0x2000) context flag next to the other context flags in hiredis.h.
  • Set it from redisConnectWithOptions and test it in redisCreateSocket and _redisContextConnectTcp.
  • Add test_tcp_socket_flags, which checks FD_CLOEXEC and SO_REUSEADDR on sockets created with each option. It only runs where SOCK_CLOEXEC is defined.

The public REDIS_OPT_* values do not change.

Testing

make hiredis-test && ./test.sh on Linux:

  • Without the fix: REDIS_OPT_REUSEADDR does not set FD_CLOEXEC and REDIS_OPT_SET_SOCK_CLOEXEC does not set SO_REUSEADDR fail (2 failures).
  • With the fix: all tests pass.

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: redisConnectWithOptions no longer stores REDIS_OPT_SET_SOCK_CLOEXEC on c->flags using a bit that overlapped REDIS_REUSEADDR (0x80). A dedicated internal flag REDIS_SET_SOCK_CLOEXEC (0x2000) is defined and set from the option; public REDIS_OPT_* values are unchanged.

Implementation: redisCreateSocket and _redisContextConnectTcp now gate SOCK_CLOEXEC on REDIS_SET_SOCK_CLOEXEC instead of the opt bit.

Tests: On platforms with SOCK_CLOEXEC, test_tcp_socket_flags asserts REDIS_OPT_REUSEADDR does not set FD_CLOEXEC, and REDIS_OPT_SET_SOCK_CLOEXEC sets FD_CLOEXEC without enabling SO_REUSEADDR; non-Win includes gain fcntl.h for those checks.

Reviewed by Cursor Bugbot for commit 1d80534. Bugbot is set up for automated code reviews on this repo. Configure here.

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.
@github-actions github-actions Bot added the bug label Nov 5, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T11:15:25.740848Z 43358c8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread test.c Outdated
redisFree(c);

options.options = REDIS_OPT_SET_SOCK_CLOEXEC;
options.endpoint.tcp.source_addr = cfg.tcp.host;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

REDIS_OPT_SET_SOCK_CLOEXEC collides with REDIS_REUSEADDR in the context flags

1 participant