Skip to content

Commit 4d3ac96

Browse files
author
Michael Sparmann
committed
DummyBlockchain: Fix possible deadlock
1 parent f06f659 commit 4d3ac96

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

core/blockchain.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,16 @@ def remove_work_source(self, worksource):
183183
def check_job(self, job):
184184
if self.currentprevhash == job.prevhash: return True
185185
cancel = []
186-
with self.blocklock:
187-
now = time.time()
188-
timeout_expired = now > self.timeoutend
189-
self.timeoutend = now + 10
190-
if job.prevhash in self.knownprevhashes: return False
191-
if timeout_expired: self.knownprevhashes = [self.currentprevhash]
192-
else: self.knownprevhashes.append(self.currentprevhash)
193-
self.currentprevhash = job.prevhash
194-
with self.core.workqueue.lock:
186+
# Needs to be locked outside of blocklock to prevent race condition
187+
with self.core.workqueue.lock:
188+
with self.blocklock:
189+
now = time.time()
190+
timeout_expired = now > self.timeoutend
191+
self.timeoutend = now + 10
192+
if job.prevhash in self.knownprevhashes: return False
193+
if timeout_expired: self.knownprevhashes = [self.currentprevhash]
194+
else: self.knownprevhashes.append(self.currentprevhash)
195+
self.currentprevhash = job.prevhash
195196
while self.jobs:
196197
job = self.jobs.pop(0)
197198
if job.worker: cancel.append(job)
@@ -200,3 +201,4 @@ def check_job(self, job):
200201
self.core.log(self, "New block detected\n", 300, "B")
201202
self.core.workqueue.cancel_jobs(cancel)
202203
return True
204+

0 commit comments

Comments
 (0)