# Request hangs

**URL:** <https://www.ruby-forum.com/t/request-hangs/203742>\
**Category:** NGINX\
**Created:** [February 26, 2011, 2:29am UTC](https://www.ruby-forum.com/t/request-hangs/203742 "2011-02-26T02:29:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 26, 2011, 2:29am UTC](https://www.ruby-forum.com/t/request-hangs/203742/1 "2011-02-26T02:29:34Z")

</div>

Hi,

I have strange problem with main request hand if I call for  
sub-request. Maybe somebody can help me with it?

I do spam filtering in post request. So I added location to nginx.conf  
and call sub-request for that location. Then read output from  
sub-request and set up nginx variable according to output.

So config looks like:  
location = /checkspam {  
internal;  
include spam\_fastcgi.conf;  
}

SOURCE code is here:  
[http://codepaste.net/17tfmu](http://codepaste.net/17tfmu)

> From client side it looks like client can not receive last chank of  
> original request (last 10-50 bytes). And it’s not easy to understand  
> what is going on. Any advises are welcome.

I put debug log here:  
[http://codepaste.net/ceiian](http://codepaste.net/ceiian)

Thanks,  
Max.

Posted at Nginx Forum:

> **[request hangs](https://forum.nginx.org/read.php?2,178511,178511#msg-178511)**
>
> Hi, I have strange problem with main request hand if I call for sub-request. Maybe somebody can help me with it? I do spam filtering in post request. So I added location to nginx.conf and call sub-request for that location. Then read output from...

---

<div class="post-metadata">

**Author:** ![magz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@magz](https://www.ruby-forum.com/u/magz)\
**Post date:** [February 28, 2011, 7:35am UTC](https://www.ruby-forum.com/t/request-hangs/203742/2 "2011-02-28T07:35:52Z")

</div>

## magz Wrote:

> From client side it looks like client can not  
> receive last chank of original request (last 10-50  
> bytes). And it’s not easy to understand what is  
> going on. Any advises are welcome.
> 
> I put debug log here:  
> [http://codepaste.net/ceiian](http://codepaste.net/ceiian)
> 
> Thanks,  
> Max.

Silence here means that question was not clear or nobody knows the  
answer. I will try to ask my question in another way. If you know the  
answer please respond to me.

I do:

1. register handler in REWRITE\_PHASE  
h = ngx\_array\_push(&cmcf-\>phases[NGX\_HTTP\_REWRITE\_PHASE].handlers);  
\*h = ngx\_http\_form\_input\_handler;

2. In ngx\_http\_form\_input\_handler() read POST body and act the  
handler:  
r-\>request\_body\_in\_single\_buf = 1;  
rc = ngx\_http\_read\_client\_request\_body(r,  
ngx\_http\_form\_input\_post\_read);

3. In ngx\_http\_form\_input\_post\_read() call subsequent with POST body to  
different location  
psr-\>handler = ngx\_http\_form\_input\_post\_subrequest\_handler;  
rc = ngx\_http\_subrequest(r, &uri, &args, &sr, psr, 0);

Now the question is why original request hands on returning last buffer  
chank to client?

It looks like client holds on read() forever. If kill signal is sent to  
nginx client receives this last buffer. Also sub-request works fine  
(send all POST data from client and receives what is should receive).  
Without sub-request everything works fine too.

Is it know nginx issue?

What workaround can be applied here? Maybe create create additional  
filter and terminate request? Or adjust request buffers after  
sub-request is done?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,178511,179019#msg-179019](http://forum.nginx.org/read.php?2,178511,179019#msg-179019)

---

<div class="post-metadata">

**Author:** ![magz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@magz](https://www.ruby-forum.com/u/magz)\
**Post date:** [February 28, 2011, 8:52am UTC](https://www.ruby-forum.com/t/request-hangs/203742/3 "2011-02-28T08:52:18Z")

</div>

On Mon, Feb 28, 2011 at 2:33 PM, magz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## magz Wrote:
> 
> > Hi,
> > 
> > I have strange problem with main request hand if  
> > I call for sub-request. Maybe somebody can help  
> > me with it?

Which version of nginx are you using?

If you’re using nginx 0.8.x and above, the symptom suggests you didn’t  
get r-\>main-\>count right. Use --with-debug while building your nginx,  
and track the values of r-\>main-\>count.

For example, in the following line of my local error.log,

```
2011/01/28 13:01:50 [debug] 1199#0: *1 http finalize request: -4,

```

“/main?” a:0, c:3

a:0 means the current request is not the active request at the moment  
(i.e., r != r-\>connection-\>data), and c:3 means r-\>main-\>count is 3.  
The request can only be successfully finalized unless it is the  
current active request. Also, the main request won’t terminate, i.e.,  
will hang if the r-\>main-\>count counter is not going to be decremented  
to zero. Well, r-\>main-\>count is just a reference counter for the main  
request object. A simple way of doing poors’ “garbage collection”,  
well, at least kind of.

When doing subrequest programming on the C level, one should be VERY  
VERY careful with these two values, among other things 🙂

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![magz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@magz](https://www.ruby-forum.com/u/magz)\
**Post date:** [February 28, 2011, 9:24am UTC](https://www.ruby-forum.com/t/request-hangs/203742/4 "2011-02-28T09:24:55Z")

</div>

## agentzh Wrote:

> help
> 
> r-\>main-\>count is 3.  
> well, at least kind of.  
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Thanks for the hint agentzh! Will try to dig in that area.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,178511,179039#msg-179039](http://forum.nginx.org/read.php?2,178511,179039#msg-179039)

---

<div class="post-metadata">

**Author:** ![magz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/47e85d/32.png) [@magz](https://www.ruby-forum.com/u/magz)\
**Post date:** [February 28, 2011, 7:28pm UTC](https://www.ruby-forum.com/t/request-hangs/203742/5 "2011-02-28T19:28:24Z")

</div>

r-\>count–; to the right place fixed my problem.

Thanks agentzh again!

Posted at Nginx Forum:

> **[Re: request hangs](https://forum.nginx.org/read.php?2,178511,179222#msg-179222)**
>
> Hi, I have strange problem with main request hand if I call for sub-request. Maybe somebody can help me with it? I do spam filtering in post request. So I added location to nginx.conf and call sub-request for that location. Then read output from...
