-
Notifications
You must be signed in to change notification settings - Fork 857
fix(socket-mode): only release connect_operation_lock when this task acquired it #1926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ckarnell
wants to merge
2
commits into
slackapi:main
Choose a base branch
from
ckarnell:fix-async-socket-mode-lock-release
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
tests/slack_sdk_async/socket_mode/test_async_client_lock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import asyncio | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| from slack_sdk.socket_mode.async_client import AsyncBaseSocketModeClient | ||
| from tests.slack_sdk_async.helpers import async_test | ||
|
|
||
|
|
||
| class _FakeClient(AsyncBaseSocketModeClient): | ||
| """Minimal concrete client: connect() blocks until released, like a real reconnect.""" | ||
|
|
||
| def __init__(self): | ||
| self.connect_operation_lock = asyncio.Lock() | ||
| self.trace_enabled = False | ||
| self.wss_uri = "wss://example.com/original" | ||
| self.connected = False | ||
| self.connect_started = asyncio.Event() | ||
| self.allow_connect_to_finish = asyncio.Event() | ||
|
|
||
| async def is_connected(self) -> bool: | ||
| return self.connected | ||
|
|
||
| async def issue_new_wss_url(self) -> str: | ||
| return "wss://example.com/new" | ||
|
|
||
| async def connect(self): | ||
| self.connect_started.set() | ||
| await self.allow_connect_to_finish.wait() | ||
|
|
||
| async def disconnect(self): | ||
| pass | ||
|
|
||
| async def session_id(self) -> str: | ||
| return "test-session" | ||
|
|
||
|
|
||
| class TestAsyncClientConnectLock(unittest.TestCase): | ||
| @async_test | ||
| async def test_cancelled_waiter_does_not_release_another_task_lock(self): | ||
| """A cancelled waiter must not release the reconnect that is still in progress. | ||
|
|
||
| `connect()` cancels `message_receiver` and `current_session_monitor` on every | ||
| successful reconnection, and both of those tasks call `connect_to_new_endpoint()`. | ||
| So a task can be cancelled while it is suspended inside | ||
| `connect_operation_lock.acquire()`. `asyncio.Lock` has no concept of ownership, so | ||
| releasing on `locked()` alone lets that cancelled task free a lock it never held, | ||
| which drops mutual exclusion around the reconnect. | ||
| """ | ||
| client = _FakeClient() | ||
|
|
||
| holder = asyncio.ensure_future(client.connect_to_new_endpoint(force=True)) | ||
| await asyncio.wait_for(client.connect_started.wait(), timeout=5) | ||
| self.assertTrue(client.connect_operation_lock.locked()) | ||
|
|
||
| waiter = asyncio.ensure_future(client.connect_to_new_endpoint(force=True)) | ||
| await asyncio.sleep(0.1) | ||
|
|
||
| waiter.cancel() | ||
| with self.assertRaises(asyncio.CancelledError): | ||
| await waiter | ||
|
|
||
| # The holder is still inside connect(), so the lock must still be held. | ||
| self.assertFalse(holder.done()) | ||
| self.assertTrue( | ||
| client.connect_operation_lock.locked(), | ||
| "the cancelled waiter released a lock it never acquired", | ||
| ) | ||
|
|
||
| client.allow_connect_to_finish.set() | ||
| await asyncio.wait_for(holder, timeout=5) | ||
| self.assertFalse(client.connect_operation_lock.locked()) | ||
|
|
||
| @async_test | ||
| async def test_reconnect_is_skipped_when_the_lock_cannot_be_acquired(self): | ||
| """Mirrors the sync client: acquire is bounded, and a failed acquire skips the work. | ||
|
|
||
| The sync client calls acquire(blocking=True, timeout=5) and gates the reconnect on | ||
| `acquired`, so a caller that never gets the lock does not reconnect and does not | ||
| release a lock it does not hold. | ||
| """ | ||
| client = _FakeClient() | ||
| # connect() must not block here: on the unpatched client the acquire succeeds and | ||
| # this test has to FAIL rather than hang, since a hanging control cannot be read. | ||
| client.allow_connect_to_finish.set() | ||
|
|
||
| with patch("asyncio.wait_for", side_effect=asyncio.TimeoutError): | ||
| await client.connect_to_new_endpoint() | ||
|
|
||
| self.assertFalse(client.connect_started.is_set(), "reconnected without holding the lock") | ||
| self.assertEqual(client.wss_uri, "wss://example.com/original", "the endpoint was rotated anyway") | ||
| self.assertFalse(client.connect_operation_lock.locked(), "released a lock it never acquired") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we mirror the same testing pattern found in the sync implementation 🙏