Skip to content

Commit a097975

Browse files
fix: set vars inside thread to avoid recursion death
1 parent e8d6994 commit a097975

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

brownie/network/middlewares/caching.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,13 @@ def __init__(self, w3: Web3) -> None:
107107

108108
self.lock = threading.Lock()
109109
self.event = threading.Event()
110-
self.start()
110+
self.start_block_filter_loop()
111111

112-
def start(self):
113-
latest = self.w3.eth.get_block("latest")
114-
self.last_block = latest.hash
115-
self.last_block_seen = latest.timestamp
116-
self.last_request = time.time()
117-
self.block_cache: OrderedDict = OrderedDict()
118-
self.block_filter = self.w3.eth.filter("latest")
119-
120-
self.is_killed = False
112+
def start_block_filter_loop(self):
113+
self.event.clear()
121114
self.loop_thread = threading.Thread(target=self.loop_exception_handler, daemon=True)
122115
self.loop_thread.start()
116+
self.event.wait()
123117

124118
@classmethod
125119
def get_layer(cls, w3: Web3, network_type: str) -> Optional[int]:
@@ -151,6 +145,16 @@ def loop_exception_handler(self) -> None:
151145
self.is_killed = True
152146

153147
def block_filter_loop(self) -> None:
148+
# initialize required state variables within the loop to avoid recursion death
149+
latest = self.w3.eth.get_block("latest")
150+
self.last_block = latest.hash
151+
self.last_block_seen = latest.timestamp
152+
self.last_request = time.time()
153+
self.block_cache: OrderedDict = OrderedDict()
154+
self.block_filter = self.w3.eth.filter("latest")
155+
self.is_killed = False
156+
self.event.set()
157+
154158
while not self.is_killed:
155159
# if the last RPC request was > 60 seconds ago, reduce the rate of updates.
156160
# we eventually settle at one query per minute after 10 minutes of no requests.
@@ -229,7 +233,7 @@ def process_request(self, make_request: Callable, method: str, params: List) ->
229233

230234
if not self.loop_thread.is_alive():
231235
# restart the block filter loop if it has crashed (usually from a ConnectionError)
232-
self.start()
236+
self.start_block_filter_loop()
233237

234238
with self.lock:
235239
self.last_request = time.time()

0 commit comments

Comments
 (0)