René's URL Explorer Experiment


Title: The Word "Finalizing" in C-API Function Names is Sometimes Misleading · Issue #110490 · python/cpython · GitHub

Open Graph Title: The Word "Finalizing" in C-API Function Names is Sometimes Misleading · Issue #110490 · python/cpython

X Title: The Word "Finalizing" in C-API Function Names is Sometimes Misleading · Issue #110490 · python/cpython

Description: (This is something I've been thinking about in the last few weeks and has before more significant as we've added Py_IsFinalizing() to the 3.13 public C-API1.) tl;dr I think that either we should make the Py_IsFinalizing() behavior match ...

Open Graph Description: (This is something I've been thinking about in the last few weeks and has before more significant as we've added Py_IsFinalizing() to the 3.13 public C-API1.) tl;dr I think that either we should ma...

X Description: (This is something I've been thinking about in the last few weeks and has before more significant as we've added Py_IsFinalizing() to the 3.13 public C-API1.) tl;dr I think that either we s...

Opengraph URL: https://github.com/python/cpython/issues/110490

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"The Word \"Finalizing\" in C-API Function Names is Sometimes Misleading","articleBody":"(This is something I've been thinking about in the last few weeks and has before more significant as we've added `Py_IsFinalizing()` to the 3.13 public C-API[^1].)\r\n\r\n[^1]: ...and possibly to the stable ABI.  See gh-110441.\r\n\r\ntl;dr I think that either we should make the `Py_IsFinalizing()` behavior match the name more closely or we should change the name.  Internally we should think in terms of what runtime capabilities are available rather than just \"is finalizing\".\r\n\r\n----\r\n\r\nAs of recently, we have `Py_IsFinalizing()` in the public C-API, along with various similarly named (`.*[Ff]inalizing.*`) functions and runtime state fields in the internal API. [^2]  In addition, `sys.is_finalizing()` has been around since 3.5[^3].\r\n\r\n[^2]: We also have the fundamentally related `Py_Finalize()` and `Py_FinalizeEx()`, which have been with us for a long, long time.\r\n[^3]: `sys.finalizing()` is a thin wrapper around `Py_IsFinalizing()`.  Before 3.13, it wrapped `_Py_IsFinalizing()`.  Before 3.7, it wrapped `_Py_Finalizing`.\r\n\r\nThey all serve to track/inform when the current runtime/interpreter is shutting down and therefore might not be in a stable state (leading to crashes).  The intention is clear in the docs for [`Py_IsFinalizing()`](https://docs.python.org/3.13/c-api/init.html#c.Py_IsFinalizing):\r\n\r\n```\r\nint Py_IsFinalizing()\r\n    Return true (non-zero) if the main Python interpreter is shutting down. Return false (zero) otherwise.\r\n```\r\n\r\nInternally, we use that status to determine the availability of certain capabilities, most notably threading.  In fact, the whole family of functions and state started in 2011 as part of the effort to deal with daemon threads during shutdown.\r\n\r\nThis has lead to a bit of a conflict in the meaning of \"finalizing\".  IMHO, it would be worth sorting out the discrepancy.  I expect this will include either changing the behavior of `Py_IsFinalizing()` or changing the name of the function to match what it is actually reporting.\r\n\r\n(Is this a critical issue?  No.  The matter at hand is fundamentally related to daemon threads. 🤮)\r\n(Is it worth thinking through anyway?  Yes.  I expect the discussion would help bring more clarity to runtime finalization and to the runtime in general.)\r\n\r\n\r\n## Context\r\n\r\nFinalization is started for the runtime by `Py_Finalize()` and `Py_FinalizeEx()`.  For an interpreter we use `Py_EndInterpreter()`.  (For thread states we don't have a specific API, but the closest is `PyThreadState_Clear()` + `PyThreadState_Delete()`.)\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eOnce finalization has begun, we keep track of that fact in a number of places:\u003c/summary\u003e\r\n\r\n* `_PyRuntimeState._finalizing`\r\n* `_PyRuntimeState._finalizing_id`\r\n* `PyInterpreterState.finalizing`\r\n* `PyInterpreterState._finalizing`\r\n* `PyInterpreterState._finalizing_id`\r\n* `PyThreadState._status.finalizing`\r\n\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eWe report it through a various API:\u003c/summary\u003e\r\n\r\n* `Py_IsFinalizing()`  [3.13] (docs: \"Return true (non-zero) if the main Python interpreter is shutting down. Return false (zero) otherwise.\")\r\n* `sys.is_finalizing()`  [3.5] (docs: \"Return True if the Python interpreter is shutting down, False otherwise.\")\r\n* `_PyRuntimeState_GetFinalizing()`\r\n* `_PyRuntimeState_GetFinalizingID()`\r\n* `_PyInterpreterState_GetFinalizing()`\r\n* `_PyInterpreterState_GetFinalizingID()`\r\n\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eHere's a timeline:\u003c/summary\u003e\r\n\r\n* [2008, ~3.0] [bpo-1856](https://bugs.python.org/issue1856) (AKA [gh-46164](https://github.com/python/cpython/issues/46164)) opened about crashes with daemon threads during runtime finalization\r\n* [2011, 3.2] added `_Py_Finalizing` as part of the effort to solve bpo-1856\r\n* [3.5] added `sys.is_finalizing()`, a light wrapper around `_Py_Finalizing`\r\n* [3.7] replaced `_Py_Finalizing` with `_PyRuntimeState.finalizing`; added `_Py_IsFinalizing()` (in \"public\" C-API)\r\n* [2019] ([gh-80608](https://github.com/python/cpython/issues/80608)) `_Py_Finalizing()` recommended for use in docs for [`PyEval_RestoreThread()`](https://docs.python.org/3.13/c-api/init.html#c.PyEval_RestoreThread)\r\n* [2020, 3.9] added `_Py_GetFinalizing()`; renamed `_PyRuntimeState.finalizing` to `_PyRuntimeState._finalizing` (and made it atomic)\r\n* [July 2023, 3.13] ([gh-106400](https://github.com/python/cpython/pull/106400)) moved `_Py_IsFinalizing()` to the internal C-API\r\n* [Aug. 2023, 3.13] ([gh-108014](https://github.com/python/cpython/issues/108014)/[gh-108032](https://github.com/python/cpython/pull/108032))  added `_Py_IsFinalizing()` back to the public C-API as `Py_IsFinalizing()`\r\n* [this week] ([gh-110397](https://github.com/python/cpython/issues/110397)/[gh-110441](https://github.com/python/cpython/pull/110441)) discussion about adding `Py_IsFinalizing()` to the stable ABI\r\n\r\n\u003c/details\u003e\r\n\r\n\r\n## The Problem\r\n\r\nThe name `Py_IsFinalizing` (or `sys.is_finalizing`[^2]) implies that the function tells you if the runtime is shutting down and will soon be unavailable.  In fact, the docs actually say this.  However, that explanation is only *mostly* accurate.  When the function returns true, that is completely correct, but the same is not *completely* correct when it returns false.  So, where might returning false be incorrect?\r\n\r\nCurrently, there is actually a meaningful space of time between when `Py_Finalize()` is called and when `Py_IsFinalizing()` returns true.  In that time we do a number of things, like wait for all non-daemon threads to stop and run all global atexit hooks.  Essentially, `Py_IsFinalizing()` returns true only at the point in finalization where other threads (i.e. not the current/main thread) should no longer rely on a stable runtime or C-API.  (Perhaps the function should be called something like `Py_AreThreadsAllowed()` instead.)\r\n\r\nTracking that point in time is important for how we handle daemon threads during shutdown.  We shouldn't change that.\r\n\r\nTo resolve any confusion and ambiguity here, we should instead:\r\n\r\n* explicitly acknowledge what we're actually tracking (in name and description)\r\n* decide what we care about internally (and why)\r\n* determine what information users actually want\r\n\r\nInternally we care about two things: whether or not the runtime currently supports multithreading and whether or not the runtime (or current interpreter) is reaching the end of its life.\r\n\r\nFor users of `PyEval_RestoreThread()`, they want to know if the current thread will be terminated if they call that function to re-acquire the GIL.  That matters for extensions running in daemon threads, and probably for some embedders.  I'm not sure they care about whether or not Python is finalizing specifically.\r\n\r\nWhat about other users?  I don't know why they might want to know if the runtime is finalizing.  It would certainly only be of interest for daemon threads (and for embedded applications).\r\n\r\n\r\n## What To Do About It?\r\n\r\nIt makes sense to figure out what folks actually care about when it comes to the concept of \"finalizing\".  It would likewise make sense to ensure names and descriptions actually match what functions do (and state is for).\r\n\r\nIn the specific case of `Py_IsFinalizing()`, I see a couple options:\r\n\r\n1. change it to track the moment `Py_Finalize()` starts, before it actually does anything, so \"finalizing\" is 100% accurate\r\n2. change the name to match what it actually tracks: whether or not other threads can count on a stable runtime\r\n\r\n(Likewise for `sys.is_finalizing()`, and most internal API.)\r\n\r\nHere are some tricky things to consider:\r\n\r\n* we want to disable some capabilities as soon as possible during shutdown (e.g. disallow creating new threads *before* waiting for existing non-daemon threads to finish)\r\n* other threads are welcome to keep using the full runtime, even after shutdown begins, as long as we haven't started cleaning up state (which would then cause crashes) [^4]\r\n* in the main thread (executing `Py_Finalize()`), extension modules can (for now?) still run the risk of accessing invalid state *after* the point `Py_IsFinalizing()` returns true, up to the point that the extension module is cleaned up (see `finalize_modules()` in pylifecycle.c) and perhaps even after that\r\n\r\n[^4]: This is the fundamental basis of the original \"finalizing\" status we introduced back in 2011.\r\nThe one tricky thing is that we don't want","author":{"url":"https://github.com/ericsnowcurrently","@type":"Person","name":"ericsnowcurrently"},"datePublished":"2023-10-06T22:35:46.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/110490/cpython/issues/110490"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:8d38de6f-9b48-37d0-7bb8-97e9411b81a9
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA86A:323DD4:260A8E6:33B5616:696ADB5B
html-safe-nonce787738ee8373d3b550443931918943c2f696e16d92f3e7694be4c1f4f906f191
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODZBOjMyM0RENDoyNjBBOEU2OjMzQjU2MTY6Njk2QURCNUIiLCJ2aXNpdG9yX2lkIjoiOTA4OTcyMzcyNDUwMjU4ODI1MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacf7ec18875fcdcb4a064d0745076ac1b773a9f5b892486d409bbbdc9e0d0e99f4
hovercard-subject-tagissue:1931009625
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/110490/issue_layout
twitter:imagehttps://opengraph.githubassets.com/aedeb4b313790417cd730d864f9ca07b2c3f84abf4c251e14f53ac77177efcc3/python/cpython/issues/110490
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/aedeb4b313790417cd730d864f9ca07b2c3f84abf4c251e14f53ac77177efcc3/python/cpython/issues/110490
og:image:alt(This is something I've been thinking about in the last few weeks and has before more significant as we've added Py_IsFinalizing() to the 3.13 public C-API1.) tl;dr I think that either we should ma...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameericsnowcurrently
hostnamegithub.com
expected-hostnamegithub.com
None5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release524a93f2c1f36522a3b4be4c110467ee4172245d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/110490#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F110490
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F110490
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/110490
Reloadhttps://github.com/python/cpython/issues/110490
Reloadhttps://github.com/python/cpython/issues/110490
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/110490
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/110490
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/110490
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/110490
The Word "Finalizing" in C-API Function Names is Sometimes Misleadinghttps://github.com/python/cpython/issues/110490#top
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
https://github.com/ericsnowcurrently
https://github.com/ericsnowcurrently
ericsnowcurrentlyhttps://github.com/ericsnowcurrently
on Oct 6, 2023https://github.com/python/cpython/issues/110490#issue-1931009625
1https://github.com/python/cpython/issues/110490#user-content-fn-1-196d0f43ccf232a0ed09b27130d26f1c
2https://github.com/python/cpython/issues/110490#user-content-fn-2-196d0f43ccf232a0ed09b27130d26f1c
3https://github.com/python/cpython/issues/110490#user-content-fn-3-196d0f43ccf232a0ed09b27130d26f1c
Py_IsFinalizing()https://docs.python.org/3.13/c-api/init.html#c.Py_IsFinalizing
bpo-1856https://bugs.python.org/issue1856
gh-46164https://github.com/python/cpython/issues/46164
bpo-1856https://bugs.python.org/issue?@action=redirect&bpo=1856
gh-80608https://github.com/python/cpython/issues/80608
PyEval_RestoreThread()https://docs.python.org/3.13/c-api/init.html#c.PyEval_RestoreThread
gh-106400https://github.com/python/cpython/pull/106400
gh-108014https://github.com/python/cpython/issues/108014
gh-108032https://github.com/python/cpython/pull/108032
gh-110397https://github.com/python/cpython/issues/110397
gh-110441https://github.com/python/cpython/pull/110441
2https://github.com/python/cpython/issues/110490#user-content-fn-2-196d0f43ccf232a0ed09b27130d26f1c
4https://github.com/python/cpython/issues/110490#user-content-fn-4-196d0f43ccf232a0ed09b27130d26f1c
gh-110397: Add Py_IsFinalizing() to the stable ABI #110441https://github.com/python/cpython/pull/110441
https://github.com/python/cpython/issues/110490#user-content-fnref-1-196d0f43ccf232a0ed09b27130d26f1c
https://github.com/python/cpython/issues/110490#user-content-fnref-2-196d0f43ccf232a0ed09b27130d26f1c
↩2https://github.com/python/cpython/issues/110490#user-content-fnref-2-2-196d0f43ccf232a0ed09b27130d26f1c
https://github.com/python/cpython/issues/110490#user-content-fnref-3-196d0f43ccf232a0ed09b27130d26f1c
https://github.com/python/cpython/issues/110490#user-content-fnref-4-196d0f43ccf232a0ed09b27130d26f1c
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.