# Ngx\_http\_request\_t's subrequests is what meaning be ï¼Ÿ

**URL:** <https://www.ruby-forum.com/t/ngx-http-request-ts-subrequests-is-what-meaning-be-i-y/190950>\
**Category:** NGINX\
**Created:** [July 1, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/ngx-http-request-ts-subrequests-is-what-meaning-be-i-y/190950 "2010-07-01T14:10:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![monad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@monad](https://www.ruby-forum.com/u/monad)\
**Post date:** [July 1, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/ngx-http-request-ts-subrequests-is-what-meaning-be-i-y/190950/1 "2010-07-01T14:10:00Z")

</div>

hi all.

I am reading nginx’s source(version is 0.7.66), but i don’t understand  
this  
section:

ngx\_int\_t  
ngx\_http\_subrequest(ngx\_http\_request\_t \*r,  
ngx\_str\_t \*uri, ngx\_str\_t \*args, ngx\_http\_request\_t \*\*psr,  
ngx\_http\_post\_subrequest\_t \*ps, ngx\_uint\_t flags)  
{  
ngx\_connection\_t \*c;  
ngx\_http\_request\_t \*sr;  
ngx\_http\_core\_srv\_conf\_t \*cscf;  
ngx\_http\_postponed\_request\_t \*pr, \*p;

//i know subrequests’s default is NGX\_HTTP\_MAX\_SUBREQUESTS + 1 = 51;  
r-\>main-\>subrequests–;

//when subrequests is 0?  
if (r-\>main-\>subrequests == 0) {  
ngx\_log\_error(NGX\_LOG\_ERR, r-\>connection-\>log, 0,  
“subrequests cycle while processing "%V"”, uri);  
r-\>main-\>subrequests = 1;  
return NGX\_ERROR;  
}  
…

//why subrequests need plus plus?  
r-\>main-\>subrequests++;

```
*psr = sr;

return ngx_http_post_request(sr);

```

}

the problem is after call ngx\_http\_subrequest and r-\>main-\>subrequests  
will  
not changeï¼ˆbecause subrequests-- and then ++), so when subrequests is  
0ï¼Ÿ

I think if r-\>main-\>subrequests is the max subrequests number,  
r-\>main-\>subrequests++  
need to be removed.

## thanks.

åšè§‚çº¦å–

douban :www.douban.com/people/mustang/

blog: [simohayha.javaeye.com](http://simohayha.javaeye.com)

twitter: [www.twitter.com/minibobo](http://www.twitter.com/minibobo)

---

<div class="post-metadata">

**Author:** ![monad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@monad](https://www.ruby-forum.com/u/monad)\
**Post date:** [July 1, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/ngx-http-request-ts-subrequests-is-what-meaning-be-i-y/190950/2 "2010-07-01T14:32:50Z")

</div>

if r-\>main-\>subrequests is avoid subrequests cycle, so why need  
r-\>main-\>subrequests++  
? because i think r-\>main-\>subrequests-- and judge r-\>main-\>subrequests  
is  
or not 0 can avoid subrequests cycle.

thanks.

---

<div class="post-metadata">

**Author:** ![monad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@monad](https://www.ruby-forum.com/u/monad)\
**Post date:** [July 1, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/ngx-http-request-ts-subrequests-is-what-meaning-be-i-y/190950/3 "2010-07-01T16:08:52Z")

</div>

Hello!

On Thu, Jul 01, 2010 at 08:08:42PM +0800, BoBo wrote:

> ```
> ngx_connection_t *c;
> "subrequests cycle while processing \"%V\"", uri);
> return ngx_http_post_request(sr);
> 
> ```
> 
> }
> 
> the problem is after call ngx\_http\_subrequest and r-\>main-\>subrequests will  
> not changeï¼ˆbecause subrequests-- and then ++), so when subrequests is 0ï¼Ÿ

Right now - it would never be 0 (and this strikes badly as long as  
you issue \> 255 subrequests, as r-\>main-\>counter overflows). This  
logic was introduced for older subrequest implementation where  
subrequests were completed directly. Subrequest processing was  
changed in 0.7.25 and this subrequests-- and subrequests++ no  
longer make sense.

I’ve posted a patch a while ago which moves r-\>main-\>subrequests++  
into request finalization, see here (in Russian):

> **[worker process exited on signal 11 (core dumped)](http://mailman.nginx.org/pipermail/nginx-ru/2010-February/032184.html)**

Maxim D.

---

<div class="post-metadata">

**Author:** ![monad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@monad](https://www.ruby-forum.com/u/monad)\
**Post date:** [July 2, 2010, 10:48am UTC](https://www.ruby-forum.com/t/ngx-http-request-ts-subrequests-is-what-meaning-be-i-y/190950/4 "2010-07-02T10:48:45Z")

</div>

Hello!

On Fri, Jul 02, 2010 at 11:03:20AM +0800, BoBo wrote:

> thanks , but now nginx use what to avoid subrequests cycle?
> 
> I think your patch doesn’t avoid subrequests cycle, because the certain may  
> call ngx\_http\_subrequest again finalize requestï¼ˆthe newest subrequest), and  
> then cause subrequests cycle.
> 
> so i think should remove r-\>main-\>subrequests++.

Removing r-\>main-\>subrequests++ completely will limit total number  
of subrequests to NGX\_HTTP\_MAX\_SUBREQUESTS. This isn’t intended  
(and will cause problems in many configurations, e.g. in the thread  
I linked it exceeded 255 without any loops).

Limit on number of subrequests was always meant to limit number of  
simulteneously executing subrequests, and this is what currently  
causes problems (due to r-\>main-\>count overflow and resulting  
SIGSEGV).

In general, I see three options on how to handle this:

1. Resurrect r-\>main-\>subrequests limit as in patch linked.

2. Drop r-\>main-\>subrequests limit and check for r-\>main-\>count  
overflows intead.

3. Drop r-\>main-\>subrequests limit and introduce something like  
subrequest recursion counter to limit depth of subrequests, not  
it’s count. This implies (2) or bumping r-\>main-\>count to  
some bigger datatype to avoid overflows.

Maxim D.

---

<div class="post-metadata">

**Author:** ![monad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@monad](https://www.ruby-forum.com/u/monad)\
**Post date:** [July 2, 2010, 5:04am UTC](https://www.ruby-forum.com/t/ngx-http-request-ts-subrequests-is-what-meaning-be-i-y/190950/5 "2010-07-02T05:04:35Z")

</div>

thanks , but now nginx use what to avoid subrequests cycle?

I think your patch doesn’t avoid subrequests cycle, because the certain  
may  
call ngx\_http\_subrequest again finalize requestï¼ˆthe newest subrequest),  
and  
then cause subrequests cycle.

so i think should remove r-\>main-\>subrequests++.

On Thu, Jul 1, 2010 at 10:08 PM, Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ngx\_int\_t  
> > r-\>main-\>subrequests–;  
> > //why subrequests need plus plus?
> 
> [worker process exited on signal 11 (core dumped)](http://nginx.org/pipermail/nginx-ru/2010-February/032184.html)
> 
> Maxim D.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
åšè§‚çº¦å–

è±†ç“£:www.douban.com/people/mustang/

blog: [simohayha.javaeye.com](http://simohayha.javaeye.com)

twitter: [Minibobo (@minibobo) / X](http://www.twitter.com/minibobo)
