From e9c572d099c88747d16cdc4baa35ba6a30efb38f Mon Sep 17 00:00:00 2001 From: David Brochart Date: Mon, 6 May 2024 10:55:05 +0200 Subject: [PATCH] Start ystore in a task --- .../jupyter-server-ydoc/jupyter_server_ydoc/rooms.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py b/projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py index e774e011..4fb155dd 100644 --- a/projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py +++ b/projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py @@ -21,6 +21,8 @@ class DocumentRoom(YRoom): """A Y room for a possibly stored document (e.g. a notebook).""" + _background_tasks: set[asyncio.Task] + def __init__( self, room_id: str, @@ -48,6 +50,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,6 +103,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: + 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( @@ -177,6 +183,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: