@@ -72,10 +72,17 @@ def __init__(
7272                DeprecationWarning ,
7373            )
7474
75-     def  _stop_running (self ):
75+     async   def  _stop_running (self ):
7676        self ._running  =  False 
77-         # PEP 661 is not accepted yet, so we cannot type a sentinel 
78-         self ._tx .put_nowait (_STOP_RUNNING_SENTINEL )  # type: ignore[arg-type] 
77+ 
78+         function  =  partial (lambda  :_STOP_RUNNING_SENTINEL )
79+         future  =  asyncio .get_event_loop ().create_future ()
80+ 
81+         self ._tx .put_nowait ((future , function ))
82+ 
83+         return  await  future 
84+ 
85+ 
7986
8087    @property  
8188    def  _conn (self ) ->  sqlite3 .Connection :
@@ -105,16 +112,16 @@ def run(self) -> None:
105112            # futures) 
106113
107114            tx_item  =  self ._tx .get ()
108-             if  tx_item  is  _STOP_RUNNING_SENTINEL :
109-                 break 
110- 
111115            future , function  =  tx_item 
112116
113117            try :
114118                LOG .debug ("executing %s" , function )
115119                result  =  function ()
116120                LOG .debug ("operation %s completed" , function )
117121                future .get_loop ().call_soon_threadsafe (set_result , future , result )
122+ 
123+                 if  result  is  _STOP_RUNNING_SENTINEL :
124+                     break 
118125            except  BaseException  as  e :  # noqa B036 
119126                LOG .debug ("returning exception %s" , e )
120127                future .get_loop ().call_soon_threadsafe (set_exception , future , e )
@@ -139,7 +146,7 @@ async def _connect(self) -> "Connection":
139146                self ._tx .put_nowait ((future , self ._connector ))
140147                self ._connection  =  await  future 
141148            except  Exception :
142-                 self ._stop_running ()
149+                 await   self ._stop_running ()
143150                self ._connection  =  None 
144151                raise 
145152
@@ -180,7 +187,7 @@ async def close(self) -> None:
180187            LOG .info ("exception occurred while closing connection" )
181188            raise 
182189        finally :
183-             self ._stop_running ()
190+             await   self ._stop_running ()
184191            self ._connection  =  None 
185192
186193    @contextmanager  
0 commit comments