Skip to content

Commit

Permalink
Lock file accesses (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Nov 29, 2022
1 parent 292a12a commit 49b3019
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions plugins/yjs/fps_yjs/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class YDocWebSocketHandler:
def __init__(self, websocket, path, permissions):
self.websocket = websocket
self.can_write = permissions is None or "write" in permissions.get("yjs", [])
self.lock = asyncio.Lock()
self.room = self.websocket_server.get_room(self.websocket.path)
self.set_file_info(path)

Expand Down Expand Up @@ -243,7 +244,8 @@ async def watch_file(self):

async def maybe_load_document(self):
file_format, file_type, file_path = await self.get_file_info()
model = await read_content(file_path, False)
async with self.lock:
model = await read_content(file_path, False)
# do nothing if the file was saved by us
if self.last_modified < to_datetime(model.last_modified):
is_notebook = file_type == "notebook"
Expand Down Expand Up @@ -285,7 +287,8 @@ async def maybe_save_document(self):
except Exception:
return
is_notebook = file_type == "notebook"
model = await read_content(file_path, True, as_json=is_notebook)
async with self.lock:
model = await read_content(file_path, True, as_json=is_notebook)
if self.last_modified < to_datetime(model.last_modified):
# file changed on disk, let's revert
self.room.document.source = model.content
Expand All @@ -302,9 +305,10 @@ async def maybe_save_document(self):
"path": file_path,
"type": file_type,
}
await write_content(content)
model = await read_content(file_path, False)
self.last_modified = to_datetime(model.last_modified)
async with self.lock:
await write_content(content)
model = await read_content(file_path, False)
self.last_modified = to_datetime(model.last_modified)
self.room.document.dirty = False


Expand Down

0 comments on commit 49b3019

Please sign in to comment.