@@ -28,6 +28,7 @@ class BaseYStore(ABC):
2828 metadata_callback : Callable [[], Awaitable [bytes ] | bytes ] | None = None
2929 version = 2
3030 _started : Event | None = None
31+ _stopped : Event | None = None
3132 _task_group : TaskGroup | None = None
3233 __start_lock : Lock | None = None
3334
@@ -50,6 +51,12 @@ def started(self) -> Event:
5051 self ._started = Event ()
5152 return self ._started
5253
54+ @property
55+ def stopped (self ) -> Event :
56+ if self ._stopped is None :
57+ self ._stopped = Event ()
58+ return self ._stopped
59+
5360 @property
5461 def _start_lock (self ) -> Lock :
5562 if self .__start_lock is None :
@@ -96,12 +103,14 @@ async def start(
96103 async with create_task_group () as self ._task_group :
97104 task_status .started ()
98105 self .started .set ()
106+ await self .stopped .wait ()
99107
100108 async def stop (self ) -> None :
101109 """Stop the store."""
102110 if self ._task_group is None :
103111 raise RuntimeError ("YStore not running" )
104112
113+ self .stopped .set ()
105114 self ._task_group .cancel_scope .cancel ()
106115 self ._task_group = None
107116
@@ -309,7 +318,7 @@ class MySQLiteYStore(SQLiteYStore):
309318 document_ttl : int | None = None
310319 path : str
311320 lock : Lock
312- db_initialized : Event
321+ db_initialized : Event | None
313322 _db : Connection
314323
315324 def __init__ (
@@ -329,6 +338,7 @@ def __init__(
329338 self .metadata_callback = metadata_callback
330339 self .log = log or getLogger (__name__ )
331340 self .lock = Lock ()
341+ self .db_initialized = None
332342
333343 async def start (
334344 self ,
@@ -356,10 +366,11 @@ async def start(
356366 self ._task_group .start_soon (self ._init_db )
357367 task_status .started ()
358368 self .started .set ()
369+ await self .stopped .wait ()
359370
360371 async def stop (self ) -> None :
361372 """Stop the store."""
362- if hasattr ( self , " db_initialized" ) and self .db_initialized .is_set ():
373+ if self . db_initialized is not None and self .db_initialized .is_set ():
363374 await self ._db .close ()
364375 await super ().stop ()
365376
@@ -405,6 +416,7 @@ async def _init_db(self):
405416 await db .commit ()
406417 await db .close ()
407418 self ._db = await connect (self .db_path )
419+ assert self .db_initialized is not None
408420 self .db_initialized .set ()
409421
410422 async def read (self ) -> AsyncIterator [tuple [bytes , bytes , float ]]:
@@ -413,8 +425,8 @@ async def read(self) -> AsyncIterator[tuple[bytes, bytes, float]]:
413425 Returns:
414426 A tuple of (update, metadata, timestamp) for each update.
415427 """
416- if not hasattr ( self , " db_initialized" ) :
417- raise RuntimeError ("ystore is not started" )
428+ if self . db_initialized is None :
429+ raise RuntimeError ("YStore not started" )
418430 await self .db_initialized .wait ()
419431 try :
420432 async with self .lock :
@@ -438,8 +450,8 @@ async def write(self, data: bytes) -> None:
438450 Arguments:
439451 data: The update to store.
440452 """
441- if not hasattr ( self , " db_initialized" ) :
442- raise RuntimeError ("ystore is not started" )
453+ if self . db_initialized is None :
454+ raise RuntimeError ("YStore not started" )
443455 await self .db_initialized .wait ()
444456 async with self .lock :
445457 # first, determine time elapsed since last update
0 commit comments