Fix critical race conditions and unsafe disk report command
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from pathlib import Path
|
||||
import os
|
||||
import time
|
||||
|
||||
LOCK_DIR = Path("/var/run/tg-bot")
|
||||
@@ -11,9 +12,14 @@ def lock_path(name: str) -> Path:
|
||||
|
||||
def acquire_lock(name: str) -> bool:
|
||||
p = lock_path(name)
|
||||
if p.exists():
|
||||
try:
|
||||
fd = os.open(str(p), os.O_CREAT | os.O_EXCL | os.O_WRONLY)
|
||||
except FileExistsError:
|
||||
return False
|
||||
p.write_text(str(time.time()))
|
||||
try:
|
||||
os.write(fd, str(time.time()).encode("ascii", errors="ignore"))
|
||||
finally:
|
||||
os.close(fd)
|
||||
return True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user