Title: Race condition when importing `collections.abc` · Issue #125245 · python/cpython · GitHub
Open Graph Title: Race condition when importing `collections.abc` · Issue #125245 · python/cpython
X Title: Race condition when importing `collections.abc` · Issue #125245 · python/cpython
Description: Bug report Bug description: Discovered alongside #125243 with similar steps to reproduce. I don't have a simpler way to trigger this than "run the PyO3 tests in a loop" because I think it requires many threads accessing the python runtim...
Open Graph Description: Bug report Bug description: Discovered alongside #125243 with similar steps to reproduce. I don't have a simpler way to trigger this than "run the PyO3 tests in a loop" because I think it requires ...
X Description: Bug report Bug description: Discovered alongside #125243 with similar steps to reproduce. I don't have a simpler way to trigger this than "run the PyO3 tests in a loop" because I thin...
Opengraph URL: https://github.com/python/cpython/issues/125245
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Race condition when importing `collections.abc`","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nDiscovered alongside #125243 with similar steps to reproduce. I don't have a simpler way to trigger this than \"run the PyO3 tests in a loop\" because I think it requires many threads accessing the python runtime simulatenously.\r\n\r\nTo trigger it, have a rust toolchain and Python installed, clone the PyO3 repo and execute the following command:\r\n\r\n```\r\nwhile RUST_BACKTRACE=1 UNSAFE_PYO3_BUILD_FREE_THREADED=1 cargo test --lib --features=full -- --test-threads=100; do :; done\r\n```\r\n\r\nYou may also hit some other test failures related to ZoneInfo, see the other issue I opened about that.\r\n\r\nYou will eventually see a test failure with the following text:\r\n\r\n```\r\n---- exceptions::socket::tests::gaierror stdout ----\r\nthread 'exceptions::socket::tests::gaierror' panicked at src/impl_/exceptions.rs:26:17:\r\nfailed to import exception socket.gaierror: ImportError: cannot import name 'Mapping' from 'collections.abc' (/Users/goldbaum/.pyenv/versions/3.13.0t/lib/python3.13t/collections/abc.py)\r\n```\r\n\r\nI slightly modified PyO3 to get a traceback as well (hidden because it's a distractingly long diff):\r\n\r\n\r\n\u003cdetails\u003e\r\n\r\n```\r\ndiff --git a/src/impl_/exceptions.rs b/src/impl_/exceptions.rs\r\nindex 15b6f53b..de63ad59 100644\r\n--- a/src/impl_/exceptions.rs\r\n+++ b/src/impl_/exceptions.rs\r\n@@ -1,4 +1,8 @@\r\n-use crate::{sync::GILOnceCell, types::PyType, Bound, Py, Python};\r\n+use crate::{\r\n+ sync::GILOnceCell,\r\n+ types::{PyTracebackMethods, PyType},\r\n+ Bound, Py, Python,\r\n+};\r\n\r\n pub struct ImportedExceptionTypeObject {\r\n imported_value: GILOnceCell\u003cPy\u003cPyType\u003e\u003e,\r\n@@ -20,8 +24,11 @@ impl ImportedExceptionTypeObject {\r\n .import(py, self.module, self.name)\r\n .unwrap_or_else(|e| {\r\n panic!(\r\n- \"failed to import exception {}.{}: {}\",\r\n- self.module, self.name, e\r\n+ \"failed to import exception {}.{}: {}\\n{}\",\r\n+ self.module,\r\n+ self.name,\r\n+ e,\r\n+ e.traceback(py).unwrap().format().unwrap(),\r\n )\r\n })\r\n }\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\nAnd the traceback is:\r\n\r\n```\r\nTraceback (most recent call last):\r\n File \"/Users/goldbaum/.pyenv/versions/3.13.0t/lib/python3.13t/socket.py\", line 55, in \u003cmodule\u003e\r\n import os, sys, io, selectors\r\n File \"/Users/goldbaum/.pyenv/versions/3.13.0t/lib/python3.13t/selectors.py\", line 10, in \u003cmodule\u003e\r\n from collections.abc import Mapping\r\n```\r\n\r\nSo somehow during setup of the `socket` module, `Mapping` isn't available yet, but only if many threads are simultaneously touching the Python runtime.\r\n\r\n(ping @davidhewitt, we probably want to disable the socket error tests on the free-threaded build as well)\r\n\r\n### CPython versions tested on:\r\n\r\n3.13\r\n\r\n### Operating systems tested on:\r\n\r\nmacOS\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-125415\n* gh-125944\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ngoldbaum","@type":"Person","name":"ngoldbaum"},"datePublished":"2024-10-10T10:01:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/125245/cpython/issues/125245"}
| 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:b4e0e7ee-fe37-9e37-ec9a-1b5ccdc33e53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A46:3651F6:1E666CB:27BAEE0:696B4244 |
| html-safe-nonce | 78648cd61c569cf371371e69df690ce673e898f73f74bc3d0ee18be18b27272b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTQ2OjM2NTFGNjoxRTY2NkNCOjI3QkFFRTA6Njk2QjQyNDQiLCJ2aXNpdG9yX2lkIjoiMjU3MDQyMzIyNTc2NTg5MzgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6358c36bf4b3182b5a84503a7966e3bebda0278930e241c45bdf4d8c26ea6890 |
| hovercard-subject-tag | issue:2578303541 |
| 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/125245/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1e117bc9da63e4a9ece160beedf4f947cc5922a7befc4b01995b283b9579216d/python/cpython/issues/125245 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1e117bc9da63e4a9ece160beedf4f947cc5922a7befc4b01995b283b9579216d/python/cpython/issues/125245 |
| og:image:alt | Bug report Bug description: Discovered alongside #125243 with similar steps to reproduce. I don't have a simpler way to trigger this than "run the PyO3 tests in a loop" because I think it requires ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ngoldbaum |
| 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