Repository navigation
fix(packages/lock): honor negative timeout as wait forever in File and Distributed adapters (#13719) - #13787
Conversation
…d Distributed adapters (appwrite#13719) Utopia\Lock\Lock::acquire() specifies that 0.0 means do not wait and a negative value means wait forever. Previously File and Distributed adapters checked `$timeout <= 0.0`, treating negative timeouts as non-blocking. This change updates the adapters to treat negative timeouts as unbounded waits with exponential backoff until acquired, matching the interface contract.
|
| } | ||
|
|
||
| $deadline = microtime(true) + $timeout; | ||
| $deadline = $timeout > 0.0 ? microtime(true) + $timeout : null; |
There was a problem hiding this comment.
Permanent failures loop forever
If flock() fails because of a filesystem or locking error rather than contention, tryAcquire() still returns false and discards $wouldBlock. With a negative timeout, the null deadline makes this failure retry forever, so the caller hangs instead of receiving the permanent lock failure.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/lock/src/File.php
Line: 32
Comment:
**Permanent failures loop forever**
If `flock()` fails because of a filesystem or locking error rather than contention, `tryAcquire()` still returns `false` and discards `$wouldBlock`. With a negative timeout, the null deadline makes this failure retry forever, so the caller hangs instead of receiving the permanent lock failure.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| $this->assertGreaterThanOrEqual(0.05, $elapsed, 'Parent should block until child releases'); | ||
| $this->assertSame('1', file_get_contents($done)); |
There was a problem hiding this comment.
This test violates the repository directive to test observable behavior without implementation-coupled sleeps. A delayed parent can start after the child's 100 ms hold and fail the elapsed-time assertion even when locking works correctly. The parent can also acquire the released lock before the child writes $done, making the next assertion race independently of lock behavior. This repository requirement must be satisfied before merging; use explicit inter-process synchronization instead of fixed sleeps and duration thresholds.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/lock/tests/FileTest.php
Line: 189-190
Comment:
**Timing makes test unreliable**
This test violates the repository directive to test observable behavior without implementation-coupled sleeps. A delayed parent can start after the child's 100 ms hold and fail the elapsed-time assertion even when locking works correctly. The parent can also acquire the released lock before the child writes `$done`, making the next assertion race independently of lock behavior. This repository requirement must be satisfied before merging; use explicit inter-process synchronization instead of fixed sleeps and duration thresholds.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| public function testNegativeTimeoutAcquiresWhenUnheld(): void | ||
| { | ||
| $lock = new Distributed($this->redis, $this->key, 30); | ||
| $this->assertTrue($lock->acquire(-1.0)); |
There was a problem hiding this comment.
With an unheld key, acquire(-1.0) succeeds on its initial attempt before the negative-timeout waiting logic runs. This test therefore cannot catch regressions in the changed Distributed retry path. Add a contended Redis test that holds the key, begins a negative-timeout acquisition, releases the holder, and observes the waiter acquire the lock.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/lock/tests/E2E/DistributedTest.php
Line: 340
Comment:
**Test skips changed wait path**
With an unheld key, `acquire(-1.0)` succeeds on its initial attempt before the negative-timeout waiting logic runs. This test therefore cannot catch regressions in the changed Distributed retry path. Add a contended Redis test that holds the key, begins a negative-timeout acquisition, releases the holder, and observes the waiter acquire the lock.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.|
Closing since this is already fixed on main. Thanks for the contribution! |
What does this PR do?
Utopia\Lock\Lock::acquire()documents its contract as:0.0means do not wait, and a negative value means wait forever.However,
File::acquire()andDistributed::acquire()previously checkedif ($timeout <= 0.0)to return early, treating negative timeouts as non-blocking attempts. Under contention, callers passing a negative timeout received an immediatefalse(or aContentionexception fromwithLock()) instead of waiting for the lock to be released.This PR updates
File::acquire()andDistributed::acquire()to:if ($timeout === 0.0)for the non-blocking fast path.$timeout < 0.0) as unbounded waits (looping with exponential backoff until acquired).$deadline = $timeout > 0.0 ? microtime(true) + $timeout : nullso timed waits expire as expected, while unbounded waits continue polling until release.Test Plan
testNegativeTimeoutAcquiresWhenUnheldandtestNegativeTimeoutBlocksUntilReleaseinpackages/lock/tests/FileTest.php.testNegativeTimeoutAcquiresWhenUnheldinpackages/lock/tests/E2E/DistributedTest.php.Related PRs and Issues
Checklist