Title: Extension Module Handles Are Never Closed · Issue #114538 · python/cpython · GitHub
Open Graph Title: Extension Module Handles Are Never Closed · Issue #114538 · python/cpython
X Title: Extension Module Handles Are Never Closed · Issue #114538 · python/cpython
Description: We load extension modules by loading the shared library file (dlopen(), LoadLibraryExW()). However we never unload/release it. In each case we get a handle when load it, but we throw the handle away. We should consider holding on to the ...
Open Graph Description: We load extension modules by loading the shared library file (dlopen(), LoadLibraryExW()). However we never unload/release it. In each case we get a handle when load it, but we throw the handle awa...
X Description: We load extension modules by loading the shared library file (dlopen(), LoadLibraryExW()). However we never unload/release it. In each case we get a handle when load it, but we throw the handle awa...
Opengraph URL: https://github.com/python/cpython/issues/114538
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Extension Module Handles Are Never Closed","articleBody":"We load extension modules by loading the shared library file (`dlopen()`, `LoadLibraryExW()`). However we never unload/release it. In each case we get a handle when load it, but we throw the handle away. We should consider holding on to the handle (maybe in something like `PyModuleObject.md_handle`).\r\n\r\nIf we save the handle, we could use it to release the loaded shared library (`dlclose()`, `FreeLibrary()`). `module_dealloc()` would probably be a good place to do so. In every case. releasing it drops an internal refcount, and when it reaches 0 the lib is actually unloaded.\r\n\r\nAside from not leaking, there's another real benefit. Extension modules can (and often do, e.g. static types) store objects in C global variables. Single-phase init modules do not have an opportunity to clean up after themselves (which was part of the motivation for PEP 489). So they don't have a chance to decref any objects they may have stashed away in globals. This is problematic when such stashed objects survive runtime init/fini cycles. [^1] Closing the lib means all the extensions C globals are discarded and thus importing the module in a later cycle will result in a clean set of globals. That may mean a few objects get leaked, but that's better than segfaulting.\r\n\r\n[^1]: The same goes for when an extension is loaded for the first time in an isolated (per-interpreter GIL) subinterpreter rather than in the main interpreter, since the C globals will outlive the interpreter. However, there are some caveats with that too. Also, though single-phase init modules are not supported in isolated interpreters, their init functions still get run.\r\n\r\nThere are some complexities though. Using `dlclose()`, etc. must be done carefully. While not critical, it's best to do it in the thread where the \"open\" was done. Also, once the lib is closed, trying to use any variables or functions defined in it will segfault. That means you can't do it if there's any thread running C code from the module (incl. daemon threads). Likewise if the module registered a callback somewhere or if a reference to one of its static types (or other global objects) is held outside the module. That may even impact GC.\r\n\r\nWe might consider at least closing the lib if `_PyImport_CheckSubinterpIncompatibleExtensionAllowed()` fails in `_PyImport_LoadDynamicModuleWithSpec()`. The risk there is smaller.\r\n\r\nFWIW, in practice this isn't a big concern because a loaded extension module typically doesn't get destroyed until not long before the process terminates (though it might be a different story for embedders). I'm bringing it up because it something I noticed and looked into and thought we might at least consider the problem.","author":{"url":"https://github.com/ericsnowcurrently","@type":"Person","name":"ericsnowcurrently"},"datePublished":"2024-01-24T22:11:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/114538/cpython/issues/114538"}
| 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:1c362325-8e66-94e0-8657-2a81c94984a1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB52:139CD4:A7BDF:D836B:696B319A |
| html-safe-nonce | c44d58f2534ff08c0048ff820c146a1370edac6c79ea2b1146f29d245602f122 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjUyOjEzOUNENDpBN0JERjpEODM2Qjo2OTZCMzE5QSIsInZpc2l0b3JfaWQiOiIyOTA1MjY2MjE3NTIzMjk0NjE4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | bc094f398d28222465ed847bbd0a5bac5c7c933c20aacaba14c77d47b35d5f4d |
| hovercard-subject-tag | issue:2099200436 |
| 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/114538/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5199696a6738d9e84c8b704b5728a8ce59acc9287f7c82eba410ea9f32448b00/python/cpython/issues/114538 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5199696a6738d9e84c8b704b5728a8ce59acc9287f7c82eba410ea9f32448b00/python/cpython/issues/114538 |
| og:image:alt | We load extension modules by loading the shared library file (dlopen(), LoadLibraryExW()). However we never unload/release it. In each case we get a handle when load it, but we throw the handle awa... |
| 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 | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width