Skip to content

ext/sockets: using accept4 wheneever possible for php_accept_connect … #19268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2025

Conversation

devnexen
Copy link
Member

…helper

Haiku now supports it as well now, so we can simplify the workflow a bit for those platforms.

@devnexen devnexen marked this pull request as ready for review July 27, 2025 14:44
Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

On first sight this looks fine, but I also found this note in the manpage:

 The accept4() function behaves exactly like accept(), but the socket it
 returns does not inherit the O_NONBLOCK flag of s; instead, it applies
 the following bits set in flags to the returned file descriptor:

@@ -284,7 +284,12 @@ static bool php_open_listen_sock(php_socket *sock, unsigned short port, int back
static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct sockaddr *la, socklen_t *la_len) /* {{{ */
{
#if defined(HAVE_ACCEPT4)
out_sock->bsd_socket = accept4(in_sock->bsd_socket, la, la_len, SOCK_CLOEXEC);
int flags = SOCK_CLOEXEC;
Copy link
Member

Choose a reason for hiding this comment

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

Weird indent

…helper

Haiku now supports it as well now, so we can simplify the workflow a bit for those platforms.
@devnexen devnexen force-pushed the haiku_accept4_support branch from da1013b to 93bdc8f Compare July 27, 2025 17:36
@@ -283,6 +283,19 @@ static bool php_open_listen_sock(php_socket *sock, unsigned short port, int back

static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct sockaddr *la, socklen_t *la_len) /* {{{ */
{
#if defined(HAVE_ACCEPT4)
int flags = SOCK_CLOEXEC;
if (!in_sock->blocking) {
Copy link
Member

@nielsdos nielsdos Jul 27, 2025

Choose a reason for hiding this comment

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

Turns out it's even more complicated:

From https://linux.die.net/man/2/accept4:
On Linux, the new socket returned by accept() does not inherit file status flags such as O_NONBLOCK and O_ASYNC from the listening socket. This behavior differs from the canonical BSD sockets implementation. Portable programs should not rely on inheritance or noninheritance of file status flags and always explicitly set all required flags on the socket returned from accept().

So I guess on BSD you have to inherit the flag and on Linux not, in order to remain consistent?

Copy link
Member Author

@devnexen devnexen Jul 27, 2025

Choose a reason for hiding this comment

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

we do not create sockets with O_ASYNC in the extension. However it seems to me it s better to set SOCK_NONBLOCK for all platforms

for cross-platform and explicit control, it is always best practice to use accept4() and include the SOCK_NONBLOCK flag if you require a non-blocking socket

Copy link
Member

Choose a reason for hiding this comment

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

I guess that makes sense. Damn why are the C interfaces always so inconsistent

@devnexen devnexen merged commit 068aaed into php:master Jul 27, 2025
9 checks passed
@devnexen devnexen deleted the haiku_accept4_support branch July 27, 2025 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants