v1.0.54: fix 4 audit bugs — _client auto-detect, _find_uid by key, tracker_add before params, flock LOCK_NB

This commit is contained in:
2026-07-27 10:18:49 +04:00
parent f546f41f21
commit 338c080254
6 changed files with 126 additions and 33 deletions
+18 -2
View File
@@ -2,6 +2,7 @@
import fcntl
import json
import os
import time
_PATH = "/tmp/instances.json"
@@ -13,6 +14,19 @@ _INITIAL = {
}
def _acquire_lock(fd):
"""LOCK_EX с таймаутом 2 секунды (LOCK_NB + retry)."""
deadline = time.time() + 2
while True:
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
return True
except BlockingIOError:
if time.time() >= deadline:
return False
time.sleep(0.05)
def _locked_read():
"""Читает файл под эксклюзивной блокировкой, seed при отсутствии."""
try:
@@ -20,7 +34,8 @@ def _locked_read():
except OSError:
return dict(_INITIAL)
try:
fcntl.flock(fd, fcntl.LOCK_EX)
if not _acquire_lock(fd):
return dict(_INITIAL)
try:
data = os.read(fd, 65536)
if data:
@@ -45,7 +60,8 @@ def _locked_write(data):
except OSError:
return
try:
fcntl.flock(fd, fcntl.LOCK_EX)
if not _acquire_lock(fd):
return
os.lseek(fd, 0, 0)
os.ftruncate(fd, 0)
os.write(fd, json.dumps(data, indent=2).encode("utf-8"))