Skip to content

Commit 4538d02

Browse files
Fix cancellation of file save operation (#241)
* Fix cancellation of file save operation * Add comment about _maybe_save_document's cancellation
1 parent b3e189e commit 4538d02

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

jupyter_collaboration/rooms.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,14 @@ async def _maybe_save_document(self, saving_document: asyncio.Task | None) -> No
234234
if saving_document is not None and not saving_document.done():
235235
# the document is being saved, cancel that
236236
saving_document.cancel()
237-
await saving_document
238237

239-
# save after X seconds of inactivity
240-
await asyncio.sleep(self._save_delay)
238+
# all async code (i.e. await statements) must be part of this try/except block
239+
# because this coroutine is run in a cancellable task and cancellation is handled here
241240

242241
try:
242+
# save after X seconds of inactivity
243+
await asyncio.sleep(self._save_delay)
244+
243245
self.log.info("Saving the content from room %s", self._room_id)
244246
await self._file.maybe_save_content(
245247
{
@@ -253,6 +255,9 @@ async def _maybe_save_document(self, saving_document: asyncio.Task | None) -> No
253255

254256
self._emit(LogLevel.INFO, "save", "Content saved.")
255257

258+
except asyncio.CancelledError:
259+
return
260+
256261
except OutOfBandChanges:
257262
self.log.info("Out-of-band changes. Overwriting the content in room %s", self._room_id)
258263
try:

0 commit comments

Comments
 (0)