public inbox for git-commits@fedoraproject.org
help / color / mirror / Atom feed
* [rpms/nginx] f43: nginx-upgrade: wait for the new master before stopping the old one
@ 2026-11-26  4:57 Felix Kaechele
  0 siblings, 0 replies; only message in thread
From: Felix Kaechele @ 2026-11-26  4:57 UTC (permalink / raw)
  To: git-commits

            A new commit has been pushed.

            Repo   : rpms/nginx
            Branch : f43
            Commit : 0967cd49a997555c6c763fd34218727c7274a126
            Author : Felix Kaechele <felix@kaechele.ca>
            Date   : 2026-11-25T21:50:48-05:00
            Stats  : +112/-12 in 2 file(s)
            URL    : https://src.fedoraproject.org/rpms/nginx/c/0967cd49a997555c6c763fd34218727c7274a126?branch=f43

            Log:
            nginx-upgrade: wait for the new master before stopping the old one

- Only send QUIT to the old master once the PID file holds a live new
  master; leave the old master running if the new one fails or does not
  start within START_TIMEOUT (90 s)
- Check afterwards that systemd tracks the new master as the main PID
- Take the old master's PID from systemd and only trust PIDs from PID
  files that belong to the unit's cgroup
- Log errors to the journal and stop discarding stderr in the file
  trigger, so failed upgrades show up in the dnf output
- Poll for PID files every 0.1 s instead of every second
- Match all instance names systemd allows (e.g. nginx@foo-bar,
  nginx@example.com, escaped names)

Thanks to felix.bouynot for the analysis and the initial patch.

Resolves: rhbz#2582004
Assisted-by: Claude Opus 5.5
Signed-off-by: Felix Kaechele <felix@kaechele.ca>

---
diff --git a/nginx-upgrade b/nginx-upgrade
index 8bf987a..fa698bc 100755
--- a/nginx-upgrade
+++ b/nginx-upgrade
@@ -6,22 +6,103 @@
 # master process of each instance, then gracefully stopping the old master.
 # This allows for zero-downtime upgrades of nginx.
 
-# Last updated: 2026-03-08
+# Last updated: 2026-11-24
 
+# Seconds to wait for the old master to rename its PID file, for the old
+# master to exit and for systemd to follow the new master.
 TIMEOUT=10
+# Seconds to wait for the new master to parse its configuration and write its
+# PID file. Failures are detected early, so this only bounds a slow start
+# (e.g. resolving upstream hostnames). Matches systemd's default
+# TimeoutStartSec.
+START_TIMEOUT=90
+
+err() {
+	# Errors go to stderr and to the journal: the RPM file trigger discards
+	# stdout, and a failed upgrade leaves the old binary running.
+	echo "ERROR: $*" >&2
+	echo "$*" | systemd-cat -t nginx-upgrade -p err 2>/dev/null
+}
+
+valid_pid() {
+	# usage: valid_pid <pid> <cgroup>
+	# PID file contents are not trusted: only accept a PID that belongs to
+	# the unit's cgroup, as systemd does for PID files not owned by root.
+	local pid=$1 cgroup=$2
+	[[ $pid =~ ^[0-9]+$ ]] && (( pid > 1 )) &&
+		grep -qxF "0::$cgroup" "/proc/$pid/cgroup" 2>/dev/null
+}
 
 wait_for_file() {
 	# usage: wait_for_file <present|absent> <file> <label>
 	local mode=$1 file=$2 label=$3
 	local i
 
-	for ((i=0; i<TIMEOUT; i++)); do
+	# Poll often: if the new master fails quickly, the old master renames
+	# .oldbin back to the PID file and a slow poll would never see it.
+	for ((i=0; i<TIMEOUT*10; i++)); do
 		[[ $mode == present && -f $file ]] && return 0
 		[[ $mode == absent  && ! -f $file ]] && return 0
-		sleep 1
+		sleep 0.1
+	done
+
+	err "$label - file never ${mode}ed: $file"
+	return 1
+}
+
+wait_for_new_master() {
+	# usage: wait_for_new_master <pid-file> <old-pid-file> <old-pid> <cgroup> <label>
+	# Prints the new master's PID on success.
+	#
+	# The old master renames its PID file to .oldbin before it executes the
+	# new binary, but the new master only writes its PID file after it has
+	# parsed its configuration. The old master must not be stopped before
+	# that: systemd (Type=forking) would find no PID to follow, consider the
+	# unit stopped and kill the new master (rhbz#2582004).
+	local file=$1 old_file=$2 old_pid=$3 cgroup=$4 label=$5
+	local i pid
+
+	for ((i=0; i<START_TIMEOUT*10; i++)); do
+		pid=
+		{ read -r pid < "$file"; } 2>/dev/null
+		if [[ $pid != "$old_pid" ]] && valid_pid "$pid" "$cgroup"; then
+			echo "$pid"
+			return 0
+		fi
+		# If the new master exits (e.g. invalid configuration), the old
+		# master renames .oldbin back to the PID file. It is also removed
+		# if the old master itself exits.
+		if [[ ! -f $old_file ]]; then
+			if kill -0 "$old_pid" 2>/dev/null; then
+				err "$label - upgrade aborted: the new master exited, the old master ($old_pid) keeps running the old binary. Check the configuration and restart the service."
+			else
+				err "$label - upgrade aborted: the old master ($old_pid) exited."
+			fi
+			return 1
+		fi
+		sleep 0.1
+	done
+
+	err "$label - upgrade aborted: no new master PID in $file after ${START_TIMEOUT}s, the old master ($old_pid) keeps running the old binary. Restart the service."
+	return 1
+}
+
+wait_for_main_pid() {
+	# usage: wait_for_main_pid <service-unit> <pid> <label>
+	# Check that systemd follows the new master once the old one is gone.
+	local unit=$1 pid=$2 label=$3
+	local i
+
+	for ((i=0; i<TIMEOUT*10; i++)); do
+		if ! /bin/systemctl is-active -q "$unit"; then
+			err "$label - $unit is no longer active after the upgrade."
+			return 1
+		fi
+		[[ $(/bin/systemctl show -p MainPID --value "$unit") == "$pid" ]] && return 0
+		sleep 0.1
 	done
 
-	echo "ERROR: $label - file never ${mode}ed: $file" >&2
+	err "$label - systemd does not track the new master ($pid) as the main process of $unit."
 	return 1
 }
 
@@ -29,23 +110,38 @@ upgrade_instance() {
 	# usage: upgrade_instance <service-unit> <pid-file>
 	local unit="$1" pid_file="$2"
 	local old_pid_file="${pid_file}.oldbin"
+	local old_pid new_pid cgroup
 
 	if [ ! -f "$pid_file" ]; then
 		echo "[$unit] No PID file at $pid_file - skipping."
 		return 0
 	fi
 
+	# Take the old master's PID from systemd rather than from a PID file.
+	old_pid=$(/bin/systemctl show -p MainPID --value "$unit")
+	cgroup=$(/bin/systemctl show -p ControlGroup --value "$unit")
+	if [[ -z $cgroup ]] || ! valid_pid "$old_pid" "$cgroup"; then
+		err "[$unit] Could not determine the main PID - skipping."
+		return 1
+	fi
+
 	echo "[$unit] Starting new master process..."
 	/bin/systemctl kill --signal=SIGUSR2 --kill-who=main "$unit"
 
-	wait_for_file present "$old_pid_file" "$unit - new master start" || return 1
+	wait_for_file present "$old_pid_file" "[$unit] new master start" || return 1
+
+	new_pid=$(wait_for_new_master "$pid_file" "$old_pid_file" "$old_pid" "$cgroup" "[$unit] new master start") || return 1
 
-	echo "[$unit] New master is up. Gracefully stopping old master..."
-	local old_pid
-	old_pid=$(cat "$old_pid_file" 2>/dev/null)
+	echo "[$unit] New master ($new_pid) is up. Gracefully stopping old master ($old_pid)..."
+	if ! valid_pid "$old_pid" "$cgroup"; then
+		err "[$unit] Old master ($old_pid) is gone before it could be stopped."
+		return 1
+	fi
 	/bin/kill -s QUIT "$old_pid" 2>/dev/null
 
-	wait_for_file absent "$old_pid_file" "$unit - old master stop" || return 1
+	wait_for_file absent "$old_pid_file" "[$unit] old master stop" || return 1
+
+	wait_for_main_pid "$unit" "$new_pid" "[$unit] new master takeover" || return 1
 
 	echo "[$unit] Done."
 	return 0
@@ -54,9 +150,11 @@ upgrade_instance() {
 overall_rc=0
 
 # Discover every active nginx instance via systemctl
-instances=$(systemctl list-units --type=service --state=running --no-legend "nginx*" |
+# Instance names may contain the characters systemd allows in unit names,
+# including "\" from escaping (e.g. nginx@foo\x2dbar.service).
+instances=$(systemctl list-units --type=service --state=running --no-legend --plain "nginx*" |
 	awk '{print $1}' |
-	grep -E '^nginx(@\w+)?\.service$')
+	grep -E '^nginx(@[-[:alnum:]:_.\\]+)?\.service$')
 
 for unit in $instances; do
 	pid_file=$(systemctl show "$unit" -p PIDFile --value)
@@ -73,6 +171,8 @@ done
 
 exit $overall_rc
 
+# Man page source for pod2man (see nginx.spec), never executed.
+# shellcheck disable=SC2317
 : <<'POD'
 =pod
 

diff --git a/nginx.spec b/nginx.spec
index 482942b..610743d 100644
--- a/nginx.spec
+++ b/nginx.spec
@@ -513,7 +513,7 @@ install -m 644 -p %{SOURCE20} %{buildroot}%{_tmpfilesdir}/nginx.conf
 # A reload is not sufficient here: https://blog.nginx.org/blog/nginx-dynamic-modules-how-they-work#upgradeMod
 %transfiletriggerin -- %{_sbindir}/nginx %{nginx_moduleconfdir} %{nginx_moduledir}
 if [ "${1:-0}" -ge 1 ]; then
-    test -f %{_sysconfdir}/nginx/rpm-disable-upgrade || /usr/bin/nginx-upgrade >/dev/null 2>&1 || :
+    test -f %{_sysconfdir}/nginx/rpm-disable-upgrade || /usr/bin/nginx-upgrade >/dev/null || :
 fi
 
 

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-26  4:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  4:57 [rpms/nginx] f43: nginx-upgrade: wait for the new master before stopping the old one Felix Kaechele

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox