Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f328c7d

Browse files
committed
asyncio, Tulip issue 171: BaseEventLoop.close() now raises an exception if the
event loop is running. You must first stop the event loop and then wait until it stopped, before closing it.
1 parent 62511fd commit f328c7d

6 files changed

Lines changed: 18 additions & 3 deletions

File tree

Doc/library/asyncio-eventloop.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Run an event loop
132132
This clears the queues and shuts down the executor, but does not wait for
133133
the executor to finish.
134134

135+
The event loop must not be running.
136+
135137
This is idempotent and irreversible. No other methods should be called after
136138
this one.
137139

Lib/asyncio/base_events.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ def close(self):
247247
248248
This clears the queues and shuts down the executor,
249249
but does not wait for the executor to finish.
250+
251+
The event loop must not be running.
250252
"""
253+
if self._running:
254+
raise RuntimeError("cannot close a running event loop")
251255
if self._closed:
252256
return
253257
self._closed = True

Lib/asyncio/proactor_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,12 @@ def _make_write_pipe_transport(self, sock, protocol, waiter=None,
355355
def close(self):
356356
if self.is_closed():
357357
return
358+
super().close()
358359
self._stop_accept_futures()
359360
self._close_self_pipe()
360361
self._proactor.close()
361362
self._proactor = None
362363
self._selector = None
363-
super().close()
364364

365365
def sock_recv(self, sock, n):
366366
return self._proactor.recv(sock, n)

Lib/asyncio/selector_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ def _make_datagram_transport(self, sock, protocol,
5757
def close(self):
5858
if self.is_closed():
5959
return
60+
super().close()
6061
self._close_self_pipe()
6162
if self._selector is not None:
6263
self._selector.close()
6364
self._selector = None
64-
super().close()
6565

6666
def _socketpair(self):
6767
raise NotImplementedError

Lib/asyncio/unix_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def _socketpair(self):
4444
return socket.socketpair()
4545

4646
def close(self):
47+
super().close()
4748
for sig in list(self._signal_handlers):
4849
self.remove_signal_handler(sig)
49-
super().close()
5050

5151
def add_signal_handler(self, sig, callback, *args):
5252
"""Add a handler for a signal. UNIX only.

Lib/test/test_asyncio/test_events.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,15 @@ def test_add_fds_after_closing(self):
13651365
with self.assertRaises(RuntimeError):
13661366
loop.add_writer(w, callback)
13671367

1368+
def test_close_running_event_loop(self):
1369+
@asyncio.coroutine
1370+
def close_loop(loop):
1371+
self.loop.close()
1372+
1373+
coro = close_loop(self.loop)
1374+
with self.assertRaises(RuntimeError):
1375+
self.loop.run_until_complete(coro)
1376+
13681377

13691378
class SubprocessTestsMixin:
13701379

0 commit comments

Comments
 (0)