Skip to content

socket: use accept4 when available - #16979

Closed
panjf2000 wants to merge 3 commits into
curl:masterfrom
panjf2000:accept4
Closed

panjf2000 wants to merge 3 commits into
curl:masterfrom
panjf2000:accept4

Conversation

@panjf2000
panjf2000 force-pushed the accept4 branch 3 times, most recently from 1504d22 to 745d7e4 Compare April 5, 2025 16:54
@testclutch

Copy link
Copy Markdown

Analysis of PR #16979 at 745d7e4f:

Test 101 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

Test 103 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

Test 108 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

Test 119 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

Test 144 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

Test 145 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

Test 212 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

There are more failures, but that's enough from Gha.

Generated by Testclutch

@panjf2000

Copy link
Copy Markdown
Contributor Author

This is weird, I passed all the tests on my Linux server:

image

Anything special on those failed CIs? @bagder @icing @vszakats

@dfandrich

Copy link
Copy Markdown
Contributor

The failing tests all seem to have debug mode enabled. Can you reproduce the failures if you configure with --enable-debug?

@panjf2000

Copy link
Copy Markdown
Contributor Author

The failing tests all seem to have debug mode enabled. Can you reproduce the failures if you configure with --enable-debug?

image

@panjf2000

Copy link
Copy Markdown
Contributor Author

Some failed tests arose, but they don't seem to be the same as those previous ones on CI's.

@bagder

bagder commented Apr 6, 2025

Copy link
Copy Markdown
Member

Add accept4 support to memdebug:

From a212acb9f2cfe740529aa03cd4fb173ec176367e Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <[email protected]>
Date: Sun, 6 Apr 2025 11:21:42 +0200
Subject: [PATCH] memdebug: add accept4

---
 lib/memdebug.c | 18 ++++++++++++++++++
 lib/memdebug.h |  6 ++++++
 2 files changed, 24 insertions(+)

diff --git a/lib/memdebug.c b/lib/memdebug.c
index 58e4614f5f..b351726b32 100644
--- a/lib/memdebug.c
+++ b/lib/memdebug.c
@@ -376,10 +376,28 @@ curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
                  source, line, sockfd);
 
   return sockfd;
 }
 
+#ifdef HAVE_ACCEPT4
+curl_socket_t curl_dbg_accept4(curl_socket_t s, void *saddr, void *saddrlen,
+                               int flags,
+                               int line, const char *source)
+{
+  struct sockaddr *addr = (struct sockaddr *)saddr;
+  curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
+
+  curl_socket_t sockfd = accept4(s, addr, addrlen, flags);
+
+  if(source && (sockfd != CURL_SOCKET_BAD))
+    curl_dbg_log("FD %s:%d accept() = %" FMT_SOCKET_T "\n",
+                 source, line, sockfd);
+
+  return sockfd;
+}
+#endif
+
 /* separate function to allow libcurl to mark a "faked" close */
 void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
 {
   if(source)
     curl_dbg_log("FD %s:%d sclose(%" FMT_SOCKET_T ")\n",
diff --git a/lib/memdebug.h b/lib/memdebug.h
index aabcaadef4..fbea0c38bd 100644
--- a/lib/memdebug.h
+++ b/lib/memdebug.h
@@ -90,10 +90,13 @@ CURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd,
                                       int line, const char *source);
 CURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd,
                                 int line, const char *source);
 CURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s, void *a, void *alen,
                                           int line, const char *source);
+CURL_EXTERN curl_socket_t curl_dbg_accept4(curl_socket_t s, void *saddr,
+                                           void *saddrlen, int flags,
+                                           int line, const char *source);
 #ifdef HAVE_SOCKETPAIR
 CURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol,
                                     curl_socket_t socket_vector[2],
                                     int line, const char *source);
 #endif
@@ -157,10 +160,13 @@ CURL_EXTERN ALLOC_FUNC
 #define socket(domain,type,protocol)\
  curl_dbg_socket((int)domain, type, protocol, __LINE__, __FILE__)
 #undef accept /* for those with accept as a macro */
 #define accept(sock,addr,len)\
  curl_dbg_accept(sock, addr, len, __LINE__, __FILE__)
+#undef accept4 /* for those with accept as a macro */
+#define accept4(sock,addr,len,flags)                            \
+  curl_dbg_accept4(sock, addr, len, flags, __LINE__, __FILE__)
 #ifdef HAVE_SOCKETPAIR
 #define socketpair(domain,type,protocol,socket_vector)\
  curl_dbg_socketpair((int)domain, type, protocol, socket_vector, \
                      __LINE__, __FILE__)
 #endif
-- 
2.49.0

@panjf2000

Copy link
Copy Markdown
Contributor Author

Any chance that this failed CI is a flaky one and is irrelevant to this PR?

@bagder

bagder commented Apr 6, 2025

Copy link
Copy Markdown
Member

Any chance that this failed CI is a flaky one and is irrelevant to this PR?

I am pretty sure that it is and is not result of your work.

@panjf2000

Copy link
Copy Markdown
Contributor Author

Alright, I think we are good to go! Could you take a glance at this? @bagder

@bagder bagder closed this in 3d02872 Apr 6, 2025
@bagder

bagder commented Apr 6, 2025

Copy link
Copy Markdown
Member

Thanks!

@panjf2000
panjf2000 deleted the accept4 branch April 6, 2025 12:38
@Myriachan

Myriachan commented Apr 8, 2025 •

Copy link
Copy Markdown
Contributor

On Windows, you can get the atomic equivalent of O_CLOEXEC by calling WSASocketW() with WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED. (Note that socket() uses WSA_FLAG_OVERLAPPED internally, so this flag is needed if using WSASocketW() instead.)

WSA_FLAG_NO_HANDLE_INHERIT requires Windows 8.0+ though, so if you need 7 compatibility it'll have to detect versions.

WinSock's accept() copies the no-inherit flag from the listening socket, so there's no direct equivalent of accept4() needed.

vszakats added a commit to vszakats/curl that referenced this pull request Jun 2, 2025
To set `-D_GNU_SOURCE`.

Required to declare `accept4` on uclibc targets. `host_os` may have
the value `uclinux-uclibcgnueabi` in this case.

Follow-up to 3d02872 curl#16979

May fix curl#17512
Reported-by: Kadambini Nema
vszakats added a commit that referenced this pull request Jun 3, 2025
To set `-D_GNU_SOURCE`.

Required to declare `accept4` on uclibc targets. `host_os` can have
the value `uclinux-uclibcgnueabi` in this case.

Fixing:
```
cf-socket.c: In function 'cf_tcp_accept_connect':
cf-socket.c:2141:18: error: implicit declaration of function 'accept4'; did you mean 'accept'? [-Werror=implicit-function-declaration]
 2141 |     s_accepted = accept4(ctx->sock, (struct sockaddr *) &add, &size,
```

Follow-up to 3d02872 #16979

Reported-by: Kadambini Nema
Fixes #17512
Closes #17524
vszakats added a commit to vszakats/curl that referenced this pull request Jul 6, 2025
vszakats added a commit to vszakats/curl that referenced this pull request Jul 6, 2025
vszakats added a commit to vszakats/curl that referenced this pull request Jul 6, 2025
vszakats added a commit that referenced this pull request Jul 6, 2025
Follow-up to 3d02872 #16979
Cherry-picked from #17827
Closes #17831
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

6 participants