From 432741d6a44d2163d7214e0a879343bcebc41fd4 Mon Sep 17 00:00:00 2001 From: jknapp Date: Tue, 24 Nov 2026 08:29:18 -0800 Subject: [PATCH] fix(shared-ols): watcher uses monotonic $SECONDS, stamps restart attempts, no fork per drained event - floor wait was based on wall-clock date: a backward clock step could stretch the deferral far past FLOOR; $SECONDS is monotonic and bounds it to FLOOR. - $(date) forked on every drained event; under unrelated churn that burned CPU for up to FLOOR seconds. Builtin $SECONDS needs no fork. - last_restart is stamped on every attempt so a failing lswsctrl is limited to one try per FLOOR instead of retrying on each event. - IFS= read -r so path names with leading/trailing whitespace are not trimmed. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6 --- scripts/ols-htaccess-watcher.sh | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/scripts/ols-htaccess-watcher.sh b/scripts/ols-htaccess-watcher.sh index 2ad8818..1b7932f 100755 --- a/scripts/ols-htaccess-watcher.sh +++ b/scripts/ols-htaccess-watcher.sh @@ -37,7 +37,6 @@ LSWSCTRL="${LSWSCTRL:-/usr/local/lsws/bin/lswsctrl}" ## render-shared-ols-config.sh; overridable so tests can run without OLS). LSWS_CONF="${OLS_LSWS_CONF:-/usr/local/lsws/conf}" SITES_ROOT="${OLS_SITES_ROOT:-${SITES_ROOT:-$LSWS_CONF/shared-sites}}" -last_restart=0 ## Set of vhost docRoot directories (absolute, no trailing slash), keyed by ## path. Rebuilt from $SITES_ROOT/*/vhconf.conf only when the rendered @@ -92,11 +91,16 @@ is_docroot_htaccess() { log() { echo "ols-htaccess-watcher: $*" >&2; } +## Time comes from bash's $SECONDS (monotonic w.r.t. wall-clock steps, no fork +## per check). A backward clock jump therefore cannot stretch the floor wait, and +## the drain loops below stay cheap even when unrelated churn under $WATCH_ROOT +## makes `read` return instantly on every event. +last_restart="" # $SECONDS of the last restart ATTEMPT; empty = none yet + do_restart() { path="$1" - local now wait_until - now=$(date +%s) - if [ $((now - last_restart)) -lt "$FLOOR" ]; then + local wait_until + if [ -n "$last_restart" ] && [ $((SECONDS - last_restart)) -lt "$FLOOR" ]; then ## COALESCE, don't drop: an edit inside the floor window used to be ## discarded, so it never applied until some later edit happened. Instead ## wait out the remainder of the window (hard-bounded by FLOOR), draining @@ -104,13 +108,14 @@ do_restart() { ## for everything that arrived. A burst of N edits still costs one restart. wait_until=$((last_restart + FLOOR)) log "within ${FLOOR}s floor — deferring restart to end of window ($path)" - while [ "$(date +%s)" -lt "$wait_until" ]; do + while [ "$SECONDS" -lt "$wait_until" ]; do read -r -t 1 _ || true done - now=$(date +%s) fi + ## Stamp the ATTEMPT (not only success) so a failing lswsctrl is also limited + ## to one try per FLOOR instead of retrying on every event. + last_restart=$SECONDS if "$LSWSCTRL" restart >/dev/null 2>&1; then - last_restart=$now log "graceful restart issued — $path changed" else log "WARNING: lswsctrl restart failed ($path)" @@ -145,7 +150,7 @@ log "watching $WATCH_ROOT for vhost-docroot .htaccess changes (debounce=${DEBOUN ## directories is what ties a change to something OLS will reread. (Legacy ## */public_html/.htaccess match is kept only when that set is empty.) inotifywait -m -r -e modify,create,delete,move "$WATCH_ROOT" --format '%w%f' 2>/dev/null | -while read -r path; do +while IFS= read -r path; do is_docroot_htaccess "$path" || continue ## A tenant DOCROOT .htaccess changed. Coalesce the save-burst, then restart ONCE. ## @@ -156,8 +161,8 @@ while read -r path; do ## events only until the deadline OR ~2s of total quiet, whichever comes first, ## so continuous activity can delay us by at most DEBOUNCE. do_restart's FLOOR ## then rate-limits across consecutive bursts. - deadline=$(( $(date +%s) + DEBOUNCE )) - while [ "$(date +%s)" -lt "$deadline" ]; do + deadline=$((SECONDS + DEBOUNCE)) + while [ "$SECONDS" -lt "$deadline" ]; do if read -r -t 2 _; then continue # more activity — keep coalescing toward the deadline else