Skip to content

Commit 0f98e92

Browse files
Fix file saving (#231)
1 parent 36991e0 commit 0f98e92

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

jupyter_collaboration/loaders.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(
4343
] = {}
4444

4545
self._watcher = asyncio.create_task(self._watch_file()) if self._poll_interval else None
46+
self.last_modified = None
4647

4748
@property
4849
def file_id(self) -> str:
@@ -137,16 +138,13 @@ async def save_content(self, model: dict[str, Any]) -> dict[str, Any]:
137138
self._log.info("Saving file: %s", path)
138139
return await ensure_async(self._contents_manager.save(model, path))
139140

140-
async def maybe_save_content(self, model: dict[str, Any]) -> dict[str, Any]:
141+
async def maybe_save_content(self, model: dict[str, Any]) -> None:
141142
"""
142143
Save the content of the file.
143144
144145
Parameters:
145146
model (dict): A dictionary with format, type, last_modified, and content of the file.
146147
147-
Returns:
148-
model (dict): A dictionary with the metadata and content of the file.
149-
150148
Raises:
151149
OutOfBandChanges: if the file was modified at a latter time than the model
152150
@@ -166,14 +164,21 @@ async def maybe_save_content(self, model: dict[str, Any]) -> dict[str, Any]:
166164
)
167165
)
168166

169-
if model["last_modified"] == m["last_modified"]:
167+
if self.last_modified == m["last_modified"]:
170168
self._log.info("Saving file: %s", path)
171-
return await ensure_async(self._contents_manager.save(model, path))
169+
# saving is shielded so that it cannot be cancelled
170+
# otherwise it could corrupt the file
171+
task = asyncio.create_task(self._save_content(model))
172+
await asyncio.shield(task)
172173

173174
else:
174175
# file changed on disk, raise an error
175176
raise OutOfBandChanges
176177

178+
async def _save_content(self, model: dict[str, Any]) -> None:
179+
m = await ensure_async(self._contents_manager.save(model, self.path))
180+
self.last_modified = m["last_modified"]
181+
177182
async def _watch_file(self) -> None:
178183
"""
179184
Async task for watching a file.

jupyter_collaboration/rooms.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def __init__(
3737
self._room_id: str = room_id
3838
self._file_format: str = file_format
3939
self._file_type: str = file_type
40-
self._last_modified: Any = None
4140
self._file: FileLoader = file
4241
self._document = YDOCS.get(self._file_type, YFILE)(self.ydoc)
4342

@@ -145,7 +144,7 @@ async def initialize(self) -> None:
145144
if self.ystore:
146145
await self.ystore.encode_state_as_update(self.ydoc)
147146

148-
self._last_modified = model["last_modified"]
147+
self._file.last_modified = model["last_modified"]
149148
self._document.dirty = False
150149
self.ready = True
151150
self._emit(LogLevel.INFO, "initialize", "Room initialized")
@@ -189,7 +188,7 @@ async def _on_content_change(self, event: str, args: dict[str, Any]) -> None:
189188
args (dict): A dictionary with format, type, last_modified.
190189
"""
191190
if event == "metadata" and (
192-
self._last_modified is None or self._last_modified < args["last_modified"]
191+
self._file.last_modified is None or self._file.last_modified < args["last_modified"]
193192
):
194193
self.log.info("Out-of-band changes. Overwriting the content in room %s", self._room_id)
195194
self._emit(LogLevel.INFO, "overwrite", "Out-of-band changes. Overwriting the room.")
@@ -204,7 +203,7 @@ async def _on_content_change(self, event: str, args: dict[str, Any]) -> None:
204203

205204
async with self._update_lock:
206205
self._document.source = model["content"]
207-
self._last_modified = model["last_modified"]
206+
self._file.last_modified = model["last_modified"]
208207
self._document.dirty = False
209208

210209
def _on_document_change(self, target: str, event: Any) -> None:
@@ -249,15 +248,13 @@ async def _maybe_save_document(self) -> None:
249248

250249
try:
251250
self.log.info("Saving the content from room %s", self._room_id)
252-
model = await self._file.maybe_save_content(
251+
await self._file.maybe_save_content(
253252
{
254253
"format": self._file_format,
255254
"type": self._file_type,
256-
"last_modified": self._last_modified,
257255
"content": self._document.source,
258256
}
259257
)
260-
self._last_modified = model["last_modified"]
261258
async with self._update_lock:
262259
self._document.dirty = False
263260

@@ -275,7 +272,7 @@ async def _maybe_save_document(self) -> None:
275272

276273
async with self._update_lock:
277274
self._document.source = model["content"]
278-
self._last_modified = model["last_modified"]
275+
self._file.last_modified = model["last_modified"]
279276
self._document.dirty = False
280277

281278
self._emit(LogLevel.INFO, "overwrite", "Out-of-band changes while saving.")

0 commit comments

Comments
 (0)