Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock file accesses #252

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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