Skip to content

Commit 7516619

Browse files
authored
avoid solver race conditions with a lock (#569)
1 parent a1fb04a commit 7516619

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/halmos/solvers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from halmos.logs import debug, error
1818
from halmos.ui import ui
19-
from halmos.utils import format_size
19+
from halmos.utils import format_size, synchronized
2020

2121
# not defaulting to latest because of https://github.com/a16z/halmos/issues/492
2222
DEFAULT_YICES_VERSION = "2.6.4"
@@ -475,6 +475,7 @@ def find_solver_binary(solver: SolverInfo) -> Path | None:
475475
return None
476476

477477

478+
@synchronized()
478479
def ensure_solver_available(solver: SolverInfo) -> Path:
479480
"""
480481
Ensures the specified solver is available, downloading it if necessary.

src/halmos/utils.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
import math
66
import re
7+
import threading
78
import uuid
8-
from functools import partial
9+
from functools import partial, wraps
910
from timeit import default_timer as timer
1011
from typing import TYPE_CHECKING, Any, TypeAlias, Union
1112

@@ -1243,6 +1244,23 @@ def wrapper(*args, **kwargs):
12431244
return decorator
12441245

12451246

1247+
def synchronized(lock=None):
1248+
"""Decorator that synchronizes access to a method or function"""
1249+
1250+
def decorator(func):
1251+
# Create a lock for this specific function if none provided
1252+
func_lock = lock or threading.RLock()
1253+
1254+
@wraps(func)
1255+
def wrapper(*args, **kwargs):
1256+
with func_lock:
1257+
return func(*args, **kwargs)
1258+
1259+
return wrapper
1260+
1261+
return decorator
1262+
1263+
12461264
dict_of_unsupported_cheatcodes = {
12471265
0x23361207: "expectCall(address,uint256,uint64,bytes)",
12481266
0x97624631: "assertEq(bytes,bytes)",

0 commit comments

Comments
 (0)