Skip to content

fix(lock): honour a negative acquire timeout with an unbounded deadline - #14521

Merged
HarshMN2345 merged 17 commits into
mainfrom
fix-lock-negative-timeout
Oct 29, 2026
Merged

HarshMN2345 merged 17 commits into
mainfrom
fix-lock-negative-timeout

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Lock::acquire() documents a negative timeout as wait forever, but File and Distributed treated it as one non-blocking try, so withLock(-1) could throw Contention.

  • File and Distributed short-circuit only on 0.0 and retry against an INF deadline when the timeout is negative; zero and positive timeouts are unchanged. The coroutine Mutex/Semaphore paths already wait forever through Channel::push().
  • Non-coroutine Mutex/Semaphore keep returning false at once when held: nothing else runs there to release them, so waiting could only hang. The Lock::acquire() docblock now states this exception.
  • FileTest::testNegativeTimeoutWaitsUntilRelease forks a holder and asserts acquire(-1.0) blocks until it releases.

Verified: bin/monorepo check lock (pint, phpstan level 9) passes; phpunit tests/FileTest.php on the host passes, and the new test fails against main's File.php; the package unit suite in appwrite-dev passes (fork tests skipped there, no pcntl). The Redis Distributed path was not run locally.

Closes #13719

Related: #13756 (merged into this branch), #13787 (same fix for File and Distributed)

flcrom and others added 15 commits September 18, 2026 14:54
fix(lock): honor negative acquire timeout as wait-forever
File and Distributed keep their original retry loop and take INF as the deadline, instead of branching on a null one. The non-coroutine Mutex and Semaphore waits say why they never end.
The coroutine and uncontended cases passed before the fix, and the Redis case belongs to the e2e tier. The file lock test now reaps its child like its neighbours.
@luxor-code-review

luxor-code-review Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Review complete. No issues found.


The change adds an indefinite-wait mode to the lock package: when acquire() is called with a negative timeout, the Mutex, Semaphore, File, and Distributed backends now poll for acquisition (10 ms backoff) until the lock is released or, for the distributed backend, until the deadline (including infinity) is reached, instead of returning false immediately. Timeout 0.0 keeps its non-blocking semantics. The packages/lock/src classes share the same wait-loop pattern across all four backends, and packages/lock/tests/FileTest.php adds a parent/child process test that proves a blocked acquire succeeds once the holder releases. The CHANGELOG documents the new behavior. All candidate findings raised during review were refuted on verification — the negative-timeout blocking contract is documented and intentional, and test-robustness concerns were speculative — so no issues remain to report.

Reviewed commit: 55ec660

Outside a coroutine nothing else runs to release an in-process lock, so waiting on a negative timeout could only hang. A held Mutex or Semaphore returns false at once there again, and the Lock contract says so.
@ghost

ghost commented Oct 15, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

Limited by an open minor finding: Add negative-timeout coverage for Distributed

This PR makes negative timeouts wait without a deadline in the File and Distributed lock adapters, while preserving immediate failure when a synchronous Mutex or Semaphore is held. It clarifies that distinction in the Lock contract and changelog, and adds a fork-based File regression test that verifies acquisition after the holder releases.

Latest changes: The latest commits clarify the synchronous Mutex/Semaphore exception in the Lock contract and narrow the changelog entry to File and Distributed.

Verdict New comments Fixed Still open
✅ Approved 0 0 1
Fix with agent prompt
### Issue 1
packages/lock/src/Distributed.php:67
**Add negative-timeout coverage for Distributed**

The new regression test covers only `File`; the Redis E2E suite already tests contention and finite timeouts but never exercises `acquire(-1.0)`, so CI can miss a regression in this changed adapter. Please add a held-lock case that becomes available before the negative-timeout acquire returns.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 5
File Change
packages/lock/CHANGELOG.md Limits the changelog description of negative-timeout waiting to File and Distributed.
packages/lock/src/Distributed.php Retries acquisition indefinitely for negative timeouts.
packages/lock/src/File.php Retries acquisition indefinitely for negative timeouts.
packages/lock/src/Lock.php Clarifies that held non-coroutine Mutex and Semaphore acquisitions return false immediately.
packages/lock/tests/FileTest.php Adds a cross-process regression test for negative-timeout File acquisition.
⏳ Still open from earlier reviews · 1

Merged main (0f1e945); no changes to this PR's code since 4d6b194 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · See the inline comments. Summary

}

$deadline = microtime(true) + $timeout;
$deadline = $timeout < 0.0 ? INF : microtime(true) + $timeout;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add negative-timeout coverage for Distributed

The new regression test covers only File; the Redis E2E suite already tests contention and finite timeouts but never exercises acquire(-1.0), so CI can miss a regression in this changed adapter. Please add a held-lock case that becomes available before the negative-timeout acquire returns.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/lock/src/Distributed.php
Line: 67

Comment:
**Add negative-timeout coverage for Distributed**

The new regression test covers only `File`; the Redis E2E suite already tests contention and finite timeouts but never exercises `acquire(-1.0)`, so CI can miss a regression in this changed adapter. Please add a held-lock case that becomes available before the negative-timeout acquire returns.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · testing · Reply if this doesn't apply.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · Looks good to merge. Summary

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-lock-negative-timeout (after).

Metric Before After Change
🚀 Requests/sec 198.14 196.77 ⚪ -0.7%
⏱️ Latency P50 85.94 ms 89.02 ms ⚪ +3.6%
⏱️ Latency P95 206.3 ms 202.81 ms ⚪ -1.7%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 89.02 202.81 12,540 196.77 -3.5
Account 160.98 303.06 660 11.03 +10.21
TablesDB 85.85 164.35 6,820 110.12 +1.26
Storage 82.33 187.95 3,300 54.7 -4.02
Functions 121.81 231.87 1,760 29.84 -2.88

Top API waits (after)

API request Max wait (ms)
account.name.update 509.31
account.prefs.update 475.77
tablesdb.rows.create 473.52
functions.variables.update 428.65
functions.variables.create 400.13

@HarshMN2345
HarshMN2345 merged commit 3a51f85 into main Oct 29, 2026
93 checks 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