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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-24 08:29:18 -08:00
1 parent b395a198b1
commit 432741d6a4
1 file changed
+15 -10
+15 -10
View File
@@ -37,7 +37,6 @@ LSWSCTRL="${LSWSCTRL:-/usr/local/lsws/bin/lswsctrl}"
## render-shared-ols-config.sh; overridable so tests can run without OLS). ## render-shared-ols-config.sh; overridable so tests can run without OLS).
LSWS_CONF="${OLS_LSWS_CONF:-/usr/local/lsws/conf}" LSWS_CONF="${OLS_LSWS_CONF:-/usr/local/lsws/conf}"
SITES_ROOT="${OLS_SITES_ROOT:-${SITES_ROOT:-$LSWS_CONF/shared-sites}}" 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 ## Set of vhost docRoot directories (absolute, no trailing slash), keyed by
## path. Rebuilt from $SITES_ROOT/*/vhconf.conf only when the rendered ## path. Rebuilt from $SITES_ROOT/*/vhconf.conf only when the rendered
@@ -92,11 +91,16 @@ is_docroot_htaccess() {
log() { echo "ols-htaccess-watcher: $*" >&2; } 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() { do_restart() {
path="$1" path="$1"
local now wait_until local wait_until
now=$(date +%s) if [ -n "$last_restart" ] && [ $((SECONDS - last_restart)) -lt "$FLOOR" ]; then
if [ $((now - last_restart)) -lt "$FLOOR" ]; then
## COALESCE, don't drop: an edit inside the floor window used to be ## COALESCE, don't drop: an edit inside the floor window used to be
## discarded, so it never applied until some later edit happened. Instead ## discarded, so it never applied until some later edit happened. Instead
## wait out the remainder of the window (hard-bounded by FLOOR), draining ## 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. ## for everything that arrived. A burst of N edits still costs one restart.
wait_until=$((last_restart + FLOOR)) wait_until=$((last_restart + FLOOR))
log "within ${FLOOR}s floor — deferring restart to end of window ($path)" 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 read -r -t 1 _ || true
done done
now=$(date +%s)
fi 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 if "$LSWSCTRL" restart >/dev/null 2>&1; then
last_restart=$now
log "graceful restart issued — $path changed" log "graceful restart issued — $path changed"
else else
log "WARNING: lswsctrl restart failed ($path)" 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 ## directories is what ties a change to something OLS will reread. (Legacy
## */public_html/.htaccess match is kept only when that set is empty.) ## */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 | 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 is_docroot_htaccess "$path" || continue
## A tenant DOCROOT .htaccess changed. Coalesce the save-burst, then restart ONCE. ## 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, ## 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 ## so continuous activity can delay us by at most DEBOUNCE. do_restart's FLOOR
## then rate-limits across consecutive bursts. ## then rate-limits across consecutive bursts.
deadline=$(( $(date +%s) + DEBOUNCE )) deadline=$((SECONDS + DEBOUNCE))
while [ "$(date +%s)" -lt "$deadline" ]; do while [ "$SECONDS" -lt "$deadline" ]; do
if read -r -t 2 _; then if read -r -t 2 _; then
continue # more activity — keep coalescing toward the deadline continue # more activity — keep coalescing toward the deadline
else else