﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc	uname	nginx_version
1565	Prematurely deleting request body temp files on fast response	nickfajones@…		"In a situation similar to: #1519:
if a request body is spooled to disk in a temp file,
and it is sufficiently large,
and the upstream response (headers) return before the request body has finished writing to upstream,

then the response handler will clean up the request body temp file, confounding the request that is still in progress.

I have a very rough and simple solution in the form of:

{{{
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index 3b69bf7..e80ceaf 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -2083,6 +2083,14 @@ ngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u,
 
     /* rc == NGX_OK */
 
+    if (r->request_body && r->request_body->temp_file
+        && r->request_body->temp_file->file.fd != NGX_INVALID_FILE
+        && r == r->main && !r->preserve_body)
+    {
+        ngx_pool_run_cleanup_file(r->pool, r->request_body->temp_file->file.fd);
+        r->request_body->temp_file->file.fd = NGX_INVALID_FILE;
+    }
+
     if (c->write->timer_set) {
         ngx_del_timer(c->write);
     }
@@ -2993,7 +3001,8 @@ ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
     }
 
     if (r->request_body && r->request_body->temp_file
-        && r == r->main && !r->preserve_body)
+        && r->request_body->temp_file->file.fd != NGX_INVALID_FILE
+        && r == r->main && !r->preserve_body && u->request_body_sent)
     {
         ngx_pool_run_cleanup_file(r->pool, r->request_body->temp_file->file.fd);
         r->request_body->temp_file->file.fd = NGX_INVALID_FILE;
}}}

However, I am not convinced this is the best solution as subrequests share the request body temp file structure with the main request, and therefore need to participate in the cleanup decision.

I hope that someone who knows the codebase better can help to form a more complete solution to the problem.

Thanks."	defect	closed	minor		nginx-core	1.14.x	fixed	http upsteram request temp_file		Linux host 4.13.0-38-generic #43~16.04.1-Ubuntu SMP Wed Mar 14 17:48:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux	"nginx version: nginx/1.14.0
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 
built with OpenSSL 1.0.2g 1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g3 -O0' --prefix=/etc/nginx --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_upstream_ip_hash_module --without-http_memcached_module --without-http_auth_basic_module --without-http_userid_module --with-http_gunzip_module --with-poll_module --with-http_v2_module --with-debug"
