Skip to content

fix(packages/lock): honor negative timeout as wait forever in File and Distributed adapters (#13719) - #13787

Closed
PINYOPATTANAWASANPORN wants to merge 1 commit into
appwrite:mainfrom
PINYOPATTANAWASANPORN:fix/packages-lock-negative-timeout-wait-forever-13719
Closed

PINYOPATTANAWASANPORN wants to merge 1 commit into
appwrite:mainfrom
PINYOPATTANAWASANPORN:fix/packages-lock-negative-timeout-wait-forever-13719

Conversation

@PINYOPATTANAWASANPORN

Copy link
Copy Markdown

What does this PR do?

Utopia\Lock\Lock::acquire() documents its contract as: 0.0 means do not wait, and a negative value means wait forever.
However, File::acquire() and Distributed::acquire() previously checked if ($timeout <= 0.0) to return early, treating negative timeouts as non-blocking attempts. Under contention, callers passing a negative timeout received an immediate false (or a Contention exception from withLock()) instead of waiting for the lock to be released.

This PR updates File::acquire() and Distributed::acquire() to:

  1. Check if ($timeout === 0.0) for the non-blocking fast path.
  2. Treat negative timeouts ($timeout < 0.0) as unbounded waits (looping with exponential backoff until acquired).
  3. Set $deadline = $timeout > 0.0 ? microtime(true) + $timeout : null so timed waits expire as expected, while unbounded waits continue polling until release.
  4. Add unit tests covering negative timeout acquisition when unheld and blocking until release under process contention.

Test Plan

  • Added testNegativeTimeoutAcquiresWhenUnheld and testNegativeTimeoutBlocksUntilRelease in packages/lock/tests/FileTest.php.
  • Added testNegativeTimeoutAcquiresWhenUnheld in packages/lock/tests/E2E/DistributedTest.php.

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?

…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.
@greptile-apps

greptile-apps Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

This PR is not yet safe to merge because File locks can hang forever on permanent locking errors, and the new contention coverage violates the repository's deterministic observable-testing requirement.

Fix All in Claude CodeFindings

  1. P1 Permanent failures loop forever ▶
  2. P2 Timing makes test unreliable ▶
  3. P2 Test skips changed wait path ▶
Fix with agent prompt
### Issue 1
packages/lock/src/File.php:32
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.

### Issue 2
packages/lock/tests/FileTest.php:189-190
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.

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!

### Issue 3
packages/lock/tests/E2E/DistributedTest.php:340
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.

Summary

This PR changes the File and Distributed lock adapters so negative acquisition timeouts wait indefinitely rather than behaving as non-blocking attempts.

  • Introduces nullable deadlines for unbounded waits in both adapters.
  • Adds uncontended negative-timeout coverage for both adapters and a fork-based contention test for File locks.
  • The File adapter can now retry permanent flock() failures indefinitely because it does not distinguish them from contention.
  • The File contention test is timing-dependent, while the Distributed test never reaches the changed waiting path.

Reviews (1) · Last reviewed commit: "fix(packages/lock): honor negative timeo..."

}

$deadline = microtime(true) + $timeout;
$deadline = $timeout > 0.0 ? microtime(true) + $timeout : null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

Fix in Claude Code Fix in Codex

Comment on lines +189 to +190
$this->assertGreaterThanOrEqual(0.05, $elapsed, 'Parent should block until child releases');
$this->assertSame('1', file_get_contents($done));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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)

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!

Fix in Claude Code Fix in Codex

public function testNegativeTimeoutAcquiresWhenUnheld(): void
{
$lock = new Distributed($this->redis, $this->key, 30);
$this->assertTrue($lock->acquire(-1.0));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

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.

Fix in Claude Code Fix in Codex

@HarshMN2345

Copy link
Copy Markdown
Member

Closing since this is already fixed on main. Thanks for the contribution!

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