From b395a198b193ac67560e0ce8cb7325ca71728e5e Mon Sep 17 00:00:00 2001 From: jknapp Date: Tue, 24 Nov 2026 08:17:13 -0800 Subject: [PATCH 1/2] fix(shared-ols): watcher executable bit, coalesce floor-window edits, harden test CI run 6313 Shell-Checks failed "plain site docRoot .htaccess triggers (+0)": the watcher was tracked as mode 100644 (the image chmods it, a bare CI checkout does not), so `env ... ols-htaccess-watcher.sh` died with "Permission denied" and no case ever restarted. Not a watcher logic bug. - watcher: mode 100755 - watcher: an edit inside the FLOOR window is deferred to the end of the window (one coalesced restart, bounded by FLOOR) instead of dropped - test: assert executable bit, launch via bash, kill children before parent (leaked inotifywait/subshell from earlier sections), add floor/coalesce cases Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6 --- scripts/ols-htaccess-watcher.sh | 14 ++++++++-- scripts/tests/ols-htaccess-watcher.test.sh | 32 ++++++++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) mode change 100644 => 100755 scripts/ols-htaccess-watcher.sh diff --git a/scripts/ols-htaccess-watcher.sh b/scripts/ols-htaccess-watcher.sh old mode 100644 new mode 100755 index 9de787c..2ad8818 --- a/scripts/ols-htaccess-watcher.sh +++ b/scripts/ols-htaccess-watcher.sh @@ -94,10 +94,20 @@ log() { echo "ols-htaccess-watcher: $*" >&2; } do_restart() { path="$1" + local now wait_until now=$(date +%s) if [ $((now - last_restart)) -lt "$FLOOR" ]; then - log "within ${FLOOR}s floor — coalescing, skipping restart ($path)" - return + ## 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 + ## the event stream meanwhile so the pipe cannot back up, then restart ONCE + ## 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 + read -r -t 1 _ || true + done + now=$(date +%s) fi if "$LSWSCTRL" restart >/dev/null 2>&1; then last_restart=$now diff --git a/scripts/tests/ols-htaccess-watcher.test.sh b/scripts/tests/ols-htaccess-watcher.test.sh index f570cc1..19c9863 100755 --- a/scripts/tests/ols-htaccess-watcher.test.sh +++ b/scripts/tests/ols-htaccess-watcher.test.sh @@ -10,9 +10,17 @@ W="$HERE/ols-htaccess-watcher.sh" if ! command -v inotifywait >/dev/null 2>&1; then echo "SKIP - inotifywait (inotify-tools) not installed; watcher test not run"; exit 0 fi +## The watcher must be executable as checked out (CI run 6313 failed with +## "Permission denied" because git tracked it 100644; the image chmods it, a bare +## checkout does not). Assert it, and launch via bash so the test never depends on it. +if [ ! -x "$W" ]; then echo "FAIL - $W is not executable (git mode must be 100755)"; exit 1; fi T=$(mktemp -d) WPID="" -cleanup() { [ -n "$WPID" ] && kill "$WPID" 2>/dev/null || true; pkill -P "${WPID:-0}" 2>/dev/null || true; rm -rf "$T"; } +## Stop children (inotifywait + the while-subshell) BEFORE the parent: once the +## parent is dead they are reparented and `pkill -P` no longer finds them, and the +## leaked watcher would keep restarting against later sections. +stopw() { [ -n "$WPID" ] || return 0; pkill -P "$WPID" 2>/dev/null || true; kill "$WPID" 2>/dev/null || true; WPID=""; } +cleanup() { stopw; rm -rf "$T"; } trap cleanup EXIT fail=0 ok() { echo "ok - $1"; } @@ -36,7 +44,7 @@ chmod +x "$T/lswsctrl" start() { env OLS_WATCH_ROOT="$U" OLS_LSWS_CONF="$CONF" OLS_SITES_ROOT="$SITES" LSWSCTRL="$T/lswsctrl" \ - OLS_HTACCESS_DEBOUNCE=1 OLS_HTACCESS_FLOOR=0 "$W" 2>"$T/log" & + OLS_HTACCESS_DEBOUNCE=1 OLS_HTACCESS_FLOOR="${FLOOR_T:-0}" bash "$W" 2>"$T/log" & WPID=$! sleep 1.5 } @@ -63,7 +71,7 @@ mkdir -p "$U/u4/d.example/public_html/pub" mkdir -p "$SITES/d"; printf 'docRoot %s\n' "$U/u4/d.example/public_html/pub" >"$SITES/d/vhconf.conf" echo '# cfg2' >"$CONF/.tmp" && mv "$CONF/.tmp" "$CONF/httpd_config.conf" check "site added after start (re-render) triggers" 1 "$U/u4/d.example/public_html/pub/.htaccess" -kill "$WPID" 2>/dev/null || true; pkill -P "$WPID" 2>/dev/null || true; WPID="" +stopw ## Fallback: no vhconf files -> legacy */public_html/.htaccess match rm -rf "$SITES"; mkdir -p "$SITES" @@ -71,4 +79,22 @@ start check "fallback (empty docRoot set): public_html/.htaccess triggers" 1 "$U/u3/c.example/public_html/.htaccess" check "fallback: deep plugin file still does NOT" 0 "$U/u1/a.example/public_html/wp-content/cache/x/.htaccess" +stopw + +## Floor window: an edit inside it is DEFERRED to the window end (one restart), +## not dropped; a burst of several edits inside it still costs a single restart. +FLOOR_T=8 +start +: >"$T/restarts" +H="$U/u3/c.example/public_html/.htaccess" +echo a >>"$H"; sleep 4 +r1=$(count) +echo b >>"$H"; sleep 0.5; echo c >>"$H"; sleep 1 +r2=$(count) +sleep 9 +r3=$(count) +[ "$r1" -eq 1 ] && ok "floor: first edit restarts immediately" || bad "floor: first edit (restarts $r1, want 1)" +[ "$r2" -eq 1 ] && ok "floor: edits inside window do not restart early" || bad "floor: early restart (restarts $r2, want 1)" +[ "$r3" -eq 2 ] && ok "floor: edits inside window coalesce into ONE deferred restart" || bad "floor: deferred restart (restarts $r3, want 2)" + [ "$fail" -eq 0 ] && echo "ALL PASS" || { echo "FAILURES"; cat "$T/log"; exit 1; } From 432741d6a44d2163d7214e0a879343bcebc41fd4 Mon Sep 17 00:00:00 2001 From: jknapp Date: Tue, 24 Nov 2026 08:29:18 -0800 Subject: [PATCH 2/2] 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