Skip to content

Check SO_ERROR when a connect() retry answers EISCONN on macOS - #1401

Merged
uglide merged 1 commit into
masterfrom
im/fix-eisconn-refused-connect
Nov 18, 2026
Merged

uglide merged 1 commit into
masterfrom
im/fix-eisconn-refused-connect

Conversation

@uglide

@uglide uglide commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

redisCheckConnectDone() finds out how a non-blocking connect ended by calling connect() again, and maps EISCONN to "connected". Since macOS 26.7 / 15.8.1 (and on macOS 27), that retry returns EISCONN for a refused connect too, while SO_ERROR still holds ECONNREFUSED:

connect()                  -> -1 EINPROGRESS
kevent                     -> EVFILT_WRITE, EV_EOF, fflags=61 (ECONNREFUSED)
connect() again            -> -1 EISCONN            <- hiredis takes this as success
getsockopt(SO_ERROR)       -> 61 (ECONNREFUSED)

(macOS 26.7.2 25G332 and 15.8.2 24H232 on the GitHub runners. On 26.6.2 25G83 the retry still returned ECONNREFUSED.)

So hiredis treats the dead socket as connected. Setting TCP_NODELAY then fails with EINVAL, redisSetTcpNoDelay() closes the fd, and the caller sees setsockopt(TCP_NODELAY): Invalid argument instead of Connection refused. This is why test.c #71 and #72 fail on the current macOS runner (e.g. in #1400). The async API takes the same path: the connect callback runs after the fd has been closed and while the adapter still watches that fd number, which is how we ran into #1399.

Other projects hit the same change and fixed it the same way, by reading SO_ERROR:

Fix

On macOS, when the retry returns EISCONN, read SO_ERROR. If an error is pending, set errno to it and return REDIS_ERR. Both callers (redisContextWaitReady() and __redisAsyncHandleConnect()) then report it through redisCheckSocketError(), which falls back to the saved errno once SO_ERROR has been read, so the message is Connection refused again.

The check applies only under __APPLE__:

  • macOS is the only kernel known to do this. FreeBSD still reports the refusal on the retry; its CI job passes A GLib adaptor #71 / Fix self-test of connect error on Linux; merge duplicated string (DRY) #72 without this change.
  • On Linux, SO_ERROR can hold a queued IP_RECVERR error on a socket that is connected and usable (the "Connection with a queued error remains usable" test), so reading it there would be wrong.
  • Other platforms keep their current behaviour. The guard can be widened if another kernel turns out to behave the same way.

Testing

🤖 Generated with Claude Code


Note

Low Risk
Small, platform-guarded change to connect completion handling; main risk is mis-detecting failures on Apple builds, with Linux paths untouched.

Overview
Fixes a macOS-specific non-blocking connect regression where retrying connect() after a refused connection returns EISCONN while SO_ERROR still holds ECONNREFUSED. Hiredis previously treated that as success, which could close the socket on later TCP_NODELAY and surface misleading errors instead of "Connection refused".

On EISCONN only when __APPLE__ is defined, redisCheckConnectDone() now reads SO_ERROR; if a pending error is set, it propagates via errno and returns REDIS_ERR instead of marking the connection complete. Linux behavior is unchanged (no extra SO_ERROR read on EISCONN there).

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

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T18:04:48.924487Z 7ff0002 New commits
ℹ️ 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.

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

ABI compatibility check

✅ libhiredis — compatible

No ABI changes detected.

✅ libhiredis_ssl — compatible

No ABI changes detected.

Baseline 5bb7f030f6631b243438cf0fdd3be84352b905bb vs head 7ff00025da8f921ba7998044819750cc4ee89a6c. Apply the ignore-abi-check label to skip this check for an intentional, SONAME-bumped ABI break.

redisCheckConnectDone() learns the result of a non-blocking connect by
calling connect() again, and maps EISCONN to "connected". Since macOS
26.7 / 15.8.1 (and on macOS 27) the retry answers EISCONN for a refused
connect too, while SO_ERROR still holds ECONNREFUSED. hiredis then
treats the dead socket as connected, fails setting TCP_NODELAY with
EINVAL, closes the fd and reports "setsockopt(TCP_NODELAY): Invalid
argument" instead of "Connection refused".

On macOS, read SO_ERROR on EISCONN and fail with the pending error if
there is one. Other platforms are left as they are: no other kernel is
known to do this (FreeBSD still reports the refusal on the retry), and
on Linux SO_ERROR may hold a queued error on a socket that is connected
and usable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@uglide
uglide force-pushed the im/fix-eisconn-refused-connect branch from b2cbeaa to 7ff0002 Compare November 18, 2026 18:04
@uglide uglide changed the title Check SO_ERROR when a connect() retry answers EISCONN Check SO_ERROR when a connect() retry answers EISCONN on macOS Nov 18, 2026
@uglide
uglide merged commit e90c24e into master Nov 18, 2026
28 checks passed
@uglide
uglide deleted the im/fix-eisconn-refused-connect branch November 18, 2026 18:11
@uglide uglide added the bug label Nov 19, 2026
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.

1 participant