nginx -s reload 后出现:nginx: worker process is shutting down的问题

1,574 views
Skip to first unread message

jie123108

unread,
Apr 15, 2014, 11:29:13 PM4/15/14
to openresty
春哥,你好!
我们在线上环境运行nginx -s reload 后,就会出现下面这种情况:
[root@VIP17 scripts]# ps -ef|grep nginx                
root      2047   629  0 11:13 pts/9    00:00:00 grep nginx
root     32425     1  0 10:52 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   32426 32425  0 10:52 ?        00:00:01 nginx: worker process is shutting down
nobody   32430 32425  0 10:52 ?        00:00:00 nginx: worker process is shutting down
nobody   32431 32425  0 10:52 ?        00:00:01 nginx: worker process is shutting down
nobody   32432 32425  0 10:52 ?        00:00:00 nginx: worker process is shutting down
nobody   32433 32425  0 10:52 ?        00:00:00 nginx: worker process is shutting down
nobody   32434 32425  0 10:52 ?        00:00:01 nginx: worker process is shutting down
nobody   32435 32425  0 10:52 ?        00:00:00 nginx: worker process is shutting down
nobody   32436 32425  0 10:52 ?        00:00:00 nginx: worker process is shutting down
nobody   32640 32425  0 10:54 ?        00:00:05 nginx: worker process      
nobody   32641 32425  0 10:54 ?        00:00:04 nginx: worker process      
nobody   32642 32425  0 10:54 ?        00:00:04 nginx: worker process      
nobody   32643 32425  0 10:54 ?        00:00:09 nginx: worker process      
nobody   32644 32425  0 10:54 ?        00:00:05 nginx: worker process      
nobody   32645 32425  0 10:54 ?        00:00:04 nginx: worker process      
nobody   32646 32425  0 10:54 ?        00:00:05 nginx: worker process      
nobody   32647 32425  0 10:54 ?        00:00:06 nginx: worker process      
 
#其中一个进程,pstack看到是这样的:
pstack 32426
#0  0x0000003e194d4473 in __epoll_wait_nocancel () from /lib64/libc.so.6
#1  0x0000000000420f9c in ngx_epoll_process_events ()
#2  0x000000000041a2c8 in ngx_process_events_and_timers ()
#3  0x000000000041fdd0 in ngx_worker_process_cycle ()
#4  0x000000000041e6e7 in ngx_spawn_process ()
#5  0x000000000041f473 in ngx_start_worker_processes ()
#6  0x0000000000420334 in ngx_master_process_cycle ()
#7  0x000000000040618a in main ()
 
