Skip to content

fix(lock): honor negative acquire timeout as wait-forever - #13756

Merged
HarshMN2345 merged 12 commits into
appwrite:fix-lock-negative-timeoutfrom
flcrom:fix-lock-negative-timeout
Oct 15, 2026
Merged

HarshMN2345 merged 12 commits into
appwrite:fix-lock-negative-timeoutfrom
flcrom:fix-lock-negative-timeout

Conversation

@flcrom

@flcrom flcrom commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Utopia\Lock\Lock::acquire() documents its contract as: 0.0 means do not wait, a negative value means wait forever. File::acquire(), Distributed::acquire(), and the non-coroutine Mutex/Semaphore paths all treated every $timeout <= 0.0 as a single non-blocking attempt, so under contention a caller following the documented contract got an immediate false (or a Contention from withLock()) instead of waiting.

Each path now only short-circuits on exactly 0.0 and retries without a deadline when the timeout is negative. Coroutine paths are untouched (Channel::push() already waits indefinitely on a negative timeout). Zero and positive timeout behavior is unchanged, including the jittered backoff in Distributed.

Test Plan

Added FileTest::testNegativeTimeoutWaitsUntilRelease, which forks a child process that holds the lock and asserts the parent's acquire(-1.0) blocks until the child releases. It fails against the previous implementation and passes with this change.

  • phpunit tests/FileTest.php on PHP 8.3: OK (5 tests, 16 assertions)
  • php -l clean on all package files
  • phpstan at the package's level 9 settings: no new findings

The Swoole-backed Mutex/Semaphore tests and the Redis-backed E2E tier need ext-swoole and a Redis server, so those run in CI.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? (No API metadata changes)

@greptile-apps

greptile-apps Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the current implementation satisfies the documented timeout contract and no actionable new defects remain.

Summary

This PR makes negative lock-acquisition timeouts honor the documented wait-forever contract across file, distributed, mutex, and semaphore implementations.

  • Distinguishes zero-timeout non-blocking acquisition from negative-timeout indefinite waiting.
  • Preserves positive deadlines and distributed-lock backoff behavior.
  • Adds observable contention coverage for file, Redis-backed, mutex, and semaphore locks.
  • Removes implementation-coupled failure-path tests and eliminates the unguarded POSIX cleanup dependency identified previously.

Reviews (3) · Last reviewed commit: "test(lock): bound Redis I/O and make for..."

Comment thread packages/lock/tests/FileTest.php Resolved
Comment thread packages/lock/src/Distributed.php Resolved
Comment thread packages/lock/tests/FileTest.php Resolved
@flcrom

flcrom commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping on this PR. Glad to make changes if anything needs adjusting.

@HarshMN2345
HarshMN2345 changed the base branch from main to fix-lock-negative-timeout October 15, 2026 07:23
@HarshMN2345
HarshMN2345 merged commit 12150a1 into appwrite:fix-lock-negative-timeout Oct 15, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

packages/lock: negative timeout should wait forever, adapters treat it as no-wait

2 participants