Output body buffer chain "flattening" doesn't work unless proxy buffering is off

Srebrenko Šehić ssehic at gmail.com
Sat Jun 5 01:56:25 MSD 2010


Hi list,

While working on an output body filter, I've noticed that if I flatten
(join all buffers from a chain into 1 buffer), response processing
hangs when nginx decides to start buffering the response from the
upstream server. This usually happens (in my case) when the upstream
response size is >64k. If I set "proxy_buffering" to "off" in my
config, everything works just fine.

Following code snippet illustrates what I'm trying to do (simplified
for brevity):

ngx_uint_t  bsize, size;
ngx_chain_t *cl;
ngx_buf_t   *b;

for (cl = in; cl; cl = cl->next) {
    size += ngx_buf_size(cl->buf);
}

if (size == 0) {
    return NGX_DONE;
}

b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
p = ngx_pcalloc(r->pool, size);

b->pos = p;
b->memory = 1;
b->last = b->pos + size;

for (cl = ctx->in; cl; cl = cl->next) {
    bsize = ngx_buf_size(cl->buf);
    ngx_memcpy(p + offset, cl->buf->pos, bsize);
    offset += bsize;
}

in->buf = b;
in->next = NULL;

return ngx_http_next_body_filter(r, in);

Error log shows the following when the response processing hangs:

2010/06/04 23:33:18 [debug] 7689#0: *1 http copy filter: 0 "/test_case_6.html?"
2010/06/04 23:33:23 [debug] 7689#0: *1 http upstream request:
"/test_case_6.html?"
2010/06/04 23:33:23 [debug] 7689#0: *1 http upstream process upstream
2010/06/04 23:33:23 [debug] 7689#0: *1 http output filter "/test_case_6.html?"
2010/06/04 23:33:23 [debug] 7689#0: *1 http copy filter: "/test_case_6.html?"
2010/06/04 23:33:23 [debug] 7689#0: *1 http postpone filter
"/test_case_6.html?" 00000000
2010/06/04 23:33:23 [debug] 7689#0: *1 http copy filter: 0 "/test_case_6.html?"
2010/06/04 23:33:23 [debug] 7689#0: *1 http output filter "/test_case_6.html?"
2010/06/04 23:33:23 [debug] 7689#0: *1 http copy filter: "/test_case_6.html?"
2010/06/04 23:33:23 [debug] 7689#0: *1 http postpone filter
"/test_case_6.html?" 00000000

What I'm doing wrong?

Thanks,
Srebrenko



More information about the nginx-devel mailing list