diff --git a/canopen/emcy.py b/canopen/emcy.py index 8b7d3bff..be2b3f32 100644 --- a/canopen/emcy.py +++ b/canopen/emcy.py @@ -72,16 +72,19 @@ def wait( while True: with self.emcy_received: prev_log_size = len(self.log) - self.emcy_received.wait(timeout) + remaining = end_time - time.time() + if remaining <= 0: + return None # Actual timeout reached + self.emcy_received.wait(remaining) if len(self.log) == prev_log_size: - # Resumed due to timeout - return None + if time.time() >= end_time: + # Resumed due to timeout + return None + else: + continue # Get last logged EMCY emcy = self.log[-1] logger.info("Got %s", emcy) - if time.time() > end_time: - # No valid EMCY received on time - return None if emcy_code is None or emcy.code == emcy_code: # This is the one we're interested in return emcy diff --git a/test/test_emcy.py b/test/test_emcy.py index b4b54a19..788ad720 100644 --- a/test/test_emcy.py +++ b/test/test_emcy.py @@ -86,6 +86,10 @@ def test_emcy_consumer_wait(self): def push_err(): emcy.on_emcy(0x81, b'\x01\x20\x01\x01\x02\x03\x04\x05', 100) + def no_err(): + with emcy.emcy_received: + emcy.emcy_received.notify_all() + def check_err(err): self.assertIsNotNone(err) self.check_error( @@ -103,6 +107,10 @@ def check_err(err): ): check_err(emcy.wait(timeout=TIMEOUT)) + # Check unfiltered wait, spurious condition wake-up. + with mock_rx_thread(emcy, no_err): + self.assertIsNone(emcy.wait(timeout=TIMEOUT)) + # Check filtered wait, on success. with ( self.assertLogs(level=logging.INFO),