﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc	uname	nginx_version
2185	ngx_write_chain_to_file cannot be closed file when write error	yugo-horie@…		"`ngx_write_chain_to_file` cannot be closed file when ngx_write_file returns NGX_ERROR.
It seems to be a same issue below. I'm not sure why here is to be missing file closing.
https://github.com/nginx/nginx/pull/52/
http://hg.nginx.org/nginx/rev/4f30f75dbdf3

patch

{{{
diff --git a/src/os/unix/ngx_files.c b/src/os/unix/ngx_files.c
index 1c82a8ea..7d3d5be8 100644
--- a/src/os/unix/ngx_files.c
+++ b/src/os/unix/ngx_files.c
@@ -300,9 +300,13 @@ ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *cl, off_t offset,
     /* use pwrite() if there is the only buf in a chain */

     if (cl->next == NULL) {
-        return ngx_write_file(file, cl->buf->pos,
+        n = ngx_write_file(file, cl->buf->pos,
                               (size_t) (cl->buf->last - cl->buf->pos),
                               offset);
+        if (n == NGX_ERROR) {
+            (void) ngx_close_file(file->fd);
+        }
+        return n;
     }

     total = 0;
@@ -321,6 +325,7 @@ ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *cl, off_t offset,
                                iovs[0].iov_len, offset);

             if (n == NGX_ERROR) {
+                (void) ngx_close_file(file->fd);
                 return n;
             }

@@ -330,6 +335,7 @@ ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *cl, o
ff_t offset,
         n = ngx_writev_file(file, &vec, offset);

         if (n == NGX_ERROR) {
+            (void) ngx_close_file(file->fd);
             return n;
         }
}}}"	defect	closed	minor		documentation	1.19.x	invalid			Linux 11143d1c63c9 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux	"nginx version: nginx/1.19.9
built by gcc 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
built with OpenSSL 1.1.1k  25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --with-pcre-jit --with-http_ssl_module --with-file-aio --with-threads --with-openssl=/usr/local/src/openssl-1.1.1k"
