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

fix: added conversation lock file management in LogManager #295

Merged
merged 1 commit into from
Dec 8, 2024
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
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using __del__ for releasing resources is not reliable. Consider using a context manager to ensure the lock is released properly.

"""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