[GH-ISSUE #75] ngx_http_waf_module 存在内存泄漏 #54

Closed
opened 2026-03-04 12:18:28 +03:00 by kerem · 18 comments
Owner

Originally created by @purplegrape on GitHub (Dec 14, 2021).
Original GitHub issue: https://github.com/ADD-SP/ngx_waf/issues/75

操作系统: amazon linux 2
nginx: 1.20.2
ngx_http_waf_module: 6.1.7

使用ngx_http_waf_module 模块之后,内存明显增加,6.1.4之后应该都有类似问题

nginx运行时间较长之后,内存使用量持续增加,到一定程度之后,error日志中报错 “ngx_slab_alloc() failed: no memory”

应该是内存泄漏无疑了。

Originally created by @purplegrape on GitHub (Dec 14, 2021). Original GitHub issue: https://github.com/ADD-SP/ngx_waf/issues/75 操作系统: amazon linux 2 nginx: 1.20.2 ngx_http_waf_module: 6.1.7 使用ngx_http_waf_module 模块之后,内存明显增加,6.1.4之后应该都有类似问题 nginx运行时间较长之后,内存使用量持续增加,到一定程度之后,error日志中报错 “ngx_slab_alloc() failed: no memory” 应该是内存泄漏无疑了。
kerem 2026-03-04 12:18:28 +03:00
Author
Owner

@purplegrape commented on GitHub (Dec 14, 2021):

image

nginx -s reload 可回收部分内存,但无法阻止内存增长,重启nginx回收内存更彻底。

<!-- gh-comment-id:993301599 --> @purplegrape commented on GitHub (Dec 14, 2021): ![image](https://user-images.githubusercontent.com/7689554/145962391-d83beffc-d450-45e5-8a5d-41ca319a3118.png) nginx -s reload 可回收部分内存,但无法阻止内存增长,重启nginx回收内存更彻底。
Author
Owner

@ADD-SP commented on GitHub (Dec 14, 2021):

内存会无限增长么?因为 LTS 版本的内存回收机制是除非共享内存耗尽,否则不会回收,所以曲线应该是一直上升,然后在峰值左右轻微波动。

<!-- gh-comment-id:993308962 --> @ADD-SP commented on GitHub (Dec 14, 2021): 内存会无限增长么?因为 LTS 版本的内存回收机制是除非共享内存耗尽,否则不会回收,所以曲线应该是一直上升,然后在峰值左右轻微波动。
Author
Owner

@ADD-SP commented on GitHub (Dec 14, 2021):

出现错误日志是应该的,应为只有出错才会意识到共享内存不足,然后回收。

<!-- gh-comment-id:993310006 --> @ADD-SP commented on GitHub (Dec 14, 2021): 出现错误日志是应该的,应为只有出错才会意识到共享内存不足,然后回收。
Author
Owner

@purplegrape commented on GitHub (Dec 14, 2021):

目前配置是 waf_mode STATIC
升级到6.1.7之后,我尝试改为 waf_mode GET POST UA URL 继续观察下

<!-- gh-comment-id:993322504 --> @purplegrape commented on GitHub (Dec 14, 2021): 目前配置是 waf_mode STATIC 升级到6.1.7之后,我尝试改为 waf_mode GET POST UA URL 继续观察下
Author
Owner

@purplegrape commented on GitHub (Dec 14, 2021):

内存会无限增长么?因为 LTS 版本的内存回收机制是除非共享内存耗尽,否则不会回收,所以曲线应该是一直上升,然后在峰值左右轻微波动。

内存增长到一定程度日志会报错ngx_slab_alloc() failed: no memory ,服务器CPU是4核,nginx 1个master+4个worker进程使用最大内存561m,master基本不占内存,worker吃内存。

current 版本的内存机制是否有改善?是否值得一试?

<!-- gh-comment-id:993327433 --> @purplegrape commented on GitHub (Dec 14, 2021): > 内存会无限增长么?因为 LTS 版本的内存回收机制是除非共享内存耗尽,否则不会回收,所以曲线应该是一直上升,然后在峰值左右轻微波动。 内存增长到一定程度日志会报错ngx_slab_alloc() failed: no memory ,服务器CPU是4核,nginx 1个master+4个worker进程使用最大内存561m,master基本不占内存,worker吃内存。 current 版本的内存机制是否有改善?是否值得一试?
Author
Owner

@ADD-SP commented on GitHub (Dec 14, 2021):

只要不是每次请求多会日志报错就行。

Current 版本的内存回收机制已经改了,每次处理请求后就会回收内存(大概),具体的回收行为由环境决定。比如有 4 个 worker 进程,每个进程处理完每个请求后就有 25% 的概率进行一次内存回收。如果某次处理过程中内存不足,则会采用更激进的回收方式。

<!-- gh-comment-id:993329789 --> @ADD-SP commented on GitHub (Dec 14, 2021): 只要不是每次请求多会日志报错就行。 Current 版本的内存回收机制已经改了,每次处理请求后就会回收内存(大概),具体的回收行为由环境决定。比如有 4 个 worker 进程,每个进程处理完每个请求后就有 25% 的概率进行一次内存回收。如果某次处理过程中内存不足,则会采用更激进的回收方式。
Author
Owner

@purplegrape commented on GitHub (Dec 14, 2021):

如果真的是因为共享内存增长快速而惰性回收的话,那应该就不是内存泄漏了,但是ngx_slab_alloc() failed: no memory 这个日志很难解释

<!-- gh-comment-id:993336162 --> @purplegrape commented on GitHub (Dec 14, 2021): 如果真的是因为共享内存增长快速而惰性回收的话,那应该就不是内存泄漏了,但是ngx_slab_alloc() failed: no memory 这个日志很难解释
Author
Owner

@ADD-SP commented on GitHub (Dec 14, 2021):

很容易解释,因为只要共享内存耗尽就会有这个日志。

<!-- gh-comment-id:993336567 --> @ADD-SP commented on GitHub (Dec 14, 2021): 很容易解释,因为只要共享内存耗尽就会有这个日志。
Author
Owner

@purplegrape commented on GitHub (Dec 14, 2021):

我只想要一个basic 的版本,能够简单的维护静态规则就好,我去尝试下current 版本看看

<!-- gh-comment-id:993405205 --> @purplegrape commented on GitHub (Dec 14, 2021): 我只想要一个basic 的版本,能够简单的维护静态规则就好,我去尝试下current 版本看看
Author
Owner

@ADD-SP commented on GitHub (Dec 14, 2021):

忘记说了,新的内存回收机制仅限于使用共享内存的功能,即所有需要指定 zone 参数的配置项,比如 waf_cc_deny。指令 waf_cache 所使用的内存不是共享内存,所以依然是惰性回收。

<!-- gh-comment-id:993466354 --> @ADD-SP commented on GitHub (Dec 14, 2021): 忘记说了,新的内存回收机制仅限于使用共享内存的功能,即所有需要指定 `zone` 参数的配置项,比如 `waf_cc_deny`。指令 `waf_cache` 所使用的内存不是共享内存,所以依然是惰性回收。
Author
Owner

@ADD-SP commented on GitHub (Dec 14, 2021):

@purplegrape 建议尽快更换为 v10.1.0,刚刚发现内存回收机制并未正常运行,虽然不会导致内存泄漏,但是会退化为惰性回收。

<!-- gh-comment-id:993499179 --> @ADD-SP commented on GitHub (Dec 14, 2021): @purplegrape 建议尽快更换为 v10.1.0,刚刚发现内存回收机制并未正常运行,虽然不会导致内存泄漏,但是会退化为惰性回收。
Author
Owner

@purplegrape commented on GitHub (Dec 20, 2021):

升级到10.1.0之后,4个workder, 内存直接干到500多m,虽然目前没什么异常,但感觉太废内存了。

image

配置文件
http {
...
waf on;
waf_mode STD;
waf_rule_path /etc/nginx/ngx_http_waf/rules/;
...
}

<!-- gh-comment-id:997559611 --> @purplegrape commented on GitHub (Dec 20, 2021): 升级到10.1.0之后,4个workder, 内存直接干到500多m,虽然目前没什么异常,但感觉太废内存了。 ![image](https://user-images.githubusercontent.com/7689554/146706907-a7e5b2e9-57f3-41e9-baaf-224fdd34435b.png) 配置文件 http { ... waf on; waf_mode STD; waf_rule_path /etc/nginx/ngx_http_waf/rules/; ... }
Author
Owner

@ADD-SP commented on GitHub (Dec 20, 2021):

其实昨天已经发现内存泄漏问题了,只是没时间修复,缓解方法就是 reload。

<!-- gh-comment-id:997562304 --> @ADD-SP commented on GitHub (Dec 20, 2021): 其实昨天已经发现内存泄漏问题了,只是没时间修复,缓解方法就是 reload。
Author
Owner

@purplegrape commented on GitHub (Dec 20, 2021):

可以使用sonarqube之类的静态工具多检查一下代码
真心建议功能不要贪多求全,简单好用才是王道

<!-- gh-comment-id:997566417 --> @purplegrape commented on GitHub (Dec 20, 2021): 可以使用sonarqube之类的静态工具多检查一下代码 真心建议功能不要贪多求全,简单好用才是王道
Author
Owner

@ADD-SP commented on GitHub (Dec 20, 2021):

已经在分支 current-dev 修复,可以下载重新安装试试。

<!-- gh-comment-id:997826352 --> @ADD-SP commented on GitHub (Dec 20, 2021): 已经在分支 `current-dev` 修复,可以下载重新安装试试。
Author
Owner

@purplegrape commented on GitHub (Dec 22, 2021):

还是等你发正式版吧

<!-- gh-comment-id:999199070 --> @purplegrape commented on GitHub (Dec 22, 2021): 还是等你发正式版吧
Author
Owner

@stale[bot] commented on GitHub (Jan 5, 2022):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
此 issue 因为最近没有任何活动已经被标记,如果在此之后的一段时间内仍没有任何活动则会被关闭。感谢您对项目的支持。

<!-- gh-comment-id:1005609729 --> @stale[bot] commented on GitHub (Jan 5, 2022): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. 此 issue 因为最近没有任何活动已经被标记,如果在此之后的一段时间内仍没有任何活动则会被关闭。感谢您对项目的支持。
Author
Owner

@ADD-SP commented on GitHub (Jan 7, 2022):

This bug has been fixed in v6.1.8 and v10.1.1.

此 bug 已经在 v6.1.8v10.1.1 中修复。

<!-- gh-comment-id:1007127929 --> @ADD-SP commented on GitHub (Jan 7, 2022): This bug has been fixed in `v6.1.8` and `v10.1.1`. 此 bug 已经在 `v6.1.8` 和 `v10.1.1` 中修复。
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ngx_waf#54
No description provided.