diff --git a/jupyter_collaboration/rooms.py b/jupyter_collaboration/rooms.py index d1e2a6bc..bd6cd1d3 100644 --- a/jupyter_collaboration/rooms.py +++ b/jupyter_collaboration/rooms.py @@ -48,6 +48,7 @@ def __init__( self._cleaner: asyncio.Task | None = None self._saving_document: asyncio.Task | None = None self._messages: dict[str, asyncio.Lock] = {} + self._background_tasks = set() # Listen for document changes self._document.observe(self._on_document_change) @@ -100,7 +101,9 @@ async def initialize(self) -> None: # try to apply Y updates from the YStore for this document read_from_source = True if self.ystore is not None: - await self.ystore.started.wait() + if not self.ystore.started.is_set(): + self.create_task(self.ystore.start()) + await self.ystore.started.wait() try: await self.ystore.apply_updates(self.ydoc) self._emit( @@ -174,6 +177,11 @@ async def stop(self) -> None: self._document.unobserve() self._file.unobserve(self.room_id) + def create_task(self, aw): + task = asyncio.create_task(aw) + self._background_tasks.add(task) + task.add_done_callback(self._background_tasks.discard) + async def _broadcast_updates(self): # FIXME should be upstreamed try: