Skip to content

Commit 0dc5452

Browse files
committed
Update SSL util to use async lock
1 parent af01eda commit 0dc5452

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

api/transformerlab/shared/ssl_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import asyncio
34
import datetime as _dt
45
import ipaddress as _ip
56
from pathlib import Path
@@ -9,7 +10,6 @@
910
from cryptography.hazmat.primitives import hashes, serialization
1011
from cryptography.hazmat.primitives.asymmetric import rsa
1112
from cryptography.x509.oid import NameOID
12-
from filelock import FileLock
1313

1414
from lab.dirs import get_workspace_dir
1515
from lab import storage
@@ -18,6 +18,11 @@
1818
"ensure_persistent_self_signed_cert",
1919
]
2020

21+
# In-process lock: prevents concurrent async tasks from racing to generate
22+
# the cert simultaneously. FileLock is not suitable here because the storage
23+
# awaits inside the critical section now genuinely yield to the event loop.
24+
_cert_lock = asyncio.Lock()
25+
2126

2227
async def ensure_persistent_self_signed_cert() -> Tuple[str, str]:
2328
# Compute paths lazily to avoid asyncio.run() at module level
@@ -26,8 +31,7 @@ async def ensure_persistent_self_signed_cert() -> Tuple[str, str]:
2631
cert_path = cert_dir / "server-cert.pem"
2732
key_path = cert_dir / "server-key.pem"
2833

29-
lock = cert_dir / ".cert.lock"
30-
with FileLock(str(lock)):
34+
async with _cert_lock:
3135
if await storage.exists(str(cert_path)) and await storage.exists(str(key_path)):
3236
return str(cert_path), str(key_path)
3337
await storage.makedirs(str(cert_dir), exist_ok=True)

0 commit comments

Comments
 (0)