FD_SETSIZE不够问题排查

升级php 7.4的时候发现有warning报错,报错内容大致是

You MUST recompile PHP with a larger value of FD_SETSIZE....

下载 php 7.4.16 源码进行排查。

grep "You MUST recompile PHP with a larger value" -rn *

找到报错的位置
main/network.c:1174: “You MUST recompile PHP with a larger value of FD_SETSIZE.\n”
代码片段如下

PHPAPI void _php_emit_fd_setsize_warning(int max_fd)
{
#ifdef PHP_WIN32
        php_error_docref(NULL, E_WARNING,
                "PHP needs to be recompiled with a larger value of FD_SETSIZE.\n"
                "If this binary is from an official www.php.net package, file a bug report\n"
                "at http://bugs.php.net, including the following information:\n"
                "FD_SETSIZE=%d, but you are using %d.\n"
                " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
                "to match to maximum number of sockets each script will work with at\n"
                "one time, in order to avoid seeing this error again at a later date.",
                FD_SETSIZE, max_fd, (max_fd + 128) & ~127);
#else
        php_error_docref(NULL, E_WARNING,
                "You MUST recompile PHP with a larger value of FD_SETSIZE.\n"
                "It is set to %d, but you have descriptors numbered at least as high as %d.\n"
                " --enable-fd-setsize=%d is recommended, but you may want to set it\n"
                "to equal the maximum number of open files supported by your system,\n"
                "in order to avoid seeing this error again at a later date.",
                FD_SETSIZE, max_fd, (max_fd + 1024) & ~1023);
#endif
}

继续搜 _php_emit_fd_setsize_warning 这个函数,找到定义如下
vim main/php_network.h +220

 # define PHP_SAFE_MAX_FD(m, n)          do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)

再搜 PHP_SAFE_MAX_FD 这个函数,找到 stream_select 函数的实现,只保留 max_fd相关代码的如下
vim ext/standard/streamsfuncs.c +797

PHP_FUNCTION(stream_select)
{
        php_socket_t max_fd = 0;
        if (r_array != NULL) {
                set_count = stream_array_to_fd_set(r_array, &rfds, &max_fd);
                if (set_count > max_set_count)
                        max_set_count = set_count;
                sets += set_count;
        }
        if (w_array != NULL) {
                set_count = stream_array_to_fd_set(w_array, &wfds, &max_fd);
                if (set_count > max_set_count)
                        max_set_count = set_count;
                sets += set_count;
        }
        if (e_array != NULL) {
                set_count = stream_array_to_fd_set(e_array, &efds, &max_fd);
                if (set_count > max_set_count)
                        max_set_count = set_count;
                sets += set_count;
        }
        
        PHP_SAFE_MAX_FD(max_fd, max_set_count);        
}

其中调用了函数 stream_array_to_fd_set,该函数定义在
vim ext/standard/streamsfuncs.c +607
核心逻辑是循环遍历当前的socket,找到找到当前监听的socket中fd的最大值。

梳理整个链路,可以得出的结论是,在调用stream_select时,如果监听的socket中出现了超过FD_SETSIZE 的fd值,就会出现这个 WARNING信息。

FD_SETSIZE

这里提到 https://bugs.php.net/bug.php?id=37025 的一个解决办法是
修改 /usr/include/bits/typesizes.h 中 __FD_SETSIZE 的值,然后重新编译PHP

/* Number of descriptors that can fit in an `fd_set'.  */
#define __FD_SETSIZE            1024

由于每个进程的fd是隔离的,单个php-fpm进程连接rabbitmq的socket数超过1024实在很难想象,猜测的话,有两种可能,
1)代码存在bug,导致不断尝试建立到rabbitmq的连接,最终超过了1024个;
2)建立mq连接后,没有及时释放;

在amqplib中 https://github.com/php-amqplib/php-amqplib/issues/693
也提到 修改另一个参数 LimitNPROC

LimitNPROC=4096

–enable-fd-setsize 设置描述集的大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值