Skip to content

Commit 4780944

Browse files
committed
fix: sigterm handling so NGINX gracefully exist
After compiling nginx with the modifications: ```shell make build-heroku-18 ``` I tested that the changes actually worked: ```shell apt-get update apt-get install -y python3-venv python3 -m venv venv ./venv/bin/pip install gunicorn FORCE=1 bin/start-nginx ./venv/bin/gunicorn -b unix:/tmp/nginx.socket app:app # in another terminal (should hang for 15s assuming app is setup correctly) curl localhost:5000 # in another terminal # get the PID ps aux | grep master # should be graceful, that is nginx should shutdown after it finishes serving the request kill -TERM $PID FORCE=1 bin/start-nginx # should kill nginx without waiting # curl returns an error: # curl: (52) Empty reply from server kill -QUIT $PID ``` The `app` used by `gunicorn` was the hello world with a sleep thrown in so we mimic a long running request. ```python import time def app(environ, start_response): time.sleep(15) data = b"Hello, World!\n" start_response("200 OK", [ ("Content-Type", "text/plain"), ("Content-Length", str(len(data))) ]) return iter([data]) ``` Based on heroku#56
1 parent 53b03b0 commit 4780944

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

nginx-heroku-18.tgz

181 Bytes
Binary file not shown.

scripts/build_nginx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ echo "Downloading $zlib_url"
3636
echo "Downloading $uuid4_url"
3737
(cd nginx-${NGINX_VERSION} && curl -L $uuid4_url | tar xvz )
3838

39+
if [ -d "/buildpack/support/patchfiles/${NGINX_VERSION}" ]
40+
then
41+
PATCHFILES=$(find /buildpack/support/patchfiles/${NGINX_VERSION} -name '*.patch')
42+
else
43+
exit 1
44+
fi
45+
46+
47+
NGX_SHUTDOWN_SIGNAL=TERM
48+
NGX_TERMINATE_SIGNAL=QUIT
49+
50+
(
51+
cd nginx-${NGINX_VERSION}
52+
for f in $PATCHFILES
53+
do
54+
echo "patch: $f"
55+
patch -p0 < $f
56+
done
57+
)
58+
3959
# This will build `nginx`
4060
(
4161
cd nginx-${NGINX_VERSION}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--- src/core/ngx_config.h 2020-05-23 04:21:07.000000000 +0000
2+
+++ src/core/ngx_config.h 2020-05-23 04:15:17.000000000 +0000
3+
@@ -56,9 +56,8 @@
4+
5+
#define ngx_random random
6+
7+
-/* TODO: #ifndef */
8+
-#define NGX_SHUTDOWN_SIGNAL QUIT
9+
-#define NGX_TERMINATE_SIGNAL TERM
10+
+#define NGX_SHUTDOWN_SIGNAL TERM
11+
+#define NGX_TERMINATE_SIGNAL QUIT
12+
#define NGX_NOACCEPT_SIGNAL WINCH
13+
#define NGX_RECONFIGURE_SIGNAL HUP

0 commit comments

Comments
 (0)