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 project only after successful open #1978

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
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
Move project lock until all steps in project open are completed (#1977)
  • Loading branch information
vkbo committed Jul 14, 2024
commit a43bd5a50e3708333d0a98076073bb23058db7be
1 change: 1 addition & 0 deletions novelwriter/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def openProject(self, projPath: str | Path, clearLock: bool = False) -> bool:
self.setProjectChanged(False)
self._valid = True
self._state = NWProjectState.READY
self._storage.lockSession() # Lock only after a successful open. See issue #1977.

SHARED.newStatusMessage(self.tr("Opened Project: {0}").format(self._data.name))

Expand Down
7 changes: 6 additions & 1 deletion novelwriter/core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ def initProjectStorage(self, path: str | Path, clearLock: bool = False) -> NWSto
if child.is_dir() and child.name.startswith("data_"):
legacy.legacyDataFolder(basePath, child)

self._writeLockFile()
self._ready = True

return NWStorageOpen.READY
Expand All @@ -249,6 +248,12 @@ def runPostSaveTasks(self, autoSave: bool = False) -> bool: # pragma: no cover
return True
return True

def lockSession(self) -> None:
"""Lock the session when the project is successfully opened."""
if self._ready:
self._writeLockFile()
return

def closeSession(self) -> None:
"""Run tasks related to closing the session."""
self._clearLockFile()
Expand Down
2 changes: 2 additions & 0 deletions tests/test_core/test_core_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def testCoreStorage_InitProjectStorage(monkeypatch, mockGUI, fncPath, mockRnd):
storage.clear()

# Open twice, where second should fail due to lockfile
# Note that locking is only possible after a successful open
assert storage.initProjectStorage(fncPath) == NWStorageOpen.READY
storage.lockSession()
assert storage.initProjectStorage(fncPath) == NWStorageOpen.LOCKED
assert isinstance(storage.lockStatus, list)
assert len(storage.lockStatus) == 4
Expand Down