Title: The `datetime` C API is not thread-safe · Issue #141563 · python/cpython · GitHub
Open Graph Title: The `datetime` C API is not thread-safe · Issue #141563 · python/cpython
X Title: The `datetime` C API is not thread-safe · Issue #141563 · python/cpython
Description: Bug report Bug description: datetime has a C API that we expose through a global variable. This does not work well under multiple threads. For example, if PyDateTime_IMPORT fails in one thread while another thread is invoking the datetim...
Open Graph Description: Bug report Bug description: datetime has a C API that we expose through a global variable. This does not work well under multiple threads. For example, if PyDateTime_IMPORT fails in one thread whil...
X Description: Bug report Bug description: datetime has a C API that we expose through a global variable. This does not work well under multiple threads. For example, if PyDateTime_IMPORT fails in one thread whil...
Opengraph URL: https://github.com/python/cpython/issues/141563
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"The `datetime` C API is not thread-safe","articleBody":"# Bug report\n\n### Bug description:\n\n`datetime` has a C API that we expose through a [global variable](https://github.com/python/cpython/blob/da7f4e4b22020cfc6c5b5918756e454ef281848d/Include/datetime.h#L197). This does not work well under multiple threads. For example, if `PyDateTime_IMPORT` fails in one thread while another thread is invoking the datetime C API, then that will set `PyDateTimeAPI` to `NULL`, leading to `NULL` pointer dereferences in the original thread. \n\nThis global variable also forces us to do some nasty hacks with subinterpreters because it exposes static types. I helped fix this a few months ago by making `_datetime` static (see [this thread](https://discuss.python.org/t/datetime-should-be-a-static-module/98857)), but there are probably other issues that I haven't found yet.\n\nI think a bandaid fix would be to make `PyDateTimeAPI` a thread-local variable, but I suspect that would break a lot of things, nor would that fix this for subinterpreters. More generally, we could deprecate `PyDateTime_IMPORT` and `PyDateTimeAPI` in favor of letting users manage the pointer on their own, but I don't know if we can do this without too many code changes (see #141543 and #83785 for the docs side).\n\ncc @pganssle\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_","author":{"url":"https://github.com/ZeroIntensity","@type":"Person","name":"ZeroIntensity"},"datePublished":"2025-11-14T16:30:19.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/141563/cpython/issues/141563"}
| 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:945691a6-8bf0-e739-2943-fc19d9c9b5c5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 89C0:178041:45B1D8:60FA48:696A6AE0 |
| html-safe-nonce | a123b678ed5e155137978ac915ec6c66817312b986776dbb9fa3cd9996665a2d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OUMwOjE3ODA0MTo0NUIxRDg6NjBGQTQ4OjY5NkE2QUUwIiwidmlzaXRvcl9pZCI6Ijc3NjczNDUzODA0NDg2OTI5NjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2c1d7fb8703dc439dcbc079033662bd98b149b9dafd68b22b07b098e2e16e3a2 |
| hovercard-subject-tag | issue:3626321776 |
| 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/141563/issue_layout |
| twitter:image | https://opengraph.githubassets.com/53bc0741ffa49af4c06b3b4819638673b4b2796f51a7165505510e33fc695b57/python/cpython/issues/141563 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/53bc0741ffa49af4c06b3b4819638673b4b2796f51a7165505510e33fc695b57/python/cpython/issues/141563 |
| og:image:alt | Bug report Bug description: datetime has a C API that we expose through a global variable. This does not work well under multiple threads. For example, if PyDateTime_IMPORT fails in one thread whil... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ZeroIntensity |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6fea32d5b7276b841b7a803796d9715bc6cfb31ed549fdf9de2948ac25d12ba6 |
| 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 | f2d9f6432a5a115ec709295ae70623f33bb80aee |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width