Title: Checking for `sys.is_finalizing` before thread.start() still yields "can't create new thread at interpreter shutdown" · Issue #114570 · python/cpython · GitHub
Open Graph Title: Checking for `sys.is_finalizing` before thread.start() still yields "can't create new thread at interpreter shutdown" · Issue #114570 · python/cpython
X Title: Checking for `sys.is_finalizing` before thread.start() still yields "can't create new thread at interpreter shutdown" · Issue #114570 · python/cpython
Description: Bug report Bug description: We have code in pymongo Our MongoClient object immediately spawns a new daemon thread to run some periodic check operations from the moment it is initialized. When run on python 3.12, we've run into the issue ...
Open Graph Description: Bug report Bug description: We have code in pymongo Our MongoClient object immediately spawns a new daemon thread to run some periodic check operations from the moment it is initialized. When run o...
X Description: Bug report Bug description: We have code in pymongo Our MongoClient object immediately spawns a new daemon thread to run some periodic check operations from the moment it is initialized. When run o...
Opengraph URL: https://github.com/python/cpython/issues/114570
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Checking for `sys.is_finalizing` before thread.start() still yields \"can't create new thread at interpreter shutdown\"","articleBody":"# Bug report\n\n### Bug description:\n\nWe have code in [pymongo](https://github.com/mongodb/mongo-python-driver) Our `MongoClient` object immediately spawns a new daemon thread to run some periodic check operations from the moment it is initialized. When run on python 3.12, we've run into the issue `can't create new thread at interpreter shutdown` because we have cases `thread.start()` that can execute as the interpreter is shutting down.\n\nTo mitigate this issue we attempted checking `sys.is_finalizing` before issuing the `start()` call: \n```python\nif not sys.is_finalizing():\n thread.start()\n```\nThis still generated the same runtime error. \n\nWe tried catching the `RuntimeError` exception:\n```python\ntry:\n thread.start()\nexcept RuntimeError:\n pass\n```\nThis worked. This feels too broad a solution than the above since it will catch and ignore all `RuntimeError`s. \n\nFinally we decided to see if we could check `sys.is_finalizing` after the exception is raised.\n\n```python\ntry:\n thread.start()\nexcept RuntimeError:\n if sys.is_finalizing():\n self._thread = None\n return\n raise\n```\nTo our surprise, this also failed. I can understand that starting a thread on interpreter shutdown should be avoided, but this also feels misleading if the system isn't correctly seeing--which is my assumption on what `sys.is_finalizing()`--that it is experiencing interpreter shutdown. \n\nShould the expectation not be that `sys.is_finalizing` is updated before the `thread.start()` is called OR by the time the `RuntimeError` is raised? This way we can safely catch or preempt the error before triggering?\n\nAs a general inquiry, though, what should be the general best practice for patching issues like this on our end as it has introduced somewhat breaking behavior from 3.11 -\u003e 3.12? Should we do like above with catching the `RuntimeError` but additionally check the exception string for the exact type of `RuntimeError`?\n\n### CPython versions tested on:\n\n3.12\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-115352\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/Jibola","@type":"Person","name":"Jibola"},"datePublished":"2024-01-25T22:30:16.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":11},"url":"https://github.com/114570/cpython/issues/114570"}
| 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:af79929f-85b1-1e56-9743-722596842fdc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8340:2F9DEC:192782F:23431A2:696A4CD2 |
| html-safe-nonce | 0f624cf373f28db19482287e93e63de4c6ddb5121a631bc15acf4f5a0bfea66a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MzQwOjJGOURFQzoxOTI3ODJGOjIzNDMxQTI6Njk2QTRDRDIiLCJ2aXNpdG9yX2lkIjoiMjQ5NDM4OTk5MjIxNzUyOTU1NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c78cbeca389e32e9225c5cee5a4706e6d96876dc91f02c484724dfd2b65b46bb |
| hovercard-subject-tag | issue:2101268870 |
| 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/114570/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d4587b6e382599d0a596b97c699780407d0e7e5754a7eaa79b259ae32cfd4222/python/cpython/issues/114570 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d4587b6e382599d0a596b97c699780407d0e7e5754a7eaa79b259ae32cfd4222/python/cpython/issues/114570 |
| og:image:alt | Bug report Bug description: We have code in pymongo Our MongoClient object immediately spawns a new daemon thread to run some periodic check operations from the moment it is initialized. When run o... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Jibola |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3f871c8e07f0ae1886fa8dac284166d28b09ad5bada6476fc10b674e489788ef |
| 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 | 63c426b30d262aba269ef14c40e3c817b384cd61 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width