Skip to content

Conversation

@ArnabChatterjee20k
Copy link
Contributor

@ArnabChatterjee20k ArnabChatterjee20k commented Jan 27, 2026

Coroutine based lock creating problems when pool is init outside the coroutine leading to the errors like 'must be accessed inside coroutine'

Summary by CodeRabbit

  • Refactor

    • Removed timeout parameter from pool synchronization calls and updated related method signatures.
    • Switched the Swoole adapter to a different locking primitive for more reliable lock handling and simplified error behavior.
  • Tests

    • Added a test to validate pool adapter initialization outside coroutine contexts.
  • Chores

    • Updated a development dependency constraint and PHPStan configuration for stricter static analysis.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 27, 2026

Warning

Rate limit exceeded

@ArnabChatterjee20k has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 0 minutes and 22 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Walkthrough

The PR removes the timeout parameter from synchronized(callable $callback, int $timeout): mixed, changing the signature to synchronized(callable $callback): mixed in src/Pools/Adapter.php and its implementations (Stack, Swoole). Swoole switches from a Channel-based lock to Swoole\Coroutine\Lock and updates lock/unlock and error handling accordingly. Call sites in src/Pools/Pool.php were updated to stop passing timeout values. A test testInitOutsideCoroutineNotThrowAnyError was added for the Swoole adapter. Composer dev dependency and PHPStan config were updated.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Update swoole lock' directly corresponds to the main change: replacing the Swoole coroutine locking mechanism with Swoole\Coroutine\Lock to fix initialization errors outside coroutines.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

}
public function testInitOutsideCoroutineNotThrowAnyError()
{
$pool = new Pool(new Swoole(), 'test', 1, fn () => 'x');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

similar to the appwrite Realtime pubsub

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/Pools/Adapter/Swoole.php`:
- Around line 13-18: The Swoole Coroutine Lock (Swoole\Coroutine\Lock) is used
in the Swoole.php class (property $lock and initialize method) but requires
Swoole >= 6.0.1; update composer.json to require "swoole": ">=6.0.1" or add a
runtime guard in the Swoole.php initialize() (and the class property usage) that
checks Swoole::VERSION or defined constant and either instantiates
Swoole\Coroutine\Lock only when the version is >= 6.0.1 or falls back to an
alternative (e.g., a noop mutex or a userland lock) and throw a clear error if
no compatible lock is available; reference the property $lock, the
initialize(int $size): static method and the Swoole\Coroutine\Lock symbol when
making changes.
🧹 Nitpick comments (3)
tests/Pools/Adapter/SwooleTest.php (1)

356-360: Good coverage for the initialization scenario.

The test validates that Pool initialization with the Swoole adapter no longer throws "must be accessed inside coroutine" errors. Consider extending this test to verify that basic operations (like calling methods that use synchronized()) also work outside a coroutine context, as that would more fully validate the fix.

src/Pools/Pool.php (1)

171-190: Docblock now misleading: setSynchronizationTimeout no longer affects lock acquisition.

The docblock at lines 172-177 states this sets "the lock timeout for adapters that support synchronized locking." However, with this PR, synchronized() no longer accepts a timeout parameter—the $synchronizedTimeout value is only used for pop() operations (line 286). Consider updating the docblock to clarify the actual usage or renaming the property/methods to better reflect their purpose (e.g., setPopTimeout).

src/Pools/Adapter/Swoole.php (1)

48-71: Docblock inconsistency: timeout reference no longer applicable.

Line 57 states "@throws \RuntimeException If the lock cannot be acquired within the timeout" but the method no longer accepts or uses a timeout parameter. The Lock::lock() method blocks indefinitely until the lock is acquired (unless deadlock occurs).

📝 Suggested docblock fix
     /**
      * Executes a callback while holding a lock.
      *
      * The lock is acquired before invoking the callback and is always released
      * afterward, even if the callback throws an exception.
      *
      * `@param` callable $callback Callback to execute within the critical section.
      * `@return` mixed The value returned by the callback.
      *
-     * `@throws` \RuntimeException If the lock cannot be acquired within the timeout.
+     * `@throws` \RuntimeException If the lock cannot be acquired.
     */

@abnegate abnegate merged commit b7d8dd0 into main Jan 28, 2026
4 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.

3 participants