Title: Segmentation fault while calling loop.create_future in finalizer (__del__) when a custom sys.excepthook is used · Issue #143 · MagicStack/uvloop · GitHub
Open Graph Title: Segmentation fault while calling loop.create_future in finalizer (__del__) when a custom sys.excepthook is used · Issue #143 · MagicStack/uvloop
X Title: Segmentation fault while calling loop.create_future in finalizer (__del__) when a custom sys.excepthook is used · Issue #143 · MagicStack/uvloop
Description: uvloop version: 0.9.1 Python version: 3.6.5rc1 Platform: linux Can you reproduce the bug with PYTHONASYNCIODEBUG in env?: yes, in fact, it is reproducible only when the asyncio debug is enabled When using uvloop and having a custom sys.e...
Open Graph Description: uvloop version: 0.9.1 Python version: 3.6.5rc1 Platform: linux Can you reproduce the bug with PYTHONASYNCIODEBUG in env?: yes, in fact, it is reproducible only when the asyncio debug is enabled Whe...
X Description: uvloop version: 0.9.1 Python version: 3.6.5rc1 Platform: linux Can you reproduce the bug with PYTHONASYNCIODEBUG in env?: yes, in fact, it is reproducible only when the asyncio debug is enabled Whe...
Opengraph URL: https://github.com/MagicStack/uvloop/issues/143
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segmentation fault while calling loop.create_future in finalizer (__del__) when a custom sys.excepthook is used","articleBody":"* **uvloop version**: 0.9.1\r\n* **Python version**: 3.6.5rc1\r\n* **Platform**: linux\r\n* **Can you reproduce the bug with `PYTHONASYNCIODEBUG` in env?**: yes, in fact, it is reproducible *only* when the asyncio debug is enabled\r\n\r\nWhen using uvloop and having a custom `sys.excepthook` which uses a variable from the outer scope, which has a finalizer (the `__del__` method) that creates a new future using `AbstractEventLoop.create_future()` method, Python interpreter crashes with a segmentation fault.\r\n\r\nExample code:\r\n```python\r\n#!/usr/bin/env python3\r\nimport asyncio\r\nimport sys\r\n\r\nimport uvloop\r\n\r\nclass LoopWrapper():\r\n def __init__(self, loop):\r\n self.loop = loop\r\n\r\n def __del__(self):\r\n print('finalizing', self.loop)\r\n self.loop.create_future() # assertion fails here\r\n\r\n\r\nasync def store_event_loop(loop):\r\n wrapped_loop = LoopWrapper(loop)\r\n\r\n def custom_excepthook(*exc_info):\r\n print('unhandled_exception', exc_info)\r\n # commenting the below line causes the failed assertion to disappear\r\n print(wrapped_loop)\r\n\r\n sys.excepthook = custom_excepthook\r\n\r\n\r\nif __name__ == '__main__':\r\n # commenting the below line causes the failed assertion to disappear\r\n asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())\r\n\r\n event_loop = asyncio.get_event_loop()\r\n # commenting the below line disables the assertions\r\n event_loop.set_debug(True)\r\n\r\n event_loop.run_until_complete(store_event_loop(event_loop))\r\n```\r\n\r\nWhen run, the above example code fails like this:\r\n```\r\n$ ./uvloop_test.py \r\nfinalizing \u003cuvloop.Loop running=False closed=False debug=True\u003e\r\nSegmentation fault (core dumped)\r\n```\r\n\r\nWhen uvloop is not used, the code fails at this `AttributeError`:\r\n```\r\nTraceback (most recent call last):\r\n File \"./uvloop_test.py\", line 13, in __del__\r\n File \"/usr/lib/python3.6/asyncio/base_events.py\", line 276, in create_future\r\nAttributeError: 'NoneType' object has no attribute 'Future'\r\n```\r\n\r\nWhen a Python interpreter compiled with `--with-pydebug` option is used, it crashes on the following assertion failure:\r\n\r\n```\r\npython3: ../Objects/abstract.c:2300: _PyObject_FastCallDict: Assertion `func != NULL' failed.\r\n```\r\n\r\nGDB backtrace (`py-bt`) shows this:\r\n\r\n```\r\n(gdb) py-bt\r\nTraceback (most recent call first):\r\n \u003cbuilt-in method create_future of Loop object at remote 0x7f7ecfafe048\u003e\r\n File \"./uvloop_test.py\", line 13, in __del__\r\n self.loop.create_future() # assertion fails here\r\n```\r\n\r\nThe issue here is that uvloop causes Python interpreter to crash. I believe that a regular Python exception should be raised instead.\r\n\r\nApart from addressing this issue, if someone can explain the details of what is happening in the example code mentioned above, in particular why it fails even without uvloop and why a finalizer triggered from a custom `sys.excepthook` is not able to call `create_future()`, it would also be helpful.\r\n\r\nThank you.","author":{"url":"https://github.com/pbasista","@type":"Person","name":"pbasista"},"datePublished":"2018-05-16T10:52:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/143/uvloop/issues/143"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:325c927b-aa43-9b67-2a4e-08092363d9f5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 84FA:328262:1CC682:27B0DA:6A5A4529 |
| html-safe-nonce | 536b0f4d98d89320b92ef8ab7c55cafed11564dbebbbf635c85f632339949e41 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NEZBOjMyODI2MjoxQ0M2ODI6MjdCMERBOjZBNUE0NTI5IiwidmlzaXRvcl9pZCI6IjI0OTQwMjE0MzI0OTk2NTE4ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1dc2d60a9effd4a85ed1a4564a977b9dc0995878590ac8fab9991ad795e69bde |
| hovercard-subject-tag | issue:323568942 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/MagicStack/uvloop/143/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fc1381bd0ae2eb7a0d124a91f654180773947e7012d9858690784e06d8806e88/MagicStack/uvloop/issues/143 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fc1381bd0ae2eb7a0d124a91f654180773947e7012d9858690784e06d8806e88/MagicStack/uvloop/issues/143 |
| og:image:alt | uvloop version: 0.9.1 Python version: 3.6.5rc1 Platform: linux Can you reproduce the bug with PYTHONASYNCIODEBUG in env?: yes, in fact, it is reproducible only when the asyncio debug is enabled Whe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pbasista |
| hostname | github.com |
| expected-hostname | github.com |
| None | a793cd1fa898929111edad5857ee1f24ecf4bf7702525e67357cabc9df136fc3 |
| turbo-cache-control | no-preview |
| go-import | github.com/MagicStack/uvloop git https://github.com/MagicStack/uvloop.git |
| octolytics-dimension-user_id | 14324950 |
| octolytics-dimension-user_login | MagicStack |
| octolytics-dimension-repository_id | 45766550 |
| octolytics-dimension-repository_nwo | MagicStack/uvloop |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 45766550 |
| octolytics-dimension-repository_network_root_nwo | MagicStack/uvloop |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5aec27bb9c4f7ebe5fbbcd1533a5509e63a570a1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width