[PATCH] Events: closed unused listening fds in worker processes.

Zexuan Luo spacewanderlzx at gmail.com
Thu Apr 18 10:18:09 UTC 2019


When reuseport is enabled in the 'listen ...' directive and a great
number of worker processes are used, each worker will inherit lots of
listening fds from the master process.
In one of our environments, one worker could have 1.5k fds before it
starts to accept any request. This situation is too scary to the
system administrator.

Zexuan Luo <spacewanderlzx at gmail.com> 于2019年4月18日周四 下午6:13写道:
>
> # HG changeset patch
> # User spacewander <spacewanderlzx at gmail.com>
> # Date 1555581540 -28800
> #      Thu Apr 18 17:59:00 2019 +0800
> # Branch ident
> # Node ID 4fb5603514f2d324c50cb7e12c6ed0dc8935de0f
> # Parent  e0834ca20c9c68c4f0728f85efb3651732134ee2
> Events: closed unused listening fds in worker processes.
>
> It doesn't affect the listening socket, since there is
> only one used fd which associates to the socket with or
> without this change.
>
> diff -r e0834ca20c9c -r 4fb5603514f2 src/core/ngx_connection.c
> --- a/src/core/ngx_connection.c    Thu Apr 05 12:33:14 2018 +0800
> +++ b/src/core/ngx_connection.c    Thu Apr 18 17:59:00 2019 +0800
> @@ -975,6 +975,14 @@
>      ls = cycle->listening.elts;
>      for (i = 0; i < cycle->listening.nelts; i++) {
>
> +#if (NGX_HAVE_REUSEPORT)
> +
> +        if (ls[i].fd == (ngx_socket_t) -1) {
> +            continue;
> +        }
> +
> +#endif
> +
>          c = ls[i].connection;
>
>          if (c) {
> diff -r e0834ca20c9c -r 4fb5603514f2 src/event/ngx_event.c
> --- a/src/event/ngx_event.c    Thu Apr 05 12:33:14 2018 +0800
> +++ b/src/event/ngx_event.c    Thu Apr 18 17:59:00 2019 +0800
> @@ -735,6 +735,18 @@
>
>  #if (NGX_HAVE_REUSEPORT)
>          if (ls[i].reuseport && ls[i].worker != ngx_worker) {
> +            ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
> +                           "close unused listening %V #%d ",
> +                           &ls[i].addr_text, ls[i].fd);
> +
> +            if (ngx_close_socket(ls[i].fd) == -1) {
> +                ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
> +                              ngx_close_socket_n " %V failed",
> +                              &ls[i].addr_text);
> +            }
> +
> +            ls[i].fd = (ngx_socket_t) -1;
> +
>              continue;
>          }
>  #endif


More information about the nginx-devel mailing list