File tree Expand file tree Collapse file tree
tests/slack_sdk_async/socket_mode Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -438,16 +438,12 @@ async def send_message(self, message: str):
438438 )
439439 # Although acquiring self.connect_operation_lock also for the first method call is the safest way,
440440 # we avoid synchronizing a lot for better performance. That's why we are doing a retry here.
441- try :
442- await self .connect_operation_lock .acquire ()
441+ async with self .connect_operation_lock :
443442 if await self .is_connected ():
444443 await self .current_session .send_str (message ) # type: ignore[union-attr]
445444 else :
446445 self .logger .warning (f"The current session ({ session_id } ) is no longer active. Failed to send a message" )
447446 raise e
448- finally :
449- if self .connect_operation_lock .locked () is True :
450- self .connect_operation_lock .release ()
451447
452448 async def close (self ):
453449 self .closed = True
Original file line number Diff line number Diff line change @@ -245,15 +245,12 @@ async def send_message(self, message: str):
245245 )
246246 # Although acquiring self.connect_operation_lock also for the first method call is the safest way,
247247 # we avoid synchronizing a lot for better performance. That's why we are doing a retry here.
248- try :
248+ async with self . connect_operation_lock :
249249 if await self .is_connected ():
250250 await self .current_session .send (message ) # type: ignore[union-attr]
251251 else :
252252 self .logger .warning (f"The current session ({ session_id } ) is no longer active. Failed to send a message" )
253253 raise e
254- finally :
255- if self .connect_operation_lock .locked () is True :
256- self .connect_operation_lock .release ()
257254
258255 async def close (self ):
259256 self .closed = True
Original file line number Diff line number Diff line change @@ -189,6 +189,30 @@ async def test_enqueue_message(self):
189189 await client .disconnect ()
190190 await client .close ()
191191
192+ @async_test
193+ async def test_send_message_leaves_a_lock_held_by_another_task (self ):
194+ client = SocketModeClient (
195+ app_token = "xapp-A111-222-xyz" ,
196+ web_client = self .web_client ,
197+ auto_reconnect_enabled = False ,
198+ )
199+
200+ async def send_str (message ):
201+ raise ConnectionError ("the connection was replaced" )
202+
203+ client .current_session = MagicMock ()
204+ client .current_session .send_str = send_str
205+ await client .connect_operation_lock .acquire ()
206+ try :
207+ with self .assertRaises (asyncio .TimeoutError ):
208+ await asyncio .wait_for (client .send_message ("hello" ), timeout = 0.1 )
209+ self .assertTrue (client .connect_operation_lock .locked ())
210+ finally :
211+ if client .connect_operation_lock .locked ():
212+ client .connect_operation_lock .release ()
213+ client .current_session = None
214+ await client .close ()
215+
192216
193217async def listener (self , message , raw_message ):
194218 pass
Original file line number Diff line number Diff line change 1+ import asyncio
12import unittest
3+ from unittest .mock import MagicMock
4+
5+ from websockets .exceptions import WebSocketException
26
37from slack_sdk .socket_mode .websockets import SocketModeClient
48from slack_sdk .web .async_client import AsyncWebClient
@@ -70,6 +74,29 @@ async def test_enqueue_message(self):
7074 finally :
7175 await client .close ()
7276
77+ @async_test
78+ async def test_send_message_leaves_a_lock_held_by_another_task (self ):
79+ client = SocketModeClient (
80+ app_token = "xapp-A111-222-xyz" ,
81+ web_client = self .web_client ,
82+ )
83+
84+ async def send (message ):
85+ raise WebSocketException ("the connection was replaced" )
86+
87+ client .current_session = MagicMock ()
88+ client .current_session .send = send
89+ await client .connect_operation_lock .acquire ()
90+ try :
91+ with self .assertRaises (asyncio .TimeoutError ):
92+ await asyncio .wait_for (client .send_message ("hello" ), timeout = 0.1 )
93+ self .assertTrue (client .connect_operation_lock .locked ())
94+ finally :
95+ if client .connect_operation_lock .locked ():
96+ client .connect_operation_lock .release ()
97+ client .current_session = None
98+ await client .close ()
99+
73100
74101async def listener (message , raw_message ):
75102 pass
You can’t perform that action at this time.
0 commit comments