Skip to content
This repository has been archived by the owner on Nov 23, 2017. It is now read-only.

close() an event loop while it is running #171

Closed
GoogleCodeExporter opened this issue Apr 10, 2015 · 6 comments
Closed

close() an event loop while it is running #171

GoogleCodeExporter opened this issue Apr 10, 2015 · 6 comments

Comments

@GoogleCodeExporter
Copy link

It should not be possible to close an event loop while it is still running. I 
suggest to modify the close() method to raise an error if the loop is still 
running.

Subclasses of BaseEventLoop should call the parent close() method first, or 
they will modify their state while the loop will not be closed.

Example:

---
import asyncio

@asyncio.coroutine
def test(loop):
    print(repr(loop))
    loop.stop()
    print(repr(loop))
    loop.close()
    print(repr(loop))

loop = asyncio.get_event_loop()
loop.run_until_complete(test(loop))

---

Result (with the closed patch applied):

---
<_UnixSelectorEventLoop running=True closed=False debug=False>
<_UnixSelectorEventLoop running=True closed=False debug=False>
<_UnixSelectorEventLoop running=True closed=True debug=False>
Traceback (most recent call last):
  File "x.py", line 12, in <module>
    loop.run_until_complete(test(loop))
  File "/home/haypo/prog/HG/tulip/asyncio/base_events.py", line 215, in run_until_complete
    self.run_forever()
  File "/home/haypo/prog/HG/tulip/asyncio/base_events.py", line 195, in run_forever
    self._run_once()
  File "/home/haypo/prog/HG/tulip/asyncio/base_events.py", line 822, in _run_once
    event_list = self._selector.select(timeout)
AttributeError: 'NoneType' object has no attribute 'select'

---

Original issue reported on code.google.com by victor.s...@gmail.com on 3 Jun 2014 at 11:00

@GoogleCodeExporter
Copy link
Author

I pushed my changes to add a new BaseEventLoop.is_closed() method. During the 
review of this change, Yury proposed to just return "<BaseEventLoop closed>" 
instead "<BaseEventLoop running=... debug=... closed=True>" for the 
BaseEventLoop.__repr__() method.

It looks to be possible to stop an event loop while it is running. Yury's 
suggestion would loose information, but closing a running event loop also looks 
like a bug.

Original comment by victor.s...@gmail.com on 3 Jun 2014 at 11:13

@GoogleCodeExporter
Copy link
Author

See also the initial Python bug report which is close to (but different of) my 
example in this issue: http://bugs.python.org/issue21326.

Original comment by victor.s...@gmail.com on 3 Jun 2014 at 11:14

@GoogleCodeExporter
Copy link
Author

In my "is_closed()" patch, I also add a sanity check in 
BaseProactorEventLoop._start_serving(): if the event loop was closed, just 
exit. I'm not sure that the sanity check makes sense if an event loop should 
not be closed while it is running.

Original comment by victor.s...@gmail.com on 3 Jun 2014 at 11:35

@GoogleCodeExporter
Copy link
Author

I agree to just raise when trying to close the loop from within a running 
callback/coroutine/task/etc. Let's just keep the __repr__ unchanged.

Original comment by gvanrossum@gmail.com on 5 Jun 2014 at 10:28

@GoogleCodeExporter
Copy link
Author

Proposed change to deny closing a running event loop:
http://codereview.appspot.com/107170043

Original comment by victor.s...@gmail.com on 16 Jun 2014 at 9:58

@GoogleCodeExporter
Copy link
Author

I commited my patch into Tulip (ab7a4b52c543), Python 3.4 (4f0480e92ffc) and 
Python 3.5 (e257a58bd718).

Original comment by victor.s...@gmail.com on 22 Jun 2014 at 11:04

  • Changed state: Fixed

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant