Skip to content

Commit 710d5ed

Browse files
authored
Merge branch 'amazonlinux:main' into fix/dropin-dir-follows-active-network-file
2 parents 6bd02b3 + dc2b238 commit 710d5ed

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

bin/setup-policy-routes.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,18 @@ refresh)
5050
start)
5151
register_networkd_reloader
5252
counter=0
53+
max_wait=3000 # 5 minute timeout to avoid infinite loop if sysfs node never appears
5354
while [ ! -e "/sys/class/net/${iface}" ]; do
5455
if ((counter % 1000 == 0)); then
5556
debug "Waiting for sysfs node to exist for ${iface} (iteration $counter)"
5657
fi
5758
sleep 0.1
58-
((counter++))
59+
((counter++)) || true
60+
if ((counter >= max_wait)); then
61+
error "Timed out waiting for sysfs node for ${iface} after $((counter / 10)) seconds"
62+
/usr/bin/systemctl disable --now refresh-policy-routes@${iface}.timer 2>/dev/null || true
63+
exit 2
64+
fi
5965
done
6066
debug "Starting configuration for $iface"
6167
debug /lib/systemd/systemd-networkd-wait-online -i "$iface"

lib/lib.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ get_meta() {
140140
local key=$1
141141
local max_tries=${2:-10}
142142
declare -i attempts=0
143-
[ -v EC2_IF_INITIAL_SETUP ] && debug "[get_meta] Querying IMDS for ${key}"
143+
if [ -v EC2_IF_INITIAL_SETUP ]; then
144+
debug "[get_meta] Querying IMDS for ${key}"
145+
fi
144146

145147
if [[ -z $imds_endpoint || -z $imds_token || -z $imds_interface ]]; then
146148
error "[get_meta] Unable to obtain IMDS token, endpoint, or interface"
@@ -632,7 +634,9 @@ maybe_reload_networkd() {
632634
networkctl reload
633635
debug "Reloaded networkd"
634636
else
635-
[ -v EC2_IF_INITIAL_SETUP ] && debug "No networkd reload needed"
637+
if [ -v EC2_IF_INITIAL_SETUP ]; then
638+
debug "No networkd reload needed"
639+
fi
636640
fi
637641
else
638642
debug "Deferring networkd reload to another process"
@@ -642,18 +646,30 @@ maybe_reload_networkd() {
642646

643647
register_networkd_reloader() {
644648
local -i registered=1 cnt=0
645-
local -i max=10000
649+
local -i max=3000 # 300s (3000 × 0.1s); matches sysfs wait timeout in setup-policy-routes.sh
646650
local -r lockfile="${lockdir}/${iface}"
647651
local old_opts=$-
648652

653+
# If the existing lock owner is no longer alive, remove the stale lockfile
654+
# so subsequent invocations don't spin for up to 1000 seconds waiting on a
655+
# process that will never release it.
656+
if [ -f "${lockfile}" ]; then
657+
local existing_pid
658+
existing_pid=$(cat "${lockfile}" 2>/dev/null)
659+
if [ -n "$existing_pid" ] && ! kill -0 "$existing_pid" 2>/dev/null; then
660+
debug "Removing stale lock from dead process $existing_pid for ${iface}"
661+
rm -f "${lockfile}"
662+
fi
663+
fi
664+
649665
# Disable -o errexit in the following block so we can capture
650666
# nonzero exit codes from a redirect without considering them
651667
# fatal errors
652668
set +e
653669
while [ $cnt -lt $max ]; do
654670
cnt+=1
655671
mkdir -p "$lockdir"
656-
trap '[ -v EC2_IF_INITIAL_SETUP ] && debug "Called trap" ; maybe_reload_networkd' EXIT
672+
trap 'if [ -v EC2_IF_INITIAL_SETUP ]; then debug "Called trap"; fi; maybe_reload_networkd' EXIT
657673
# If the redirect fails, most likely because the target file
658674
# already exists and -o noclobber is in effect, $? will be set
659675
# nonzero. If it succeeds, it is set to 0

systemd/system/policy-routes@.service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ User=root
1717
ExecStart=/usr/bin/setup-policy-routes %i start
1818
Restart=on-failure
1919
RestartSec=1
20+
RestartPreventExitStatus=2
2021
KillMode=process

0 commit comments

Comments
 (0)