From 99f03a2bce23f55b49d85cd7fc1acbaee3dd22c7 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Fri, 28 Apr 2023 00:03:02 -0400 Subject: [core] always decr fd count upon socket close() always decr fd count in connection_close() always decr fd count in fdevent_sched_run() Error return value from close() should be used for diagnostics and recovery, but the state of the file descriptor is unspecified by POSIX. On most systems, it is invalid to redo close(). (Linux 'man 2 close' suggests that HP-UX is an outlier, and that a future POSIX standard update will specify the behavior for the file descriptor to be closed) EBADF should not happen in those funcs for lighttpd since those should be the only locations in lighttpd where those fds are closed. --- src/connections.c | 5 ++--- src/fdevent_impl.c | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/connections.c b/src/connections.c index 7c849bf1..51afa665 100644 --- a/src/connections.c +++ b/src/connections.c @@ -88,9 +88,7 @@ static void connection_close(connection *con) { fdevent_fdnode_event_del(srv->ev, con->fdn); fdevent_unregister(srv->ev, con->fdn); con->fdn = NULL; - if (0 == fdio_close_socket(con->fd)) - --srv->cur_fds; - else + if (0 != fdio_close_socket(con->fd)) log_serror(r->conf.errh, __FILE__, __LINE__, "(warning) close: %d", con->fd); @@ -100,6 +98,7 @@ static void connection_close(connection *con) { } con->fd = -1; + --srv->cur_fds; connection_del(srv, con); } diff --git a/src/fdevent_impl.c b/src/fdevent_impl.c index a3a1f66b..bd409a85 100644 --- a/src/fdevent_impl.c +++ b/src/fdevent_impl.c @@ -282,14 +282,15 @@ fdevent_sched_run (fdevents * const ev) { for (fdnode *fdn = ev->pendclose; fdn; ) { int fd = fdn->fd; + /*(inlined fdio_close_socket() for tighter loop; worthwhile?)*/ #ifdef _WIN32 if (0 != closesocket(fd)) /* WSAPoll() valid only on SOCKET */ #else if (0 != close(fd)) #endif log_serror(ev->errh, __FILE__, __LINE__, "close() %d", fd); - else - --(*ev->cur_fds); + + --(*ev->cur_fds); fdnode * const fdn_tmp = fdn; fdn = (fdnode *)fdn->ctx; /* next */ -- cgit v1.2.1