# Program hangs when modifying u-\>buffer while using proxy module to talk to upstream backend

**URL:** <https://www.ruby-forum.com/t/program-hangs-when-modifying-u-buffer-while-using-proxy-module-to-talk-to-upstream-backend/218718>\
**Category:** NGINX\
**Created:** [April 10, 2012, 4:02am UTC](https://www.ruby-forum.com/t/program-hangs-when-modifying-u-buffer-while-using-proxy-module-to-talk-to-upstream-backend/218718 "2012-04-10T04:02:30Z")\
**Posts on this page:** 6\
**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:** [April 10, 2012, 4:02am UTC](https://www.ruby-forum.com/t/program-hangs-when-modifying-u-buffer-while-using-proxy-module-to-talk-to-upstream-backend/218718/1 "2012-04-10T04:02:30Z")

</div>

Hi,

I currently use upstream to talk to a back-end, which sends me some  
response. In my module, i am trying to re-format the plain-text  
upstream backend response, to XML, and I also add some new data to it,  
based on an in-memory lookup within my module. What would be the best  
way to do this?

In my setup, I am able to parse response from upstream (u-\>buffer),  
construct a modified string and assign it back to u-\>buffer, and am I  
setting u-\>headers\_in.content\_length\_n and  
r-\>headers\_out.content\_length\_n correctly. The issue i face is, every  
_second_ request hangs, if i am trying the request through the  
browser. (However, trying through command-line & curl, works  
everytime). And I always see this error message in the logs: “client  
prematurely closed connection, so upstream connection is closed too  
while sending to client, client: 127.0.0.1”. What am i doing wrong?

Thanks,  
Ashish

---

<div class="post-metadata">

**Author:** ![ashishsdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@ashishsdev](https://www.ruby-forum.com/u/ashishsdev)\
**Post date:** [April 10, 2012, 7:17am UTC](https://www.ruby-forum.com/t/program-hangs-when-modifying-u-buffer-while-using-proxy-module-to-talk-to-upstream-backend/218718/2 "2012-04-10T07:17:44Z")

</div>

see the fastcgi module as an example

2012/4/10 Ashish S [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![ashishsdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@ashishsdev](https://www.ruby-forum.com/u/ashishsdev)\
**Post date:** [April 10, 2012, 2:14pm UTC](https://www.ruby-forum.com/t/program-hangs-when-modifying-u-buffer-while-using-proxy-module-to-talk-to-upstream-backend/218718/3 "2012-04-10T14:14:23Z")

</div>

Hello!

On Mon, Apr 09, 2012 at 07:02:10PM -0700, Ashish S wrote:

> Hi,
> 
> I currently use upstream to talk to a back-end, which sends me some  
> response. In my module, i am trying to re-format the plain-text  
> upstream backend response, to XML, and I also add some new data to it,  
> based on an in-memory lookup within my module. What would be the best  
> way to do this?

Unless you are working on custom protocol module, you may want to  
use filter module to re-format data instead.

> In my setup, I am able to parse response from upstream (u-\>buffer),  
> construct a modified string and assign it back to u-\>buffer, and am I  
> setting u-\>headers\_in.content\_length\_n and  
> r-\>headers\_out.content\_length\_n correctly. The issue i face is, every  
> _second_ request hangs, if i am trying the request through the  
> browser. (However, trying through command-line & curl, works  
> everytime). And I always see this error message in the logs: “client  
> prematurely closed connection, so upstream connection is closed too  
> while sending to client, client: 127.0.0.1”. What am i doing wrong?

It’s hard to say anything without seeing the code, but most likely  
you fail to set u-\>length properly. See e.g. memcached module for  
a simple example of non-buffered protocol handler.

Maxim D.

---

<div class="post-metadata">

**Author:** ![ashishsdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@ashishsdev](https://www.ruby-forum.com/u/ashishsdev)\
**Post date:** [April 10, 2012, 11:03pm UTC](https://www.ruby-forum.com/t/program-hangs-when-modifying-u-buffer-while-using-proxy-module-to-talk-to-upstream-backend/218718/4 "2012-04-10T23:03:55Z")

</div>

Hi, I figured the issue. Maxim’s intuition was right. u-\>length was  
not set correctly (the u-\>length set in the code i copy-pasted  
earlier, was modified later by a filter method). After fixing the  
issue, the code works correctly, no more hangs.

Thanks,  
Ashish

---

<div class="post-metadata">

**Author:** ![ashishsdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@ashishsdev](https://www.ruby-forum.com/u/ashishsdev)\
**Post date:** [April 10, 2012, 8:16pm UTC](https://www.ruby-forum.com/t/program-hangs-when-modifying-u-buffer-while-using-proxy-module-to-talk-to-upstream-backend/218718/5 "2012-04-10T20:16:34Z")

</div>

Hi Maxim,

Here is a simple test code i wrote, (for my process header method),  
which shows the same behavior.

============  
ngx\_int\_t  
ngx\_http\_my\_process\_header(ngx\_http\_request\_t \*r)  
{  
ngx\_http\_upstream\_t \*u = r-\>upstream;

```
std::string tmpString ("Test string");
ngx_memcpy(u->buffer.start, tmpString.c_str(), tmpString.length());
u->buffer.pos = u->buffer.start;
u->buffer.last = u->buffer.pos + tmpString.length();

u->length = u->headers_in.content_length_n = tmpString.length();
r->headers_out.content_length_n = tmpString.length();
u->headers_in.status_n = NGX_HTTP_OK;
u->state->status = NGX_HTTP_OK;

return NGX_OK;

```

# }

Thanks,  
Ashish

---

<div class="post-metadata">

**Author:** ![ashishsdev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@ashishsdev](https://www.ruby-forum.com/u/ashishsdev)\
**Post date:** [April 26, 2012, 5:17pm UTC](https://www.ruby-forum.com/t/program-hangs-when-modifying-u-buffer-while-using-proxy-module-to-talk-to-upstream-backend/218718/6 "2012-04-26T17:17:10Z")

</div>

Hi Maxim,

> It’s hard to say anything without seeing the code, but most likely  
> you fail to set u-\>length properly. See e.g. memcached module for  
> a simple example of non-buffered protocol handler.

Is there an example i can follow, for buffered protocol handler?

Thanks  
Ashish
