From 647e81866e3ae09ec9e3f7693114d642e8345cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Thu, 2 Oct 2025 15:22:15 -0400 Subject: [PATCH] qtwait: fix race condition bug when timeout is None --- qtapputils/qthelpers.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/qtapputils/qthelpers.py b/qtapputils/qthelpers.py index 56615ab..12aef1f 100644 --- a/qtapputils/qthelpers.py +++ b/qtapputils/qthelpers.py @@ -372,11 +372,17 @@ def qtwait(condition, timeout=None, check_interval=50, error_message=None): loop = QEventLoop() start_time = time.monotonic() + timed_out = [False] + # We use a mutable container for 'timed_out' so the inner check_condition() + # can update the value in the enclosing scope (works for nested functions + # without using the 'nonlocal' keyword). + def check_condition(): elapsed_time = time.monotonic() - start_time if condition(): loop.quit() elif timeout is not None and elapsed_time >= timeout: + timed_out[0] = True loop.quit() timer = QTimer() @@ -386,6 +392,7 @@ def check_condition(): check_condition() loop.exec_() - if not condition(): + # Only raise TimeoutError if timeout was specified and reached. + if timeout is not None and timed_out[0] and not condition(): raise TimeoutError( error_message or "Condition not met within timeout.")