Skip to contentSkip to conversation

daemon: correctly handle soft accept() errors

PATCH Superseded Carlo Marcelo Arenas Belón 5 participants 14 messages Jun 26, 2025 – Jul 1, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 daemon: correctly handle soft accept() errors

Since df076bdbcc ([PATCH] GIT: Listen on IPv6 as well, if available., 2005-07-23), the original error checking was included in an inner loop unchanged, where its effect was different.

Instead of retrying, after a EINTR during accept() in the listening socket, it will advance to the next one and try with that instead, leaving the client waiting for another round.

Make sure that the loop doesn't advance and while at it, make sure that any possible completed childs get reaped earlier. To avoid an unlilely busy loop, fallback to the old behaviour after a couple of attempts.

---
 daemon.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
daemon.c +12 -2
index d1be61fd57..8d02099a4e 100644
@@ -1153,11 +1153,21 @@ static int service_loop(struct socketlist *socklist)
 #endif
 				} ss;
 				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+				int incoming;
+
+redo:
+				incoming = accept(pfd[i].fd, &ss.sa, &sslen);
 				if (incoming < 0) {
+					int retry = 2;
+
 					switch (errno) {
-					case EAGAIN:
 					case EINTR:
+						if (--retry) {
+							check_dead_children();
+							goto redo;
+						}
+						/* fallthrough */
+					case EAGAIN:
 					case ECONNABORTED:
 						continue;
 					default:
Signature · 1 line
2.50.0.132.gb7f585ac00
#1 lore raw
Quoted 10 lines · Carlo Marcelo Arenas Belón

Since df076bdbcc ([PATCH] GIT: Listen on IPv6 as well, if available., 2005-07-23), the original error checking was included in an inner loop unchanged, where its effect was different.

Instead of retrying, after a EINTR during accept() in the listening socket, it will advance to the next one and try with that instead, leaving the client waiting for another round.

Make sure that the loop doesn't advance and while at it, make sure that any possible completed childs get reaped earlier. To avoid an

s/childs/children/

Quoted 1 line

unlilely busy loop, fallback to the old behaviour after a couple

unlilely?

Quoted 3 lines

of attempts.

#2 lore raw

PATCH v2 1/1 daemon: correctly handle soft accept() errors in service_loop

In reply to Carlo Marcelo Arenas Belón

Since df076bdbcc ([PATCH] GIT: Listen on IPv6 as well, if available., 2005-07-23), the original error checking was included in an inner loop unchanged, where its effect was different.

Instead of retrying, after a EINTR during accept() in the listening socket, it will advance to the next one and try with that instead, leaving the client waiting for another round.

Make sure that the loop doesn't advance and while at it, make sure that any possible completed children get reaped earlier. To avoid an unlikely busy loop, fallback to the old behaviour after a couple of attempts.

---
 daemon.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
daemon.c +11 -2
index d1be61fd57..f113839781 100644
@@ -1145,6 +1145,7 @@ static int service_loop(struct socketlist *socklist)
 
 		for (size_t i = 0; i < socklist->nr; i++) {
 			if (pfd[i].revents & POLLIN) {
+				int incoming;
 				union {
 					struct sockaddr sa;
 					struct sockaddr_in sai;
@@ -1153,11 +1154,19 @@ static int service_loop(struct socketlist *socklist)
 #endif
 				} ss;
 				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+				int retry = 3;
+
+			redo:
+				incoming = accept(pfd[i].fd, &ss.sa, &sslen);
 				if (incoming < 0) {
 					switch (errno) {
-					case EAGAIN:
 					case EINTR:
+						if (--retry) {
+							check_dead_children();
+							goto redo;
+						}
+						/* fallthrough */
+					case EAGAIN:
 					case ECONNABORTED:
 						continue;
 					default:
Signature · 1 line
2.50.0.132.g195eec4876
#3 lore raw

Re: [PATCH v2] daemon: correctly handle soft accept() errors in service_loop

Hi Carlo

Quoted 9 lines · Carlo Marcelo Arenas Belón

Since df076bdbcc ([PATCH] GIT: Listen on IPv6 as well, if available., 2005-07-23), the original error checking was included in an inner loop unchanged, where its effect was different.

Instead of retrying, after a EINTR during accept() in the listening socket, it will advance to the next one and try with that instead, leaving the client waiting for another round.

Make sure that the loop doesn't advance

That makes sense

Quoted 2 lines

and while at it, make sure
that any possible completed children get reaped earlier.

What's the rationale for that? It means we end up calling reap_dead_children() twice which sounds inefficient. If it is important then I'm also struggling to see how it fits in with the proposal to use SA_RESTART

Quoted 22 lines

To avoid an
unlikely busy loop, fallback to the old behaviour after a couple of attempts.

---
  daemon.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
daemon.c +1 -0
index d1be61fd57..f113839781 100644
@@ -1145,6 +1145,7 @@ static int service_loop(struct socketlist *socklist)
 
  		for (size_t i = 0; i < socklist->nr; i++) {
  			if (pfd[i].revents & POLLIN) {
+				int incoming;
  				union {
  					struct sockaddr sa;
  					struct sockaddr_in sai;

daemon.c static int service_loop(struct socketlist *socklist) · line 1156

Quoted 4 lines
daemon.c +0 -1
@@ -1153,11 +1154,19 @@ static int service_loop(struct socketlist *socklist)
  #endif
  				} ss;
  				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);

Why is the declaration of incoming moved but retry is declared here?

Thanks

Phillip

Quoted 17 lines
daemon.c +10 -1
+				int retry = 3;
+
+			redo:
+				incoming = accept(pfd[i].fd, &ss.sa, &sslen);
  				if (incoming < 0) {
  					switch (errno) {
-					case EAGAIN:
  					case EINTR:
+						if (--retry) {
+							check_dead_children();
+							goto redo;
+						}
+						/* fallthrough */
+					case EAGAIN:
  					case ECONNABORTED:
  						continue;
  					default:
#4 lore raw

Re: [PATCH v2] daemon: correctly handle soft accept() errors in service_loop

Quoted 13 lines · Phillip Wood
Quoted 11 lines · Carlo Marcelo Arenas Belón
daemon.c +1 -0
index d1be61fd57..f113839781 100644
@@ -1145,6 +1145,7 @@ static int service_loop(struct socketlist *socklist)
  		for (size_t i = 0; i < socklist->nr; i++) {
  			if (pfd[i].revents & POLLIN) {
+				int incoming;
  				union {
  					struct sockaddr sa;
Quoted 8 lines

daemon.c static int service_loop(struct socketlist *socklist) · line 1156

Quoted 6 lines
daemon.c +0 -1
  					struct sockaddr_in sai;
@@ -1153,11 +1154,19 @@ static int service_loop(struct socketlist *socklist)
  #endif
  				} ss;
  				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);

Why is the declaration of incoming moved but retry is declared here?

Separating the declaration and assignment for incoming is needed so we can insert a label for goto; moving it up just removes distractions so the rest of the logic is clearly in view.

Obviously that includes the definition and assignment for retry.

How would you suggest to arrange this better?

Carlo

#5 lore raw

Re: [PATCH v2] daemon: correctly handle soft accept() errors in service_loop

Quoted 23 lines · Carlo Marcelo Arenas Belón
Quoted 13 lines · Phillip Wood
Quoted 11 lines · Carlo Marcelo Arenas Belón
daemon.c +1 -0
index d1be61fd57..f113839781 100644
@@ -1145,6 +1145,7 @@ static int service_loop(struct socketlist *socklist)
  		for (size_t i = 0; i < socklist->nr; i++) {
  			if (pfd[i].revents & POLLIN) {
+				int incoming;
  				union {
  					struct sockaddr sa;
Quoted 8 lines

daemon.c static int service_loop(struct socketlist *socklist) · line 1156

Quoted 6 lines
daemon.c +0 -1
  					struct sockaddr_in sai;
@@ -1153,11 +1154,19 @@ static int service_loop(struct socketlist *socklist)
  #endif
  				} ss;
  				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);

Why is the declaration of incoming moved but retry is declared here?

Quoted 7 lines

Separating the declaration and assignment for incoming is needed so we can insert a label for goto; moving it up just removes distractions so the rest of the logic is clearly in view.

Obviously that includes the definition and assignment for retry.

How would you suggest to arrange this better?

I think what Phillip meant was more like this, perhaps.

		socklen_t sslen = sizeof(ss);

- int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+ int incoming;
+ int retry = 3;
+

+		incoming = accept(pfd[i].fd, &ss.sa, &sslen);
		if (incoming < 0) {
			...
#6 lore raw

Re: [PATCH v2] daemon: correctly handle soft accept() errors in service_loop

Quoted 41 lines · Junio C Hamano
Quoted 23 lines · Carlo Marcelo Arenas Belón
Quoted 13 lines · Phillip Wood
Quoted 11 lines · Carlo Marcelo Arenas Belón
daemon.c +1 -0
index d1be61fd57..f113839781 100644
@@ -1145,6 +1145,7 @@ static int service_loop(struct socketlist *socklist)
  		for (size_t i = 0; i < socklist->nr; i++) {
  			if (pfd[i].revents & POLLIN) {
+				int incoming;
  				union {
  					struct sockaddr sa;
Quoted 8 lines

daemon.c static int service_loop(struct socketlist *socklist) · line 1156

Quoted 6 lines
daemon.c +0 -1
  					struct sockaddr_in sai;
@@ -1153,11 +1154,19 @@ static int service_loop(struct socketlist *socklist)
  #endif
  				} ss;
  				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);

Why is the declaration of incoming moved but retry is declared here?

Quoted 7 lines

Separating the declaration and assignment for incoming is needed so we can insert a label for goto; moving it up just removes distractions so the rest of the logic is clearly in view.

Obviously that includes the definition and assignment for retry.

How would you suggest to arrange this better?

I think what Phillip meant was more like this, perhaps.

		socklen_t sslen = sizeof(ss);
daemon.c +4 -1
-		int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+		int incoming;
+		int retry = 3;
+
+		incoming = accept(pfd[i].fd, &ss.sa, &sslen);
Quoted 2 lines
		if (incoming < 0) {
			...

That seems unnecessarily restrictive just to minimize churn and leaves the deflaration of incoming strangely sitting in between two assignments, which while it doesn't trigger -Wdeclaration-after-statement seems to go against its spirit.

Will include in a v3 with all other suggestions, but frankly think that the original was overall cleaner.

Carlo

#7 lore raw

Re: [PATCH v2] daemon: correctly handle soft accept() errors in service_loop

Hridoy Ahmed

Quoted 57 lines · Carlo Marcelo Arenas Belón
Quoted 36 lines · Junio C Hamano
Quoted 23 lines · Carlo Marcelo Arenas Belón
Quoted 13 lines · Phillip Wood
Quoted 13 lines · Carlo Marcelo Arenas Belón
Quoted 11 lines
daemon.c +1 -0
index d1be61fd57..f113839781 100644
@@ -1145,6 +1145,7 @@ static int service_loop(struct socketlist *socklist)
         for (size_t i = 0; i < socklist->nr; i++) {
             if (pfd[i].revents & POLLIN) {
+                int incoming;
                 union {
                     struct sockaddr sa;
Quoted 8 lines
Quoted 8 lines

daemon.c static int service_loop(struct socketlist *socklist) · line 1156

Quoted 6 lines
daemon.c +0 -1
                     struct sockaddr_in sai;
@@ -1153,11 +1154,19 @@ static int service_loop(struct socketlist *socklist)
 #endif
                 } ss;
                 socklen_t sslen = sizeof(ss);
-                int incoming = accept(pfd[i].fd, &ss.sa, &sslen);

Why is the declaration of incoming moved but retry is declared here?

Quoted 7 lines

Separating the declaration and assignment for incoming is needed so we can insert a label for goto; moving it up just removes distractions so the rest of the logic is clearly in view.

Obviously that includes the definition and assignment for retry.

How would you suggest to arrange this better?

I think what Phillip meant was more like this, perhaps.

       socklen_t sslen = sizeof(ss);

daemon.c static int service_loop(struct socketlist *socklist) · line 1161

Quoted 7 lines
daemon.c +4 -1
-        int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+        int incoming;
+        int retry = 3;
+
+        incoming = accept(pfd[i].fd, &ss.sa, &sslen);
       if (incoming < 0) {
...

That seems unnecessarily restrictive just to minimize churn and leaves the deflaration of incoming strangely sitting in between two assignments, which while it doesn't trigger -Wdeclaration-after-statement seems to go against its spirit.

Will include in a v3 with all other suggestions, but frankly think that the original was overall cleaner.

Carlo

#8 lore raw

Re: [PATCH v2] daemon: correctly handle soft accept() errors in service_loop

In reply to Carlo Marcelo Arenas Belón
Quoted 5 lines · Carlo Marcelo Arenas Belón
Quoted 5 lines
		socklen_t sslen = sizeof(ss);
daemon.c +3 -1
-		int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+		int incoming;
+		int retry = 3;
+
Quoted 8 lines
Quoted 3 lines
daemon.c +1 -0
+		incoming = accept(pfd[i].fd, &ss.sa, &sslen);
		if (incoming < 0) {
			...

That seems unnecessarily restrictive just to minimize churn and leaves the deflaration of incoming strangely sitting in between two assignments, which while it doesn't trigger -Wdeclaration-after-statement seems to go against its spirit.

Hmph, I am not Phillip, but my take on it is that incoming and retry are fairly closely related variables in this loop, and better grouped together?

I also find it a bit ugly to hardcode "3" here like this, but perhaps I am overthinking about it.

#9 lore raw

Re: [PATCH v2] daemon: correctly handle soft accept() errors in service_loop

Quoted 10 lines · Junio C Hamano
Quoted 4 lines · Carlo Marcelo Arenas Belón

That seems unnecessarily restrictive just to minimize churn and leaves the deflaration of incoming strangely sitting in between two assignments, which while it doesn't trigger -Wdeclaration-after-statement seems to go against its spirit.

Hmph, I am not Phillip, but my take on it is that incoming and retry are fairly closely related variables in this loop, and better grouped together?

That was my thinking too

Thanks

Phillip

#10 lore raw

PATCH v3 1/1 daemon: correctly handle soft accept() errors in service_loop

In reply to Carlo Marcelo Arenas Belón

Since df076bdbcc ([PATCH] GIT: Listen on IPv6 as well, if available., 2005-07-23), the original error checking was included in an inner loop unchanged, where its effect was different.

Instead of retrying, after a EINTR during accept() in the listening socket, it will advance to the next one and try with that instead, leaving the client waiting for another round.

Make sure to retry with the same listener socket that failed originally.

To avoid an unlikely busy loop, fallback to the old behaviour after a couple of attempts.

---
 daemon.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
daemon.c +10 -2
index d1be61fd57..9ac9efa17c 100644
@@ -1153,11 +1153,19 @@ static int service_loop(struct socketlist *socklist)
 #endif
 				} ss;
 				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+				int incoming;
+				int retry = 3;
+
+			redo:
+				incoming = accept(pfd[i].fd, &ss.sa, &sslen);
 				if (incoming < 0) {
 					switch (errno) {
-					case EAGAIN:
 					case EINTR:
+						if (--retry)
+							goto redo;
+
+						/* fallthrough */
+					case EAGAIN:
 					case ECONNABORTED:
 						continue;
 					default:
Signature · 1 line
2.50.0.132.g195eec4876.dirty
#11 lore raw

Re: [PATCH v3] daemon: correctly handle soft accept() errors in service_loop

Hi Carlo

This looks good

Thanks

Phillip

Quoted 44 lines · Carlo Marcelo Arenas Belón

Since df076bdbcc ([PATCH] GIT: Listen on IPv6 as well, if available., 2005-07-23), the original error checking was included in an inner loop unchanged, where its effect was different.

Instead of retrying, after a EINTR during accept() in the listening socket, it will advance to the next one and try with that instead, leaving the client waiting for another round.

Make sure to retry with the same listener socket that failed originally.

To avoid an unlikely busy loop, fallback to the old behaviour after a couple of attempts.

---
  daemon.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
daemon.c +10 -2
index d1be61fd57..9ac9efa17c 100644
@@ -1153,11 +1153,19 @@ static int service_loop(struct socketlist *socklist)
  #endif
  				} ss;
  				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+				int incoming;
+				int retry = 3;
+
+			redo:
+				incoming = accept(pfd[i].fd, &ss.sa, &sslen);
  				if (incoming < 0) {
  					switch (errno) {
-					case EAGAIN:
  					case EINTR:
+						if (--retry)
+							goto redo;
+
+						/* fallthrough */
+					case EAGAIN:
  					case ECONNABORTED:
  						continue;
  					default:
#12 lore raw

Re: [PATCH v3] daemon: correctly handle soft accept() errors in service_loop

Quoted 7 lines · Phillip Wood

Hi Carlo

This looks good

Thanks

Phillip

Thanks, both of you. Shall we mark the topic for 'next', then?

Quoted 42 lines · Carlo Marcelo Arenas Belón
Quoted 41 lines

Since df076bdbcc ([PATCH] GIT: Listen on IPv6 as well, if available., 2005-07-23), the original error checking was included in an inner loop unchanged, where its effect was different.
Instead of retrying, after a EINTR during accept() in the listening socket, it will advance to the next one and try with that instead, leaving the client waiting for another round.
Make sure to retry with the same listener socket that failed originally.
To avoid an unlikely busy loop, fallback to the old behaviour after a
couple of attempts.

---
  daemon.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
daemon.c +10 -2
index d1be61fd57..9ac9efa17c 100644
@@ -1153,11 +1153,19 @@ static int service_loop(struct socketlist *socklist)
  #endif
  				} ss;
  				socklen_t sslen = sizeof(ss);
-				int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
+				int incoming;
+				int retry = 3;
+
+			redo:
+				incoming = accept(pfd[i].fd, &ss.sa, &sslen);
  				if (incoming < 0) {
  					switch (errno) {
-					case EAGAIN:
  					case EINTR:
+						if (--retry)
+							goto redo;
+
+						/* fallthrough */
+					case EAGAIN:
  					case ECONNABORTED:
  						continue;
  					default:
#13 lore raw

Re: [PATCH v3] daemon: correctly handle soft accept() errors in service_loop

Quoted 2 lines · Junio C Hamano

Thanks, both of you. Shall we mark the topic for 'next', then?

Yes, I think so

Thanks

Phillip