mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 01:19:31 +08:00
Independent maintainer review approved for 257c5e62. Windows Python 3.12 pre-commit 15/15, targeted Windows and WSL Linux regressions passed; latest HEAD CI has no failures.
50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
"""Cross-process exclusion for durable session migrations."""
|
|
|
|
from collections.abc import Iterator
|
|
from contextlib import contextmanager
|
|
import errno
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
try: # POSIX
|
|
import fcntl
|
|
except ImportError: # pragma: no cover - exercised only on Windows
|
|
fcntl = None # type: ignore[assignment]
|
|
try: # Windows
|
|
import msvcrt
|
|
except ImportError: # pragma: no cover - exercised only off Windows
|
|
msvcrt = None # type: ignore[assignment]
|
|
|
|
|
|
@contextmanager
|
|
def exclusive_lock(lock_path: Path) -> Iterator[None]:
|
|
with lock_path.open("a+b") as lock_file:
|
|
if sys.platform != "win32" and fcntl is not None:
|
|
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
|
|
elif msvcrt is not None:
|
|
# Without a lock, workers starting together race the WAL switch
|
|
# ("database is locked") and CREATE TRIGGER ("already exists").
|
|
lock_file.seek(0, 2)
|
|
if lock_file.tell() == 0:
|
|
lock_file.write(b"\0")
|
|
lock_file.flush()
|
|
lock_file.seek(0)
|
|
# LK_LOCK gives up with EDEADLK after about ten seconds, and a
|
|
# one-time upgrade of a large database can take longer than that.
|
|
# Keep waiting, as flock does on POSIX.
|
|
while True:
|
|
try:
|
|
msvcrt.locking(lock_file.fileno(), msvcrt.LK_LOCK, 1) # type: ignore[attr-defined] # Windows-only API
|
|
break
|
|
except OSError as exc:
|
|
if exc.errno != errno.EDEADLK:
|
|
raise
|
|
try:
|
|
yield
|
|
finally:
|
|
if sys.platform != "win32" and fcntl is not None:
|
|
fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
|
|
elif msvcrt is not None:
|
|
lock_file.seek(0)
|
|
msvcrt.locking(lock_file.fileno(), msvcrt.LK_UNLCK, 1) # type: ignore[attr-defined] # Windows-only API
|