Skip to content

Commit 86b3ac3

Browse files
davidbrochartandrii-i
authored andcommitted
Start ystore in a task (jupyterlab#303)
* Start ystore in a task * Use YStore's start_lock * Bump pycrdt-websocket>=0.13.4
1 parent 288b869 commit 86b3ac3

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
class DocumentRoom(BaseRoom):
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,
@@ -47,6 +49,7 @@ def __init__(
4749
self._cleaner: asyncio.Task | None = None
4850
self._saving_document: asyncio.Task | None = None
4951
self._messages: dict[str, asyncio.Lock] = {}
52+
self._background_tasks = set()
5053

5154
# Listen for document changes
5255
self._document.observe(self._on_document_change)
@@ -78,6 +81,10 @@ async def initialize(self) -> None:
7881
# try to apply Y updates from the YStore for this document
7982
read_from_source = True
8083
if self.ystore is not None:
84+
async with self.ystore.start_lock:
85+
if not self.ystore.started.is_set():
86+
self.create_task(self.ystore.start())
87+
await self.ystore.started.wait()
8188
try:
8289
await self.ystore.apply_updates(self.ydoc)
8390
self._emit(
@@ -152,7 +159,20 @@ async def stop(self) -> None:
152159
if self._saving_document:
153160
self._saving_document.cancel()
154161

155-
return super().stop()
162+
self._document.unobserve()
163+
self._file.unobserve(self.room_id)
164+
165+
def create_task(self, aw):
166+
task = asyncio.create_task(aw)
167+
self._background_tasks.add(task)
168+
task.add_done_callback(self._background_tasks.discard)
169+
170+
async def _broadcast_updates(self):
171+
# FIXME should be upstreamed
172+
try:
173+
await super()._broadcast_updates()
174+
except asyncio.CancelledError:
175+
pass
156176

157177
async def _on_outofband_change(self) -> None:
158178
"""

projects/jupyter-server-ydoc/pyproject.toml

Lines changed: 1 addition & 1 deletion
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)