{"action":"created","issue":{"url":"https://api.github.com/repos/python/asyncio/issues/258","labels_url":"https://api.github.com/repos/python/asyncio/issues/258/labels{/name}","comments_url":"https://api.github.com/repos/python/asyncio/issues/258/comments","events_url":"https://api.github.com/repos/python/asyncio/issues/258/events","html_url":"https://github.com/python/asyncio/issues/258","id":97295261,"number":258,"title":"Graceful loop shutdown with running executor tasks","user":{"login":"asvetlov","id":356399,"avatar_url":"https://avatars.githubusercontent.com/u/356399?v=3","gravatar_id":"","url":"https://api.github.com/users/asvetlov","html_url":"https://github.com/asvetlov","followers_url":"https://api.github.com/users/asvetlov/followers","following_url":"https://api.github.com/users/asvetlov/following{/other_user}","gists_url":"https://api.github.com/users/asvetlov/gists{/gist_id}","starred_url":"https://api.github.com/users/asvetlov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asvetlov/subscriptions","organizations_url":"https://api.github.com/users/asvetlov/orgs","repos_url":"https://api.github.com/users/asvetlov/repos","events_url":"https://api.github.com/users/asvetlov/events{/privacy}","received_events_url":"https://api.github.com/users/asvetlov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-07-26T07:45:38Z","updated_at":"2015-09-16T17:51:29Z","closed_at":null,"body":"When I send a long task to tread-pool executor and immediately close the loop without waiting future returned by `.run_in_executor()` call I get exception report in logs:\r\n```\r\nERROR:concurrent.futures:exception calling callback for <Future at 0x7ff1ae393630 state=finished returned NoneType>\r\nTraceback (most recent call last):\r\n  File \"/usr/lib/python3.4/concurrent/futures/_base.py\", line 297, in _invoke_callbacks\r\n    callback(self)\r\n  File \"/home/andrew/projects/asyncio/asyncio/futures.py\", line 410, in <lambda>\r\n    new_future._copy_state, future))\r\n  File \"/home/andrew/projects/asyncio/asyncio/base_events.py\", line 487, in call_soon_threadsafe\r\n    handle = self._call_soon(callback, args)\r\n  File \"/home/andrew/projects/asyncio/asyncio/base_events.py\", line 461, in _call_soon\r\n    self._check_closed()\r\n  File \"/home/andrew/projects/asyncio/asyncio/base_events.py\", line 288, in _check_closed\r\n    raise RuntimeError('Event loop is closed')\r\nRuntimeError: Event loop is closed\r\n```\r\nThat's because task executed in thread has finished after loop closing. The task calls `call_soon_threadsafe()` (see `futures.wrap_future()` for details) and it fails on check for loop health.\r\n\r\nDue threaded nature it's hard to avoid situations like this: threads cannot be cancelled, we need cope with these even after loop closing. That's pretty common situation, especially for client-side jobs like web page fetching: DNS resolver is executed in thread pool also.\r\n\r\nI see two options:\r\n\r\n1.  Ignore loop closing in `wrap_future` call, skip `call_soon_threadsafe()` for closed loops\r\n2. Do the same but only if special boolean flag passed into `run_in_executor()` (while I don't see situations when I don't like the behavior, maybe flag should be `True` by default)."},"comment":{"url":"https://api.github.com/repos/python/asyncio/issues/comments/140818964","html_url":"https://github.com/python/asyncio/issues/258#issuecomment-140818964","issue_url":"https://api.github.com/repos/python/asyncio/issues/258","id":140818964,"user":{"login":"asvetlov","id":356399,"avatar_url":"https://avatars.githubusercontent.com/u/356399?v=3","gravatar_id":"","url":"https://api.github.com/users/asvetlov","html_url":"https://github.com/asvetlov","followers_url":"https://api.github.com/users/asvetlov/followers","following_url":"https://api.github.com/users/asvetlov/following{/other_user}","gists_url":"https://api.github.com/users/asvetlov/gists{/gist_id}","starred_url":"https://api.github.com/users/asvetlov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asvetlov/subscriptions","organizations_url":"https://api.github.com/users/asvetlov/orgs","repos_url":"https://api.github.com/users/asvetlov/repos","events_url":"https://api.github.com/users/asvetlov/events{/privacy}","received_events_url":"https://api.github.com/users/asvetlov/received_events","type":"User","site_admin":false},"created_at":"2015-09-16T17:51:29Z","updated_at":"2015-09-16T17:51:29Z","body":"See http://stackoverflow.com/questions/32598231/asyncio-runtimeerror-event-loop-is-closed/32615276 for yet another example for the problem"}}