Skip to content

Fix non-blocking Socket#connect on Unix - #17436

Open
ysbaddaden wants to merge 3 commits into
crystal-lang:masterfrom
ysbaddaden:fix/socket-connect-on-macos-26.7
Open

ysbaddaden wants to merge 3 commits into
crystal-lang:masterfrom
ysbaddaden:fix/socket-connect-on-macos-26.7

Conversation

@ysbaddaden

@ysbaddaden ysbaddaden commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

The macOS 26.7 release apparently has a bug where non-blocking socket connection may complete with EISCONN (connected) when it actually failed (sic). See https://forum.crystal-lang.org/t/socket-connection-error-after-macos-26-7-update/9145

EDIT: Our non-blocking loop for Socket#connect is wrong: unlike other operations, connect musn't be re-tried after waiting, it's actually asynchronous, and we must check the socket error state after waiting for the socket to become writable. See connect(2).

Adds a second check for the actual error state (SO_ERROR) of the socket after EISCONN. This apparently catches the issue.

Adapted from https://github.com/nogginly/socket_connect_fix.cr/blob/b22e7df11d3911be9f04f4a9fc5ff8de0c829e2d/src/socket_connect_fix.cr by @nogginly

The macOS 26.7 release has a bug where non-blocking socket connections
may complete with EISCONN (connected) when it actually failed (sic).

Adds a second check for the actual error state (SO_ERROR) of the socket
after EISCONN, which apparently catches the issue.

Co-Authored-By: nogginly <19142777+nogginly@users.noreply.github.com>
@ysbaddaden ysbaddaden self-assigned this Sep 18, 2026
@ysbaddaden ysbaddaden added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:networking labels Sep 18, 2026
@straight-shoota

straight-shoota commented Sep 19, 2026

Copy link
Copy Markdown
Member

EventLoop::IoUring#connect also ignores EISCONN. We should probably just drop that as well.

I suppose there is no reason to check with getsockopt with io_uring.

@ysbaddaden

Copy link
Copy Markdown
Collaborator Author

Now io_uring also raises on EISCONN 👍

There's indeed no need to check for SO_ERROR, the kernel threads do the wait.

@straight-shoota straight-shoota added this to the 1.21.1 milestone Sep 21, 2026
@straight-shoota straight-shoota changed the title Fix: Socket#connect on macOS 26.7 Fix non-blocking Socket#connect on Unix Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:networking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants