10000 Closes #22922: More EventLoop methods fail if the loop is closed. Ini… · python/cpython@e80bf0d · GitHub
[go: up one dir, main page]

Skip to content

Commit e80bf0d

Browse files
committed
Closes #22922: More EventLoop methods fail if the loop is closed. Initial patch
written by Torsten Landschoff. create_task(), call_at(), call_soon(), call_soon_threadsafe() and run_in_executor() now raise an error if the event loop is closed.
1 parent dd8224e commit e80bf0d

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

Lib/asyncio/base_events.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def create_task(self, coro):
177177
178178
Return a task object.
179179
"""
180+
self._check_closed()
180181
task = tasks.Task(coro, loop=self)
181182
if task._source_traceback:
182183
del task._source_traceback[-1]
@@ -360,6 +361,7 @@ def call_at(self, when, callback, *args):
360361
if (coroutines.iscoroutine(callback)
361362
or coroutines.iscoroutinefunction(callback)):
362363
raise TypeError("coroutines cannot be used with call_at()")
364+
self._check_closed()
363365
if self._debug:
364366
self._assert_is_current_event_loop()
365367
timer = events.TimerHandle(when, callback, args, self)
@@ -390,6 +392,7 @@ def _call_soon(self, callback, args, check_loop):
390392
raise TypeError("coroutines cannot be used with call_soon()")
391393
if self._debug and check_loop:
392394
self._assert_is_current_event_loop()
395+
self._check_closed()
393396
handle = events.Handle(callback, args, self)
394397
if handle._source_traceback:
395398
del handle._source_traceback[-1]
@@ -426,6 +429,7 @@ def run_in_executor(self, executor, callback, *args):
426429
if (coroutines.iscoroutine(callback)
427430
or coroutines.iscoroutinefunction(callback)):
428431
raise TypeError("coroutines cannot be used with run_in_executor()")
432+
self._check_closed()
429433
if isinstance(callback, events.Handle):
430434
assert not args
431435
assert not isinstance(callback, events.TimerHandle)

Lib/asyncio/unix_events.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def add_signal_handler(self, sig, callback, *args):
7171
or coroutines.iscoroutinefunction(callback)):
7272
raise TypeError("coroutines cannot be used with add_signal_handler()")
7373
self._check_signal(sig)
74+
self._check_closed()
7475
try:
7576
# set_wakeup_fd() raises ValueError if this is not the
7677
# main thread. By calling it early we ensure that an

Lib/test/test_asyncio/test_events.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ def setUp(self):
226226

227227
def tearDown(self):
228228
# just in case if we have transport close callbacks
229-
test_utils.run_briefly(self.loop)
229+
if not self.loop.is_closed():
230+
test_utils.run_briefly(self.loop)
230231

231232
self.loop.close()
232233
gc.collect()
@@ -1434,6 +1435,38 @@ def close_loop(loop):
14341435
with self.assertRaises(RuntimeError):
14351436
self.loop.run_until_complete(coro)
14361437

1438+
def test_close(self):
1439+
self.loop.close()
1440+
1441+
@asyncio.coroutine
1442+
def test():
1443+
pass
1444+
1445+
func = lambda: False
1446+
coro = test()
1447+
self.addCleanup(coro.close)
1448+
1449+
# operation blocked when the loop is closed
1450+
with self.assertRaises(RuntimeError):
1451+
self.loop.run_forever()
1452+
with self.assertRaises(RuntimeError):
1453+
fut = asyncio.Future(loop=self.loop)
1454+
self.loop.run_until_complete(fut)
1455+
with self.assertRaises(RuntimeError):
1456+
self.loop.call_soon(func)
1457+
with self.assertRaises(RuntimeError):
1458+
self.loop.call_soon_threadsafe(func)
1459+
with self.assertRaises(RuntimeError):
1460+
self.loop.call_later(1.0, func)
1461+
with self.assertRaises(RuntimeError):
1462+
self.loop.call_at(self.loop.time() + .0, func)
1463+
with self.assertRaises(RuntimeError):
1464+
self.loop.run_in_executor(None, func)
1465+
with self.assertRaises(RuntimeError):
1466+
self.loop.create_task(coro)
1467+
with self.assertRaises(RuntimeError):
1468+
self.loop.add_signal_handler(signal.SIGTERM, func)
1469+
14371470

14381471
class SubprocessTestsMixin:
14391472

0 commit comments

Comments
 (0)
0