Title: Crash Due to Exception in threading._shutdown() · Issue #113148 · python/cpython · GitHub
Open Graph Title: Crash Due to Exception in threading._shutdown() · Issue #113148 · python/cpython
X Title: Crash Due to Exception in threading._shutdown() · Issue #113148 · python/cpython
Description: One of the first thing that happens during interpreter finalization is waiting for all non-daemon threads to finish. This is implemented by calling threading._shutdown(). If an exception is raised there (e.g. in a threading "atexit" func...
Open Graph Description: One of the first thing that happens during interpreter finalization is waiting for all non-daemon threads to finish. This is implemented by calling threading._shutdown(). If an exception is raised ...
X Description: One of the first thing that happens during interpreter finalization is waiting for all non-daemon threads to finish. This is implemented by calling threading._shutdown(). If an exception is raised ...
Opengraph URL: https://github.com/python/cpython/issues/113148
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Crash Due to Exception in threading._shutdown()","articleBody":"One of the first thing that happens during interpreter finalization is waiting for all non-daemon threads to finish. This is implemented by calling `threading._shutdown()`. If an exception is raised there (e.g. in a `threading` \"atexit\" function), it gets ignored and we end up with non-daemon threads still running. In subinterpreters that results in a fatal error almost immediately after, since we make sure there's only one thread state left at that point.\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eReproducer:\u003c/summary\u003e\r\n\r\n```\r\n$ cat \u003e /tmp/crash-interp-lingering-thread.py \u003c\u003c EOF\r\nimport threading\r\nfrom test.support import interpreters\r\n\r\ninterp = interpreters.create()\r\ninterp.exec_sync(f\"\"\"if True:\r\n import threading\r\n import time\r\n\r\n done = False\r\n\r\n def notify_fini():\r\n global done\r\n done = True\r\n raise Exception # \u003c-------\r\n t.join()\r\n threading._register_atexit(notify_fini)\r\n\r\n def task():\r\n while not done:\r\n time.sleep(0.1)\r\n t = threading.Thread(target=task)\r\n t.start()\r\n\"\"\")\r\ninterp.close()\r\n\r\n\r\nEOF\r\n$ ./python /tmp/crash-interp-lingering-thread.py\r\nException ignored on threading shutdown:\r\nTraceback (most recent call last):\r\n File \"./cpython/Lib/threading.py\", line 1623, in _shutdown\r\n atexit_call()\r\n File \"\u003cstring\u003e\", line 10, in notify_fini\r\nException:\r\nFatal Python error: Py_EndInterpreter: not the last thread\r\nPython runtime state: initialized\r\n\r\nCurrent thread 0x00007fc8bf76a100 (most recent call first):\r\n \u003cno Python frame\u003e\r\n\r\nThread 0x00007fc8bd3d5700 (most recent call first):\r\n File \"\u003cstring\u003e\", line 16 in task\r\n File \"./cpython/Lib/threading.py\", line 1020 in run\r\n File \"./cpython/Lib/threading.py\", line 1083 in _bootstrap_inner\r\n File \"./cpython/Lib/threading.py\", line 1040 in _bootstrap\r\nAborted (core dumped)\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\n----\r\n\r\nThe solution? Make sure `threading._shutdown()` finishes its fundamental job: stop all non-daemon threads and wait for them to finish. At the very least, this means ignoring exceptions from functions registered with `threading._register_atexit()`.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-130594\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ericsnowcurrently","@type":"Person","name":"ericsnowcurrently"},"datePublished":"2023-12-14T21:45:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/113148/cpython/issues/113148"}
| 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:6fbf45c7-f1a3-3e51-bc8e-7b46799dfdc6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A77E:3499D7:28F21A:37EDF9:6A568CC8 |
| html-safe-nonce | e4cbc350bbf2f81755fed50d87db995de7d889781c19e0f285e69bfecbaefafc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzdFOjM0OTlENzoyOEYyMUE6MzdFREY5OjZBNTY4Q0M4IiwidmlzaXRvcl9pZCI6IjgxMzU2MTQ1MjA4NzYyNDAwNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 162374fd9809873adf3569af46fbd61e14feacfcaca4a0a72eed55ed7279fc95 |
| hovercard-subject-tag | issue:2042554053 |
| 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/python/cpython/113148/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2294c7de9ff31618d3c6391f34bc69884c528c9b6b6a38def6a534a1bed316e5/python/cpython/issues/113148 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2294c7de9ff31618d3c6391f34bc69884c528c9b6b6a38def6a534a1bed316e5/python/cpython/issues/113148 |
| og:image:alt | One of the first thing that happens during interpreter finalization is waiting for all non-daemon threads to finish. This is implemented by calling threading._shutdown(). If an exception is raised ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ericsnowcurrently |
| hostname | github.com |
| expected-hostname | github.com |
| None | 8f21be8503ba8fe6154df415b855c03af52eae3f285bd6ae1fef041dc053da38 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | 78af172a48a2056810bb627ffa684003e4f9a5ca |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width