Title: Starting new thread during finalization leads to call to `PyMem_Free` without holding the GIL · Issue #109795 · python/cpython · GitHub
Open Graph Title: Starting new thread during finalization leads to call to `PyMem_Free` without holding the GIL · Issue #109795 · python/cpython
X Title: Starting new thread during finalization leads to call to `PyMem_Free` without holding the GIL · Issue #109795 · python/cpython
Description: Crash report What happened? Since #109135, thread_run checks if it is called during finalization, and in this case it frees thread bootstate in thread_bootstate_free and returns. The problem is that PyMem_Free shouldn't be called if GIL ...
Open Graph Description: Crash report What happened? Since #109135, thread_run checks if it is called during finalization, and in this case it frees thread bootstate in thread_bootstate_free and returns. The problem is tha...
X Description: Crash report What happened? Since #109135, thread_run checks if it is called during finalization, and in this case it frees thread bootstate in thread_bootstate_free and returns. The problem is tha...
Opengraph URL: https://github.com/python/cpython/issues/109795
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Starting new thread during finalization leads to call to `PyMem_Free` without holding the GIL","articleBody":"# Crash report\n\n### What happened?\n\nSince #109135, `thread_run` checks if it is called during finalization, and in this case it frees thread bootstate in `thread_bootstate_free` and returns. The problem is that `PyMem_Free` shouldn't be called if GIL is not held.\n\nRepro and error message (just in case):\n```python\nimport os\nimport random\nimport signal\nimport subprocess\nimport sys\nimport time\n\nscript = \"\"\"\nimport threading\nwhile True:\n t = threading.Thread()\n t.start()\n t.join()\n\"\"\"\n\nwhile True:\n p = subprocess.Popen([sys.executable, '-c', script], stderr=subprocess.PIPE)\n time.sleep(random.random())\n os.kill(p.pid, signal.SIGINT)\n _, err = p.communicate()\n if p.returncode == -signal.SIGABRT:\n print(err.decode('utf-8'))\n break\n```\n\n```\nTraceback (most recent call last):\n File \"\u003cstring\u003e\", line 5, in \u003cmodule\u003e\n File \"/home/radislav/projects/cpython/Lib/threading.py\", line 983, in start\n self._started.wait()\n File \"/home/radislav/projects/cpython/Lib/threading.py\", line 641, in wait\n signaled = self._cond.wait(timeout)\n ^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/radislav/projects/cpython/Lib/threading.py\", line 341, in wait\n waiter.acquire()\nKeyboardInterrupt\nFatal Python error: _PyMem_DebugFree: Python memory allocator called without holding the GIL\nPython runtime state: finalizing (tstate=0x0000561b61b67338)\n\nThread 0x00007f8f890b8280 (most recent call first):\n \u003cno Python frame\u003e\n```\n\ncc @vstinner\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.13.0a0 (heads/main:3e8fcb7df7, Sep 23 2023, 01:43:45) [GCC 10.2.1 20210110]\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-109808\n* gh-109852\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/chgnrdv","@type":"Person","name":"chgnrdv"},"datePublished":"2023-09-23T21:12:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/109795/cpython/issues/109795"}
| 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:a21c571b-0938-ebbc-9d84-5e0ab5d20e24 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C50:3C7D7F:12B35C0:19C0691:696AA02E |
| html-safe-nonce | 542e350ee37ff70d6dab5dfc310707e7b7a0e6d5fa43c05b2af7371c1e8d26f7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzUwOjNDN0Q3RjoxMkIzNUMwOjE5QzA2OTE6Njk2QUEwMkUiLCJ2aXNpdG9yX2lkIjoiMjM1NDE1OTE5NDAzOTAzMzkwMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 833e6417ce39d5ed0adb115eb0ffa9fcf11361271a805b5f09d60f99420365b4 |
| hovercard-subject-tag | issue:1910001884 |
| 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/109795/issue_layout |
| twitter:image | https://opengraph.githubassets.com/556017e5ac936ffda04bd06e451ea158fba31e3b0b21f70fce62a09ac0fabe6d/python/cpython/issues/109795 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/556017e5ac936ffda04bd06e451ea158fba31e3b0b21f70fce62a09ac0fabe6d/python/cpython/issues/109795 |
| og:image:alt | Crash report What happened? Since #109135, thread_run checks if it is called during finalization, and in this case it frees thread bootstate in thread_bootstate_free and returns. The problem is tha... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chgnrdv |
| hostname | github.com |
| expected-hostname | github.com |
| None | a51f97dbb9326f71c08ecb61577457d543c602124d1a2672871258ef37ac5261 |
| 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 | 4bd0eac606c70914085176ef312ebdcd97a8cdf1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width