view auto/install @ 9203:0de20f43db25

Fixed request termination with AIO and subrequests (ticket #2555). When a request was terminated due to an error via ngx_http_terminate_request() while an AIO operation was running in a subrequest, various issues were observed. This happened because ngx_http_request_finalizer() was only set in the subrequest where ngx_http_terminate_request() was called, but not in the subrequest where the AIO operation was running. After completion of the AIO operation normal processing of the subrequest was resumed, leading to issues. In particular, in case of the upstream module, termination of the request called upstream cleanup, which closed the upstream connection. Attempts to further work with the upstream connection after AIO operation completion resulted in segfaults in ngx_ssl_recv(), "readv() failed (9: Bad file descriptor) while reading upstream" errors, or socket leaks. In ticket #2555, issues were observed with the following configuration with cache background update (with thread writing instrumented to introduce a delay, when a client closes the connection during an update): location = /background-and-aio-write { proxy_pass ... proxy_cache one; proxy_cache_valid 200 1s; proxy_cache_background_update on; proxy_cache_use_stale updating; aio threads; aio_write on; limit_rate 1000; } Similarly, the same issue can be seen with SSI, and can be caused by errors in subrequests, such as in the following configuration (where "/proxy" uses AIO, and "/sleep" returns 444 after some delay, causing request termination): location = /ssi-active-boom { ssi on; ssi_types *; return 200 ' <!--#include virtual="/proxy" --> <!--#include virtual="/sleep" --> '; limit_rate 1000; } Or the same with both AIO operation and the error in non-active subrequests (which needs slightly different handling, see below): location = /ssi-non-active-boom { ssi on; ssi_types *; return 200 ' <!--#include virtual="/static" --> <!--#include virtual="/proxy" --> <!--#include virtual="/sleep" --> '; limit_rate 1000; } Similarly, issues can be observed with just static files. However, with static files potential impact is limited due to timeout safeguards in ngx_http_writer(), and the fact that c->error is set during request termination. In a simple configuration with an AIO operation in the active subrequest, such as in the following configuration, the connection is closed right after completion of the AIO operation anyway, since ngx_http_writer() tries to write to the connection and fails due to c->error set: location = /ssi-active-static-boom { ssi on; ssi_types *; return 200 ' <!--#include virtual="/static-aio" --> <!--#include virtual="/sleep" --> '; limit_rate 1000; } In the following configuration, with an AIO operation in a non-active subrequest, the connection is closed only after send_timeout expires: location = /ssi-non-active-static-boom { ssi on; ssi_types *; return 200 ' <!--#include virtual="/static" --> <!--#include virtual="/static-aio" --> <!--#include virtual="/sleep" --> '; limit_rate 1000; } Fix is to introduce r->main->terminated flag, which is to be checked by AIO event handlers when the r->main->blocked counter is decremented. When the flag is set, handlers are expected to wake up the connection instead of the subrequest (which might be already cleaned up). Additionally, now ngx_http_request_finalizer() is always set in the active subrequest, so waking up the connection properly finalizes the request even if termination happened in a non-active subrequest.
author Maxim Dounin <mdounin@mdounin.ru>
date Tue, 30 Jan 2024 03:20:05 +0300
parents 549b13cd793b
children
line wrap: on
line source


# Copyright (C) Igor Sysoev
# Copyright (C) Nginx, Inc.


if [ $USE_PERL != NO ]; then

    cat << END                                                >> $NGX_MAKEFILE

install_perl_modules:
	cd $NGX_OBJS/src/http/modules/perl && \$(MAKE) install
END

    NGX_INSTALL_PERL_MODULES=install_perl_modules

fi


case ".$NGX_SBIN_PATH" in
    ./*)
    ;;

    *)
        NGX_SBIN_PATH=$NGX_PREFIX/$NGX_SBIN_PATH
    ;;
esac


case ".$NGX_MODULES_PATH" in
    ./*)
    ;;

    *)
        NGX_MODULES_PATH=$NGX_PREFIX/$NGX_MODULES_PATH
    ;;
esac

NGX_MODULES_PATH=`dirname $NGX_MODULES_PATH/.`


case ".$NGX_CONF_PATH" in
    ./*)
    ;;

    *)
        NGX_CONF_PATH=$NGX_PREFIX/$NGX_CONF_PATH
    ;;
esac


NGX_CONF_PREFIX=`dirname $NGX_CONF_PATH`


case ".$NGX_PID_PATH" in
    ./*)
    ;;

    *)
        NGX_PID_PATH=$NGX_PREFIX/$NGX_PID_PATH
    ;;
esac


case ".$NGX_ERROR_LOG_PATH" in
    ./* | .)
    ;;

    *)
        NGX_ERROR_LOG_PATH=$NGX_PREFIX/$NGX_ERROR_LOG_PATH
    ;;
esac


case ".$NGX_HTTP_LOG_PATH" in
    ./*)
    ;;

    *)
        NGX_HTTP_LOG_PATH=$NGX_PREFIX/$NGX_HTTP_LOG_PATH
    ;;
esac


if test -f man/nginx.8 ; then
    NGX_MAN=man/nginx.8
else
    NGX_MAN=docs/man/nginx.8
fi

if test -d html ; then
    NGX_HTML=html
else
    NGX_HTML=docs/html
fi

cat << END                                                    >> $NGX_MAKEFILE

manpage:	$NGX_OBJS/nginx.8

$NGX_OBJS/nginx.8:	$NGX_MAN $NGX_AUTO_CONFIG_H
	sed -e "s|%%PREFIX%%|$NGX_PREFIX|" \\
		-e "s|%%PID_PATH%%|$NGX_PID_PATH|" \\
		-e "s|%%CONF_PATH%%|$NGX_CONF_PATH|" \\
		-e "s|%%ERROR_LOG_PATH%%|${NGX_ERROR_LOG_PATH:-stderr}|" \\
		< $NGX_MAN > \$@

install:	build $NGX_INSTALL_PERL_MODULES
	test -d '\$(DESTDIR)$NGX_PREFIX' || mkdir -p '\$(DESTDIR)$NGX_PREFIX'

	test -d '\$(DESTDIR)`dirname "$NGX_SBIN_PATH"`' \\
		|| mkdir -p '\$(DESTDIR)`dirname "$NGX_SBIN_PATH"`'
	test ! -f '\$(DESTDIR)$NGX_SBIN_PATH' \\
		|| mv '\$(DESTDIR)$NGX_SBIN_PATH' \\
			'\$(DESTDIR)$NGX_SBIN_PATH.old'
	cp $NGX_OBJS/nginx '\$(DESTDIR)$NGX_SBIN_PATH'

	test -d '\$(DESTDIR)$NGX_CONF_PREFIX' \\
		|| mkdir -p '\$(DESTDIR)$NGX_CONF_PREFIX'

	cp conf/koi-win '\$(DESTDIR)$NGX_CONF_PREFIX'
	cp conf/koi-utf '\$(DESTDIR)$NGX_CONF_PREFIX'
	cp conf/win-utf '\$(DESTDIR)$NGX_CONF_PREFIX'

	test -f '\$(DESTDIR)$NGX_CONF_PREFIX/mime.types' \\
		|| cp conf/mime.types '\$(DESTDIR)$NGX_CONF_PREFIX'
	cp conf/mime.types '\$(DESTDIR)$NGX_CONF_PREFIX/mime.types.default'

	test -f '\$(DESTDIR)$NGX_CONF_PREFIX/fastcgi_params' \\
		|| cp conf/fastcgi_params '\$(DESTDIR)$NGX_CONF_PREFIX'
	cp conf/fastcgi_params \\
		'\$(DESTDIR)$NGX_CONF_PREFIX/fastcgi_params.default'

	test -f '\$(DESTDIR)$NGX_CONF_PREFIX/fastcgi.conf' \\
		|| cp conf/fastcgi.conf '\$(DESTDIR)$NGX_CONF_PREFIX'
	cp conf/fastcgi.conf '\$(DESTDIR)$NGX_CONF_PREFIX/fastcgi.conf.default'

	test -f '\$(DESTDIR)$NGX_CONF_PREFIX/uwsgi_params' \\
		|| cp conf/uwsgi_params '\$(DESTDIR)$NGX_CONF_PREFIX'
	cp conf/uwsgi_params \\
		'\$(DESTDIR)$NGX_CONF_PREFIX/uwsgi_params.default'

	test -f '\$(DESTDIR)$NGX_CONF_PREFIX/scgi_params' \\
		|| cp conf/scgi_params '\$(DESTDIR)$NGX_CONF_PREFIX'
	cp conf/scgi_params \\
		'\$(DESTDIR)$NGX_CONF_PREFIX/scgi_params.default'

	test -f '\$(DESTDIR)$NGX_CONF_PATH' \\
		|| cp conf/nginx.conf '\$(DESTDIR)$NGX_CONF_PATH'
	cp conf/nginx.conf '\$(DESTDIR)$NGX_CONF_PREFIX/nginx.conf.default'

	test -d '\$(DESTDIR)`dirname "$NGX_PID_PATH"`' \\
		|| mkdir -p '\$(DESTDIR)`dirname "$NGX_PID_PATH"`'

	test -d '\$(DESTDIR)`dirname "$NGX_HTTP_LOG_PATH"`' \\
		|| mkdir -p '\$(DESTDIR)`dirname "$NGX_HTTP_LOG_PATH"`'

	test -d '\$(DESTDIR)$NGX_PREFIX/html' \\
		|| cp -R $NGX_HTML '\$(DESTDIR)$NGX_PREFIX'
END


if test -n "$NGX_ERROR_LOG_PATH"; then
    cat << END                                                >> $NGX_MAKEFILE

	test -d '\$(DESTDIR)`dirname "$NGX_ERROR_LOG_PATH"`' \\
		|| mkdir -p '\$(DESTDIR)`dirname "$NGX_ERROR_LOG_PATH"`'
END

fi


if test -n "$DYNAMIC_MODULES"; then
    cat << END                                                >> $NGX_MAKEFILE

	test -d '\$(DESTDIR)$NGX_MODULES_PATH' \\
		|| mkdir -p '\$(DESTDIR)$NGX_MODULES_PATH'
END

fi


for ngx_module in $DYNAMIC_MODULES
do
    ngx_module=$ngx_module$ngx_modext

    cat << END                                                >> $NGX_MAKEFILE

	test ! -f '\$(DESTDIR)$NGX_MODULES_PATH/$ngx_module' \\
		|| mv '\$(DESTDIR)$NGX_MODULES_PATH/$ngx_module' \\
			'\$(DESTDIR)$NGX_MODULES_PATH/$ngx_module.old'
	cp $NGX_OBJS/$ngx_module '\$(DESTDIR)$NGX_MODULES_PATH/$ngx_module'
END

done


# create Makefile

cat << END >> Makefile

build:
	\$(MAKE) -f $NGX_MAKEFILE

install:
	\$(MAKE) -f $NGX_MAKEFILE install

modules:
	\$(MAKE) -f $NGX_MAKEFILE modules

upgrade:
	$NGX_SBIN_PATH -t

	kill -USR2 \`cat $NGX_PID_PATH\`
	sleep 1
	test -f $NGX_PID_PATH.oldbin

	kill -QUIT \`cat $NGX_PID_PATH.oldbin\`

.PHONY:	build install modules upgrade
END