Skip to content

_lock_dir_age() always returns 0 on GNU/Linux — _lock_try_adopt can never fire, so the pid-less-orphan save outage is still live #198

Closed

Description

@bonyohana

_lock_dir_age() always returns 0 on GNU/Linux, so _lock_try_adopt can never fire and the pid-less-orphan outage it exists to prevent is still live on Linux.

scripts/lib-lock.sh:167

_mtime=$(stat -f %m "$1" 2>/dev/null || stat -c %Y "$1" 2>/dev/null) || true
case "$_mtime" in
    ''|*[!0-9]*) echo 0; return 0 ;;
esac

The intent is "try BSD, fall back to GNU". The exit codes behave — GNU stat -f %m exits 1, so the fallback does run. The problem is stdout: stat -f on GNU is filesystem status, and it prints a multi-line block before failing. The command substitution captures the whole A || B pipeline's stdout, so _mtime ends up as that block concatenated with the correct mtime from the fallback:

  File: "/tmp/tmp.lgvCL8C9y6"
    ID: 26b9871bc77c8277 Namelen: 255     Type: ext2/ext3
...
1785136136

That contains non-digits, so the guard on the next line matches *[!0-9]* and returns 0.

Measured on Linux (GNU coreutils):

dir aged to 500s
  stat -f %m  -> exit 1, stdout = filesystem block
  stat -c %Y  -> exit 0, stdout = 1785136136   (correct)
  _lock_dir_age -> 0s        (should be ~500)

Consequence. _LOCK_ADOPT_AFTER defaults to 30, so the check at lib-lock.sh:187

[ "$(_lock_dir_age "$_dir")" -lt "$_LOCK_ADOPT_AFTER" ] && return 1

is [ 0 -lt 30 ] — always true, always refuse. _lock_try_adopt can never succeed on Linux at the default threshold. Reproduced:

L=$(mktemp -d)/x.lock; mkdir -p "$L"
touch -d "@$(( $(date +%s) - 3600 ))" "$L"    # a one-hour-old pid-less orphan
lock_acquire "$L" 1 && echo ACQUIRED || echo BLOCKED
# -> BLOCKED

So the failure the function's own comment describes — "that lock can never be acquired again by anyone — a permanent, silent outage of every save, fixable only by deleting the directory by hand" — is unfixed on Linux, which I'd guess is most installs.

Why the suite doesn't catch it. test_a_pidless_orphan_is_adopted_not_blocked_forever sets _LOCK_ADOPT_AFTER=0 (tests/test_lock_primitive.py:225). That makes the comparison [ 0 -lt 0 ], which is false, so adoption proceeds and the test passes through the broken age rather than exercising it. The test is green on a code path that cannot work at the shipped default.

Fix sketch — don't let the probe's stdout reach the capture:

_lock_dir_age() {
    local _mtime _now
    _mtime=$(stat -c %Y "$1" 2>/dev/null) || _mtime=""
    case "$_mtime" in
        ''|*[!0-9]*) _mtime=$(stat -f %m "$1" 2>/dev/null) || _mtime="" ;;
    esac
    case "$_mtime" in
        ''|*[!0-9]*) echo 0; return 0 ;;
    esac
    _now=$(date +%s)
    echo $(( _now - _mtime ))
}

Separate captures, so a failing probe cannot contaminate a succeeding one. (Order is arbitrary — GNU-first just avoids the noisy call on the more common platform. On macOS stat -c fails cleanly and the BSD branch runs.) A regression test would need to assert _lock_dir_age on a directory of known age rather than only asserting acquisition with the threshold zeroed.

Secondary, same function. _lock_try_adopt creates ${_dir}/adopt as its single-winner marker and rmdirs it after writing the pid. An adopter killed between those two steps strands the marker: no pid, no pid.stealing.*, and adopt/ present. _lock_try_steal returns early (no pid to judge), and every subsequent _lock_try_adopt fails at mkdir — so the lock is permanently unacquirable again, by the same argument the function was written to answer. Reproduced once the age bug above is patched out. Possible treatment: age the marker the same way the lock is aged, or write the adopter's pid into it so a later adopter can judge it dead.

Found while reading 0.8.8; both are in code added by #182/#185. I'm happy to open a PR for the _lock_dir_age fix plus a known-age regression test if that's useful — no obligation either way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions