@@ -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
643647register_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
0 commit comments