Skip to content

Fix fcntl reference for fds[1] in socketpair setup#16072

Merged
straight-shoota merged 3 commits intocrystal-lang:masterfrom
kojix2:socketpair-nonblock
Aug 15, 2025
Merged

Fix fcntl reference for fds[1] in socketpair setup#16072
straight-shoota merged 3 commits intocrystal-lang:masterfrom
kojix2:socketpair-nonblock

Conversation

@kojix2
Copy link
Contributor

@kojix2 kojix2 commented Aug 8, 2025

Hi

While reviewing Crystal’s implementation with AI, I came across a potential bug in the non-blocking setup of socketpair.

According to the AI, the issue is as follows:


Problem
On line 206, the code reads flags from fds[0] when setting them for fds[1]:

fcntl(fds[1], LibC::F_SETFL, fcntl(fds[0], LibC::F_GETFL) | LibC::O_NONBLOCK)

It should read the flags from fds[1] itself:

fcntl(fds[1], LibC::F_SETFL, fcntl(fds[1], LibC::F_GETFL) | LibC::O_NONBLOCK)

This affects only older systems without SOCK_CLOEXEC support, where the second socket may incorrectly inherit the first socket’s settings.


I’m not an expert on sockets, so I can’t confirm this. If it’s incorrect, please close the PR. Thank you.

@straight-shoota
Copy link
Member

This is probably not a real bug. It's not super clean, but semantically should be all right.
Both file descriptors in fds should have the same status flags (manpage: "The two sockets are indistinguishable.").

Potentially we could avoid one syscall here: store fcntl(fds[0], LibC::F_GETFL) | LibC::O_NONBLOCK in a variable and apply it to both file descriptors. That would have the same effect as the current code but with one less syscall.

@kojix2
Copy link
Contributor Author

kojix2 commented Aug 8, 2025

Thanks @straight-shoota
This turned out not to be a bug.

I have added the commit based on your suggestion.

@kojix2 kojix2 changed the title Fix socketpair fcntl reference bug for fds[1] Reduce fcntl syscalls in socketpair setup Aug 8, 2025
@ysbaddaden
Copy link
Collaborator

ysbaddaden commented Aug 8, 2025

Nope, it's indeed a copy-pasta typo. The first commit was more correct (get flags from fds[1]) even though the flags should be identical (in theory) for both fds.

This reverts commit 9461701.
@kojix2 kojix2 changed the title Reduce fcntl syscalls in socketpair setup Fix fcntl reference for fds[1] in socketpair setup Aug 13, 2025
Copy link
Collaborator

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

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

Thank you @kojix2 🙇

@ysbaddaden ysbaddaden added this to the 1.18.0 milestone Aug 13, 2025
@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:files labels Aug 15, 2025
@straight-shoota straight-shoota merged commit 3ad31eb into crystal-lang:master Aug 15, 2025
41 checks passed
@straight-shoota straight-shoota changed the title Fix fcntl reference for fds[1] in socketpair setup Fix fcntl reference for fds[1] in socketpair setup Sep 15, 2025
@straight-shoota straight-shoota changed the title Fix fcntl reference for fds[1] in socketpair setup Fix fcntl reference for fds[1] in socketpair setup Sep 15, 2025
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. platform:unix topic:stdlib:files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants