Skip to content

Commit

Permalink
fix: added conversation lock file management in LogManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Dec 1, 2024
1 parent 88c715b commit 48b62ca
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion gptme/logmanager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fcntl
import json
import logging
import shutil
Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(
log: list[Message] | None = None,
logdir: PathLike | None = None,
branch: str | None = None,
lock: bool = True,
):
self.current_branch = branch or "main"

Expand All @@ -82,6 +84,24 @@ def __init__(
self.logdir = Path(fpath)
self.name = self.logdir.name

# Create and optionally lock the directory
self.logdir.mkdir(parents=True, exist_ok=True)
if lock:
self._lockfile = self.logdir / ".lock"
self._lockfile.touch(exist_ok=True)
self._lock_fd = self._lockfile.open("w")

# Try to acquire an exclusive lock

try:
fcntl.flock(self._lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
logger.debug(f"Acquired lock on {self.logdir}")
except BlockingIOError:
self._lock_fd.close()
raise RuntimeError(
f"Another gptme instance is using {self.logdir}"
) from None

# load branches from adjacent files
self._branches = {self.current_branch: Log(log or [])}
if self.logdir / "conversation.jsonl":
Expand All @@ -97,7 +117,15 @@ def __init__(
if _branch not in self._branches:
self._branches[_branch] = Log.read_jsonl(file)

# TODO: Check if logfile has contents, then maybe load, or should it overwrite?
def __del__(self):
"""Release the lock and close the file descriptor"""
if hasattr(self, "_lock_fd"):
try:
fcntl.flock(self._lock_fd, fcntl.LOCK_UN)
self._lock_fd.close()
logger.debug(f"Released lock on {self.logdir}")
except Exception as e:
logger.warning(f"Error releasing lock: {e}")

@property
def log(self) -> Log:
Expand Down

0 comments on commit 48b62ca

Please sign in to comment.