Skip to content

Commit bfb098a

Browse files
fix(socket-mode): only release connect_operation_lock when this task acquired it (#1926)
Co-authored-by: Cohen Karnell <7363269+ckarnell@users.noreply.github.com> Co-authored-by: William Bergamin <wbergamin@slack-corp.com> Co-authored-by: William Bergamin <wbergamin@salesforce.com>
1 parent 7294cf4 commit bfb098a

2 files changed

Lines changed: 137 additions & 4 deletions

File tree

‎slack_sdk/socket_mode/async_client.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,22 @@ async def disconnect(self):
7171

7272
async def connect_to_new_endpoint(self, force: bool = False):
7373
session_id = await self.session_id()
74+
acquired = False
7475
try:
75-
await self.connect_operation_lock.acquire()
76+
try:
77+
acquired = await asyncio.wait_for(self.connect_operation_lock.acquire(), timeout=5)
78+
except asyncio.TimeoutError:
79+
acquired = False
7680
if self.trace_enabled:
77-
self.logger.debug(f"For reconnection, the connect_operation_lock was acquired (session: {session_id})")
78-
if force or not await self.is_connected():
81+
self.logger.debug(
82+
f"Attempted to acquire the connect_operation_lock for reconnection "
83+
f"(acquired={acquired}, session={session_id})"
84+
)
85+
if force or (acquired and not await self.is_connected()):
7986
self.wss_uri = await self.issue_new_wss_url()
8087
await self.connect()
8188
finally:
82-
if self.connect_operation_lock.locked() is True:
89+
if acquired:
8390
self.connect_operation_lock.release()
8491
if self.trace_enabled:
8592
self.logger.debug(f"The connect_operation_lock for reconnection was released (session: {session_id})")
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import asyncio
2+
import unittest
3+
from unittest.mock import patch
4+
5+
from slack_sdk.socket_mode.async_client import AsyncBaseSocketModeClient
6+
from tests.slack_sdk_async.helpers import async_test
7+
8+
9+
class _FakeSocketModeClient(AsyncBaseSocketModeClient):
10+
def __init__(self):
11+
self.connect_operation_lock = asyncio.Lock()
12+
self.trace_enabled = False
13+
self.wss_uri = "wss://example.com/original"
14+
self.connected = False
15+
self.connect_started = asyncio.Event()
16+
self.allow_connect_to_finish = asyncio.Event()
17+
18+
async def is_connected(self) -> bool:
19+
return self.connected
20+
21+
async def issue_new_wss_url(self) -> str:
22+
return "wss://example.com/new"
23+
24+
async def connect(self):
25+
self.connect_started.set()
26+
await self.allow_connect_to_finish.wait()
27+
28+
async def disconnect(self):
29+
pass
30+
31+
async def session_id(self) -> str:
32+
return "test-session"
33+
34+
35+
class TestAsyncBaseSocketModeClient(unittest.TestCase):
36+
@async_test
37+
async def test_cancelled_waiter_does_not_release_another_task_lock(self):
38+
client = _FakeSocketModeClient()
39+
40+
holder = asyncio.ensure_future(client.connect_to_new_endpoint(force=True))
41+
await asyncio.wait_for(client.connect_started.wait(), timeout=5)
42+
self.assertTrue(
43+
client.connect_operation_lock.locked(),
44+
"the holder never acquired the connect lock before entering connect()",
45+
)
46+
47+
waiter = asyncio.ensure_future(client.connect_to_new_endpoint())
48+
await asyncio.sleep(0.1)
49+
50+
waiter.cancel()
51+
with self.assertRaises(asyncio.CancelledError):
52+
await waiter
53+
54+
self.assertFalse(
55+
holder.done(),
56+
"the holder finished before connect() was allowed to complete",
57+
)
58+
self.assertTrue(
59+
client.connect_operation_lock.locked(),
60+
"the cancelled waiter released a lock it never acquired",
61+
)
62+
63+
client.allow_connect_to_finish.set()
64+
await asyncio.wait_for(holder, timeout=5)
65+
self.assertFalse(
66+
client.connect_operation_lock.locked(),
67+
"the holder did not release the connect lock after connect() finished",
68+
)
69+
70+
@async_test
71+
async def test_reconnect_is_skipped_when_the_lock_cannot_be_acquired(self):
72+
client = _FakeSocketModeClient()
73+
# connect() must not block here: on the unpatched client the acquire succeeds and
74+
# this test has to FAIL rather than hang, since a hanging control cannot be read.
75+
client.allow_connect_to_finish.set()
76+
77+
with patch.object(asyncio, "wait_for", side_effect=asyncio.TimeoutError):
78+
await client.connect_to_new_endpoint()
79+
80+
self.assertFalse(client.connect_started.is_set(), "reconnected without holding the lock")
81+
self.assertEqual(client.wss_uri, "wss://example.com/original", "the endpoint was rotated anyway")
82+
self.assertFalse(client.connect_operation_lock.locked(), "released a lock it never acquired")
83+
84+
@async_test
85+
async def test_force_reconnects_even_when_the_lock_cannot_be_acquired(self):
86+
client = _FakeSocketModeClient()
87+
# connect() must not block: force drives the reconnect even though the acquire timed out.
88+
client.allow_connect_to_finish.set()
89+
90+
with patch.object(asyncio, "wait_for", side_effect=asyncio.TimeoutError):
91+
await client.connect_to_new_endpoint(force=True)
92+
93+
self.assertTrue(client.connect_started.is_set(), "force did not reconnect after the acquire timed out")
94+
self.assertEqual(client.wss_uri, "wss://example.com/new", "force did not rotate the endpoint")
95+
self.assertFalse(
96+
client.connect_operation_lock.locked(),
97+
"held or released a lock it never acquired",
98+
)
99+
100+
@async_test
101+
async def test_reconnect_skipped_when_already_connected(self):
102+
client = _FakeSocketModeClient()
103+
client.connected = True
104+
client.allow_connect_to_finish.set()
105+
106+
await client.connect_to_new_endpoint()
107+
self.assertFalse(client.connect_started.is_set(), "reconnected while already connected without force")
108+
self.assertEqual(client.wss_uri, "wss://example.com/original", "rotated the endpoint without force")
109+
self.assertFalse(
110+
client.connect_operation_lock.locked(),
111+
"the connect lock was not released after skipping the reconnect",
112+
)
113+
114+
@async_test
115+
async def test_force_reconnects_even_when_already_connected(self):
116+
client = _FakeSocketModeClient()
117+
client.connected = True
118+
client.allow_connect_to_finish.set()
119+
120+
await client.connect_to_new_endpoint(force=True)
121+
self.assertTrue(client.connect_started.is_set(), "force did not trigger a reconnect")
122+
self.assertEqual(client.wss_uri, "wss://example.com/new", "force did not rotate the endpoint")
123+
self.assertFalse(
124+
client.connect_operation_lock.locked(),
125+
"the connect lock was not released after a forced reconnect",
126+
)

0 commit comments

Comments
 (0)