From 05f9b9d6d83706b022390d81fcead2371b4f1551 Mon Sep 17 00:00:00 2001 From: Anton Patrushev Date: Sat, 28 Jul 2018 01:27:14 +0500 Subject: [PATCH] swallowing RuntimeError on calls to closed loops (#102) * swallowing RuntimeError on call_soon_threadsafe of closed loops * test to cover new code --- janus/__init__.py | 5 ++++- tests/test_sync.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/janus/__init__.py b/janus/__init__.py index ce05d96..1c31e52 100644 --- a/janus/__init__.py +++ b/janus/__init__.py @@ -46,8 +46,11 @@ def __init__(self, maxsize=0, *, loop=None): self._pending = set() def checked_call_soon_threadsafe(callback, *args): - if not loop.is_closed(): + try: loop.call_soon_threadsafe(callback, *args) + except RuntimeError: + # swallowing agreed in #2 + pass self._call_soon_threadsafe = checked_call_soon_threadsafe def checked_call_soon(callback, *args): diff --git a/tests/test_sync.py b/tests/test_sync.py index 4732468..9911e08 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -5,6 +5,7 @@ import queue import time import unittest +from unittest.mock import patch import threading @@ -380,6 +381,14 @@ def test_failing_queue(self): self.failing_queue_test(q) self.failing_queue_test(q) + def test_closed_loop_non_failing(self): + q = janus.Queue(QUEUE_SIZE, loop=self.loop).sync_q + # we are pacthing loop to follow setUp/tearDown agreement + with patch.object(self.loop, 'call_soon_threadsafe') as func: + func.side_effect = RuntimeError() + q.put_nowait(1) + self.assertEqual(func.call_count, 1) + if __name__ == "__main__": unittest.main()