Skip to content

Commit 7488667

Browse files
committed
bugfix: the main request response was not discarded by srcache_store when there was an error in the last minute (like a read-timeout error happens when ngx_http_upstream reads the upstream response body).
1 parent f842534 commit 7488667

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/ngx_http_srcache_filter_module.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ struct ngx_http_srcache_ctx_s {
109109

110110
ngx_http_srcache_postponed_request_t *postponed_requests;
111111

112+
ngx_uint_t http_status; /* the HTTP status code that has been
113+
actually sent */
114+
112115
unsigned waiting_subrequest:1;
113116
unsigned request_done:1;
114117
unsigned from_cache:1;

src/ngx_http_srcache_store.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ ngx_http_srcache_header_filter(ngx_http_request_t *r)
235235

236236
r->filter_need_in_memory = 1;
237237

238+
ctx->http_status = r->headers_out.status;
238239
ctx->store_response = 1;
239240

240241
return NGX_OK;
@@ -427,6 +428,20 @@ ngx_http_srcache_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
427428
ctx->store_response = 0;
428429
goto done;
429430
}
431+
432+
if (r->headers_out.status >= NGX_HTTP_SPECIAL_RESPONSE
433+
&& r->headers_out.status != ctx->http_status)
434+
{
435+
/* data truncation or body receive timeout */
436+
437+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
438+
"srcache_store: skipped due to new error status "
439+
"code %ui (old: %ui)",
440+
r->headers_out.status, ctx->http_status);
441+
442+
ctx->store_response = 0;
443+
goto done;
444+
}
430445
#endif
431446

432447
rc = ngx_http_srcache_store_subrequest(r, ctx);

t/timeout.t

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,63 @@ Content-Type: text/css
5454
--- response_body
5555
hello
5656
57+
58+
59+
=== TEST 2: main req upstream truncation (with content-length)
60+
--- config
61+
location = /t {
62+
srcache_store PUT /err;
63+
64+
proxy_read_timeout 100ms;
65+
proxy_pass http://127.0.0.1:11945/echo;
66+
}
67+
68+
location = /err {
69+
content_by_lua '
70+
ngx.log(ngx.ERR, "location /err is called")
71+
';
72+
}
73+
74+
--- tcp_listen: 11945
75+
--- tcp_no_close
76+
--- tcp_reply eval
77+
"HTTP/1.0 200 OK\r\nContent-Length: 120\r\n\r\nhello world"
78+
79+
--- request
80+
GET /t
81+
--- response_body
82+
--- no_error_log
83+
location /err is called
84+
--- error_log
85+
srcache_store: skipped because response body truncated: 120 > 0
86+
87+
88+
89+
=== TEST 3: main req upstream truncation (without content-length)
90+
--- config
91+
location = /t {
92+
srcache_store PUT /err;
93+
94+
proxy_read_timeout 100ms;
95+
proxy_pass http://127.0.0.1:11945/echo;
96+
}
97+
98+
location = /err {
99+
content_by_lua '
100+
ngx.log(ngx.ERR, "location /err is called")
101+
';
102+
}
103+
104+
--- tcp_listen: 11945
105+
--- tcp_no_close
106+
--- tcp_reply eval
107+
"HTTP/1.0 200 OK\r\n\r\nhello world"
108+
109+
--- request
110+
GET /t
111+
--- response_body
112+
--- no_error_log
113+
location /err is called
114+
--- error_log
115+
srcache_store: skipped due to new error status code 502 (old: 200)
116+

0 commit comments

Comments
 (0)