strace -p 32426
Process 32426 attached - interrupt to quit
epoll_wait(35,  <unfinished ...>
 
# 新启动的进程的:
pstack 32641
#0  0x0000003e194d4473 in __epoll_wait_nocancel () from /lib64/libc.so.6
#1  0x0000000000420f9c in ngx_epoll_process_events ()
#2  0x000000000041a2c8 in ngx_process_events_and_timers ()
#3  0x000000000041fdd0 in ngx_worker_process_cycle ()
#4  0x000000000041e6e7 in ngx_spawn_process ()
#5  0x000000000041f473 in ngx_start_worker_processes ()
#6  0x0000000000420921 in ngx_master_process_cycle ()
#7  0x000000000040618a in main ()
 strace -p 32641
Process 32641 attached - interrupt to quit
epoll_wait(8, {{EPOLLIN|EPOLLOUT, {u32=3960849105, u64=47901436134097}}}, 512, 500) = 1
recvfrom(67, "POST /user/xxxx/ HTTP/1."..., 1024, 0, NULL, NULL) = 307
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 71
ioctl(71, FIONBIO, [1])                 = 0
epoll_ctl(8, EPOLL_CTL_ADD, 71, {EPOLLIN|EPOLLOUT|EPOLLET, {u32=3960851984, u64=47901436136976}}) = 0
connect(71, {sa_family=AF_INET, sin_port=htons(9998), sin_addr=inet_addr("xx.xx.xx.xx")}, 16) = -1 EINPROGRESS (Operation now in progress)
recvfrom(67, 0x7fff3ee49d37, 1, 2, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
epoll_wait(8, {{EPOLLOUT, {u32=3960851984, u64=47901436136976}}}, 512, 500) = 1
getsockopt(71, SOL_SOCKET, SO_ERROR, [-1435026707638648832], [4]) = 0
后面省略....
 
编译命令是这样的:
./configure --prefix=/usr/local/nginx-1.0.15_city_mod --with-http_stub_status_module \
--with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module \
--with-openssl=/usr/local/openssl-1.0.1c --with-pcre=../pcre-8.10 \
--add-module=../nginx_tcp_proxy_module-0.4.4 \
--add-module=../nginx_upload_module-2.0.12 \
--add-module=../ngx_cache_purge-1.3 \
--with-ld-opt=-g \
--with-http_secure_link_module \
--add-module=/home/lxj/ngx_city_mod \
--add-module=/home/lxj/ngx_req_stat 
 
其中最后面两个是自己的模块,一个是请求统计模块,一个是使用IP定位城市的模块。
 
操作系统版本:
lsb_release -a
LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 5.6 (Final)
Release:        5.6
Codename:       Final
 
 
其中的req_stat模块,实现了init process 方法,里面启动了一个定时器,定时刷统计信息到mongodb中。
exit process 原来也有做刷新数据到mongodb中的操作,现在去掉了(开始我怀疑是这个引起的,后面确认去掉后问题依旧)。
 
出现这种问题,会是什么原因? 会是我自己的模块出问题导致的么?
 
 
 
 

jie123108

Yichun Zhang (agentzh)

unread,
Apr 16, 2014, 3:42:31 PM4/16/14
to openresty
Hello!

On Tue, Apr 15, 2014 at 8:29 PM, jie123108 wrote:
> 我们在线上环境运行nginx -s reload 后,就会出现下面这种情况:
> [root@VIP17 scripts]# ps -ef|grep nginx
> root 2047 629 0 11:13 pts/9 00:00:00 grep nginx
> root 32425 1 0 10:52 ? 00:00:00 nginx: master process
> /usr/local/nginx/sbin/nginx
> nobody 32426 32425 0 10:52 ? 00:00:01 nginx: worker process is
> shutting down

"is shutting down" 这个状态是指对应的 nginx worker
进程收到了退出指令,但它还需要等待当前处理中的请求全部结束才结束自己的生命,同时它不再接受新的请求。这便是所谓的“优雅退出”,为的是避免影响
nginx 当前已经开始处理的请求。

nginx worker 进程判断当前正在处理的请求已经全部退出的一个标准是检查当前是否还有未触发的活跃的定时器(timer)。

> 其中的req_stat模块,实现了init process 方法,里面启动了一个定时器,定时刷统计信息到mongodb中。

看起来是这个模块的定时器的问题。这个模块在创建新的定时器之前应当检查 C 全局变量 ngx_exiting 的值,为真时即当前 worker
已接收到退出指令,此时该模块应该不再创建新的定时器。否则不断创建的新的定时器会让 worker 进程认为当前还有没有结束的请求。

你可以参考 ngx_lua 模块的 timer 部分的实现:

https://github.com/chaoslawful/lua-nginx-module/blob/master/src/ngx_http_lua_timer.c#L94

它就对此种情况进行了恰当的处理。

Regards,
-agentzh

jie123108

unread,
Apr 17, 2014, 5:43:54 AM4/17/14
to openresty
谢谢春哥,这么一说我完全明白了。
我已经按你说的修改了:
1.创建新的定时器之前检查ngx_exiting,为真时不创建。
2.exit_process 方法中,删除还未执行的定时器。
 
目前测试已经完全正常了。
 
 

jie123108
 
发送时间: 2014-04-17 03:42
收件人: openresty
主题: Re: [openresty] nginx -s reload 后出现:nginx: worker process is shutting down的问题
-- 
-- 
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

eason feng

unread,
Jan 11, 2019, 2:26:20 AM1/11/19
to openresty
春哥,你好!

我在openresty上集成了interl的QAT卡的驱动,其中,也出现了类似的Shutting down不掉的问题,打印出对战信息如下:

[root@gateway_10 ~]# pstack 111072
Thread 2 (Thread 0x7f6a0478d700 (LWP 111073)):
#0  0x00007f6a08271bdd in nanosleep () from /lib64/libpthread.so.0
#1  0x00007f6a0653a1dc in timer_poll_func (ih=<optimized out>) at qat_polling.c:189
#2  0x00007f6a0826adc5 in start_thread () from /lib64/libpthread.so.0
#3  0x00007f6a06c7973d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f6a0888b740 (LWP 111072)):
#0  0x00007f6a06c79d13 in epoll_wait () from /lib64/libc.so.6
#1  0x000055662ea88303 in ngx_epoll_process_events (cycle=0x556630de5f80, timer=1540, flags=1) at src/event/modules/ngx_epoll_module.c:863
#2  0x000055662ea7f91f in ngx_process_events_and_timers (cycle=cycle@entry=0x556630de5f80) at src/event/ngx_event.c:252
#3  0x000055662ea86471 in ngx_worker_process_cycle (cycle=cycle@entry=0x556630de5f80, data=data@entry=0x13) at src/os/unix/ngx_process_cycle.c:822
#4  0x000055662ea84e16 in ngx_spawn_process (cycle=cycle@entry=0x556630de5f80, proc=0x55662ea863f0 <ngx_worker_process_cycle>, data=0x13, name=0x55662eb7df93 "worker process", respawn=respawn@entry=19) at src/os/unix/ngx_process.c:198
#5  0x000055662ea87725 in ngx_reap_children (cycle=0x556630de5f80) at src/os/unix/ngx_process_cycle.c:694
#6  ngx_master_process_cycle (cycle=cycle@entry=0x556630de5f80) at src/os/unix/ngx_process_cycle.c:180
#7  0x000055662ea6002d in main (argc=<optimized out>, argv=<optimized out>) at src/core/nginx.c:387


请问应该如何处理呀,谢谢~~~

tokers

unread,
Jan 11, 2019, 2:28:43 AM1/11/19
to openresty
Hello!

集成 QAT 应该不会导致旧进程无法退出的问题,可能还是其他逻辑引起的。

Yuansheng Wang

unread,
Jan 11, 2019, 4:23:10 AM1/11/19
to open...@googlegroups.com
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html


--

Yuansheng +86-18600159913
OpenResty Inc.
Reply all
Reply to author
Forward
0 new messages