You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bugfix: buffer bloat and CPU 100% when download large file was filtered by body_filter_by_lua. (#1909)
When http body was change by lua script, ngx_http_lua_body_filter_param_set will
mark the origin buf as consumed with code "cl->buf->pos = cl->buf->last". Because
the buf was marked as consumed, the function ngx_output_chain will continue to
write data to the output filter.
When kernel socket send buffer is full, write to the socket will return EAGAIN.
And the output buf will be cached in the output chain. The output chain becomes longer and longer.
Function ngx_chain_update_chains do a linear iteration of the output chain to
put the new buf to the end of the chain. In the end, there are thousands of buffs
in the output chain which causes the CPU utilization becomes 100% and memory continue to grow up.
nginx.conf:
location /download {
alias download;
header_filter_by_lua_block {
ngx.header.content_length = nil
}
body_filter_by_lua_block {
ngx.arg[1]=ngx.arg[1]
}
}
create a 2G file with the following cmd:
mkdir -p /usr/local/openresty/nginx/download
dd if=/dev/zero of=/usr/local/openresty/nginx/download/2G count=4096000
reproduce with the following cmd:
curl -o /dev/null http://127.0.0.1/download/2G
typical call stacks:
42706f: ngx_chain_update_chains[14]
47e993: ngx_http_chunked_body_filter[14]
483024: ngx_http_gzip_body_filter[14]
4864d3: ngx_http_ssi_body_filter[14]
489668: ngx_http_charset_body_filter[14]
48b86c: ngx_http_addition_body_filter[14]
48bf9c: ngx_http_gunzip_body_filter[14]
48d75e: ngx_http_trailers_filter[14]
4fb63c: ngx_http_lua_capture_body_filter[14]
509cb9: ngx_http_lua_body_filter[14]
42739e: ngx_output_chain[14]
48e1aa: ngx_http_copy_filter[14]
45ea2b: ngx_http_output_filter[14]
496569: ngx_http_static_handler[14]
45ec6e: ngx_http_core_content_phase[14]
459a25: ngx_http_core_run_phases[14]
463ed3: ngx_http_process_request[14]
4643ff: ngx_http_process_request_headers[14]
4647a6: ngx_http_process_request_line[14]
44c1fe: ngx_epoll_process_events[14]
4431c3: ngx_process_events_and_timers[14]
44a65a: ngx_worker_process_cycle[14]
449074: ngx_spawn_process[14]
44aaac: ngx_start_worker_processes[14]
44b213: ngx_master_process_cycle[14]
422df2: main[14]
23493: __libc_start_main[2]
422e5e: _start[14]
42706f: ngx_chain_update_chains[14]
509ce7: ngx_http_lua_body_filter[14]
42739e: ngx_output_chain[14]
48e1aa: ngx_http_copy_filter[14]
45ea2b: ngx_http_output_filter[14]
496569: ngx_http_static_handler[14]
45ec6e: ngx_http_core_content_phase[14]
459a25: ngx_http_core_run_phases[14]
463ed3: ngx_http_process_request[14]
4643ff: ngx_http_process_request_headers[14]
4647a6: ngx_http_process_request_line[14]
44c1fe: ngx_epoll_process_events[14]
4431c3: ngx_process_events_and_timers[14]
44a65a: ngx_worker_process_cycle[14]
449074: ngx_spawn_process[14]
44aaac: ngx_start_worker_processes[14]
44b213: ngx_master_process_cycle[14]
422df2: main[14]
23493: __libc_start_main[2]
422e5e: _start[14]
47d51f: ngx_http_write_filter[14]
47e973: ngx_http_chunked_body_filter[14]
483024: ngx_http_gzip_body_filter[14]
4864d3: ngx_http_ssi_body_filter[14]
489668: ngx_http_charset_body_filter[14]
48b86c: ngx_http_addition_body_filter[14]
48bf9c: ngx_http_gunzip_body_filter[14]
48d75e: ngx_http_trailers_filter[14]
4fb63c: ngx_http_lua_capture_body_filter[14]
509cb9: ngx_http_lua_body_filter[14]
42739e: ngx_output_chain[14]
48e1aa: ngx_http_copy_filter[14]
45ea2b: ngx_http_output_filter[14]
496569: ngx_http_static_handler[14]
45ec6e: ngx_http_core_content_phase[14]
459a25: ngx_http_core_run_phases[14]
463ed3: ngx_http_process_request[14]
4643ff: ngx_http_process_request_headers[14]
4647a6: ngx_http_process_request_line[14]
44c1fe: ngx_epoll_process_events[14]
4431c3: ngx_process_events_and_timers[14]
44a65a: ngx_worker_process_cycle[14]
449074: ngx_spawn_process[14]
44aaac: ngx_start_worker_processes[14]
44b213: ngx_master_process_cycle[14]
422df2: main[14]
23493: __libc_start_main[2]
422e5e: _start[14]
0 commit comments