nginx 100% cpu and it may be a nginx bug

Maxim Dounin mdounin at mdounin.ru
Tue Nov 22 11:01:55 UTC 2011


Hello!

On Tue, Nov 22, 2011 at 05:38:11AM -0500, wangbin579 wrote:

> The function ngx_http_upstream_get_round_robin_peer in nginx will loop
> for a long period of times when pc->tries equals to 0.
> 
> Well then, under what conditions does pc->tries equal to zero?
> 
> When executing ngx_http_upstream_get_round_robin_peer for the first
> time, in some cases it returns NGX_OK while pc->tries equals to 1. 
> 
> When in ngx_http_upstream_process_header, nginx process may enter the
> following:
> 1524         if (n == NGX_ERROR || n == 0) {
> 1525             ngx_http_upstream_next(r, u,
> NGX_HTTP_UPSTREAM_FT_ERROR);
> 1526             return;
> 1527         }
> 
> then enter ngx_http_upstream_next function
> 
> In the ngx_http_upstream_next,nginx process will call
> ngx_http_upstream_free_round_robin_peer
> 
> 2796     if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {
> 2797         u->peer.free(&u->peer, u->peer.data, state);
> 2798     }
> 
> 
> we now enter ngx_http_upstream_free_round_robin_peer.
> 
> Pay attention to the following codes in the function:
> if (pc->tries) {
>    pc->tries--;
> }
> 
> After executing the above, pc->tries changes from 1 to 0.
> 
> then the process returns to ngx_http_upstream_next
> 
> 2892     ngx_http_upstream_connect(r, u);
> 2893 }

The ngx_http_upstream_next() will finalize request if tries == 0.

You are probably facing the problem with keepalive backend 
connections in 1.0.x (where they aren't officially supported).  It 
is fixed with this changeset while introducing official keepalive 
backend connections support in 1.1.4:

http://trac.nginx.org/nginx/changeset/4116/nginx

Maxim Dounin



More information about the nginx mailing list