Skip to content

Commit 50c94cf

Browse files
Merge pull request #43 from geo-stack/fix)qtwait_race_condition
PR: fix race condition bug when timeout is None in 'qtwait' function
2 parents 46bc0a2 + 647e818 commit 50c94cf

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

qtapputils/qthelpers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,17 @@ def qtwait(condition, timeout=None, check_interval=50, error_message=None):
372372
loop = QEventLoop()
373373
start_time = time.monotonic()
374374

375+
timed_out = [False]
376+
# We use a mutable container for 'timed_out' so the inner check_condition()
377+
# can update the value in the enclosing scope (works for nested functions
378+
# without using the 'nonlocal' keyword).
379+
375380
def check_condition():
376381
elapsed_time = time.monotonic() - start_time
377382
if condition():
378383
loop.quit()
379384
elif timeout is not None and elapsed_time >= timeout:
385+
timed_out[0] = True
380386
loop.quit()
381387

382388
timer = QTimer()
@@ -386,6 +392,7 @@ def check_condition():
386392
check_condition()
387393
loop.exec_()
388394

389-
if not condition():
395+
# Only raise TimeoutError if timeout was specified and reached.
396+
if timeout is not None and timed_out[0] and not condition():
390397
raise TimeoutError(
391398
error_message or "Condition not met within timeout.")

0 commit comments

Comments
 (0)