File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -87,25 +87,26 @@ def acquire_sync(self, agent_name: str) -> None:
8787 """Synchronous acquire — for non-async code paths.
8888
8989 Prefer async acquire() when possible.
90+ If called while an event loop is already running in the current thread,
91+ this method raises RuntimeError to avoid deadlock.
9092 """
9193 sem = self ._get_semaphore (agent_name )
9294 if sem is None :
9395 return
9496 try :
9597 asyncio .get_running_loop ()
96- # Running loop in current thread: blocking here can deadlock.
97- logger .warning (
98- f"acquire_sync('{ agent_name } ') called with a running event loop; "
99- "use async acquire() in async contexts."
100- )
101- return
10298 except RuntimeError :
10399 # No running loop — safe to create one
104100 loop = asyncio .new_event_loop ()
105101 try :
106102 loop .run_until_complete (sem .acquire ())
107103 finally :
108104 loop .close ()
105+ else :
106+ raise RuntimeError (
107+ f"acquire_sync('{ agent_name } ') cannot be called with a running event loop; "
108+ "use async acquire() in async contexts."
109+ )
109110
110111 def release (self , agent_name : str ) -> None :
111112 """Release concurrency slot for agent. No-op if unlimited."""
Original file line number Diff line number Diff line change @@ -109,10 +109,15 @@ def test_sync_acquire_release(self):
109109
110110 @pytest .mark .asyncio
111111 async def test_sync_acquire_running_loop_noop (self ):
112- """Sync acquire in async context should not block event loop ."""
112+ """Sync acquire in async context should fail fast without changing state ."""
113113 from praisonaiagents .agent .concurrency import ConcurrencyRegistry
114114 reg = ConcurrencyRegistry ()
115115 reg .set_limit ("loop_agent" , 1 )
116116 await reg .acquire ("loop_agent" )
117- reg .acquire_sync ("loop_agent" )
117+ with pytest .raises (RuntimeError , match = "running event loop" ):
118+ reg .acquire_sync ("loop_agent" )
119+ with pytest .raises (asyncio .TimeoutError ):
120+ await asyncio .wait_for (reg .acquire ("loop_agent" ), timeout = 0.05 )
121+ reg .release ("loop_agent" )
122+ await asyncio .wait_for (reg .acquire ("loop_agent" ), timeout = 0.05 )
118123 reg .release ("loop_agent" )
You can’t perform that action at this time.
0 commit comments