Skip to content

Commit 5b166c4

Browse files
Start ystore in a task (#303)
* Start ystore in a task * Use YStore's start_lock * Bump pycrdt-websocket>=0.13.4
1 parent 0cc728a commit 5b166c4

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

projects/jupyter-server-ydoc/jupyter_server_ydoc/rooms.py

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
class DocumentRoom(YRoom):
2222
"""A Y room for a possibly stored document (e.g. a notebook)."""
2323

24+
_background_tasks: set[asyncio.Task]
25+
2426
def __init__(
2527
self,
2628
room_id: str,
@@ -48,6 +50,7 @@ def __init__(
4850
self._cleaner: asyncio.Task | None = None
4951
self._saving_document: asyncio.Task | None = None
5052
self._messages: dict[str, asyncio.Lock] = {}
53+
self._background_tasks = set()
5154

5255
# Listen for document changes
5356
self._document.observe(self._on_document_change)
@@ -100,6 +103,10 @@ async def initialize(self) -> None:
100103
# try to apply Y updates from the YStore for this document
101104
read_from_source = True
102105
if self.ystore is not None:
106+
async with self.ystore.start_lock:
107+
if not self.ystore.started.is_set():
108+
self.create_task(self.ystore.start())
109+
await self.ystore.started.wait()
103110
try:
104111
await self.ystore.apply_updates(self.ydoc)
105112
self._emit(
@@ -177,6 +184,11 @@ async def stop(self) -> None:
177184
self._document.unobserve()
178185
self._file.unobserve(self.room_id)
179186

187+
def create_task(self, aw):
188+
task = asyncio.create_task(aw)
189+
self._background_tasks.add(task)
190+
task.add_done_callback(self._background_tasks.discard)
191+
180192
async def _broadcast_updates(self):
181193
# FIXME should be upstreamed
182194
try:

projects/jupyter-server-ydoc/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ authors = [
3030
dependencies = [
3131
"jupyter_server>=2.4.0,<3.0.0",
3232
"jupyter_ydoc>=2.0.0,<3.0.0",
33-
"pycrdt-websocket>=0.13.1,<0.14.0",
33+
"pycrdt-websocket>=0.13.4,<0.14.0",
3434
"jupyter_events>=0.10.0",
3535
"jupyter_server_fileid>=0.7.0,<1",
3636
"jsonschema>=4.18.0"

0 commit comments

Comments
 (0)