Skip to contentSkip to conversation

fsidd: handle accept4() failures before creating client event

PATCH In review Scott Mayhew 2 participants 2 messages Sep 3 – Sep 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsidd: handle accept4() failures before creating client event

Handle failures from accept4() before creating a client event, and add a short backoff on errors indicating resource exhaustion.

---
 support/reexport/fsidd.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
support/reexport/fsidd.c +26 -2
index 51750ea3..9b80b451 100644
@@ -17,6 +17,7 @@
 #include "xlog.h"
 
 static struct event_base *evbase;
+static struct event *srv_ev;
 static struct reexpdb_backend_plugin *dbbackend = &sqlite_plug_ops;
 
 /* assert_safe() always evalutes it argument, as it might have
@@ -133,21 +134,44 @@ static void client_cb(evutil_socket_t cl, short ev, void *d)
 	}
 }
 
+static void srv_reenable_cb(evutil_socket_t fd, short ev, void *d)
+{
+	(void)fd;
+	(void)ev;
+	(void)d;
+	if (srv_ev)
+		event_add(srv_ev, NULL);
+}
+
 static void srv_cb(evutil_socket_t fd, short ev, void *d)
 {
 	int cl = accept4(fd, NULL, NULL, SOCK_NONBLOCK);
 	struct event *client_ev;
+
+	if (cl == -1) {
+		if (errno == EMFILE || errno == ENFILE || errno == ENOMEM ||
+				errno == ENOBUFS) {
+			struct timeval tv = {0, 200000};
+			event_del(srv_ev);
+			event_base_once(evbase, -1, EV_TIMEOUT, srv_reenable_cb,
+					NULL, &tv);
+		}
+		return;
+	}
 	
 	(void)ev;
 	(void)d;
 
 	client_ev = event_new(evbase, cl, EV_READ | EV_PERSIST | EV_CLOSED, client_cb, event_self_cbarg());
-	event_add(client_ev, NULL);
+	if (!client_ev || event_add(client_ev, NULL) == -1) {
+		if (client_ev)
+			event_free(client_ev);
+		close(cl);
+	}
 }
 
 int main(void)
 {
-	struct event *srv_ev;
 	struct sockaddr_un addr;
 	socklen_t addr_len;
 	char *sock_file;
Signature · 1 line
2.55.0
#1 lore raw
Quoted 4 lines · Scott Mayhew

Handle failures from accept4() before creating a client event, and add a short backoff on errors indicating resource exhaustion.

Committed... (tag: nfs-utils-2-9-3-rc4)

steved.

Quoted 63 lines
---
  support/reexport/fsidd.c | 28 ++++++++++++++++++++++++++--
  1 file changed, 26 insertions(+), 2 deletions(-)
support/reexport/fsidd.c +26 -2
index 51750ea3..9b80b451 100644
@@ -17,6 +17,7 @@
  #include "xlog.h"
 
  static struct event_base *evbase;
+static struct event *srv_ev;
  static struct reexpdb_backend_plugin *dbbackend = &sqlite_plug_ops;
 
  /* assert_safe() always evalutes it argument, as it might have
@@ -133,21 +134,44 @@ static void client_cb(evutil_socket_t cl, short ev, void *d)
  	}
  }
 
+static void srv_reenable_cb(evutil_socket_t fd, short ev, void *d)
+{
+	(void)fd;
+	(void)ev;
+	(void)d;
+	if (srv_ev)
+		event_add(srv_ev, NULL);
+}
+
  static void srv_cb(evutil_socket_t fd, short ev, void *d)
  {
  	int cl = accept4(fd, NULL, NULL, SOCK_NONBLOCK);
  	struct event *client_ev;
+
+	if (cl == -1) {
+		if (errno == EMFILE || errno == ENFILE || errno == ENOMEM ||
+				errno == ENOBUFS) {
+			struct timeval tv = {0, 200000};
+			event_del(srv_ev);
+			event_base_once(evbase, -1, EV_TIMEOUT, srv_reenable_cb,
+					NULL, &tv);
+		}
+		return;
+	}
 
  	(void)ev;
  	(void)d;
 
  	client_ev = event_new(evbase, cl, EV_READ | EV_PERSIST | EV_CLOSED, client_cb, event_self_cbarg());
-	event_add(client_ev, NULL);
+	if (!client_ev || event_add(client_ev, NULL) == -1) {
+		if (client_ev)
+			event_free(client_ev);
+		close(cl);
+	}
  }
 
  int main(void)
  {
-	struct event *srv_ev;
  	struct sockaddr_un addr;
  	socklen_t addr_len;
  	char *sock_file;