Skip to content

The datetime C API is not thread-safe #141563

Description

@ZeroIntensity

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 datetime C API, then that will set PyDateTimeAPI to NULL, leading to NULL pointer dereferences in the original thread.

This 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), but there are probably other issues that I haven't found yet.

I 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).

cc @pganssle

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. sharktide commented on Nov 15, 2025

    @sharktide
    Contributor

    Has anyone found any examples of this on GitHub? I imagine that this might happen because it seems like it would be common to use dateline to measure the time of something executing but at the same time why use datetime itself? There has to be better libs or inbuilt functions for most use cases

  2. ZeroIntensity commented on Nov 15, 2025

    @ZeroIntensity
    MemberAuthor
  3. kumaraditya303 commented on Jan 21, 2026

    @kumaraditya303
    Contributor

    I think we can make it thread safe with atomics, basically create a exchange loop and only initialize it if it is not initialized already.

    Something like this should work:

    diff --git a/Include/datetime.h b/Include/datetime.h
    index ed36e6e48c8..17c85b19e11 100644
    --- a/Include/datetime.h
    +++ b/Include/datetime.h
    @@ -197,7 +197,15 @@ typedef struct {
     static PyDateTime_CAPI *PyDateTimeAPI = NULL;
     
     #define PyDateTime_IMPORT \
    -    PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
    +    do
    +    {
    +        PyDateTime_CAPI *capi = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0); \
    +        if (capi && _Py_atomic_load_ptr(&PyDateTimeAPI) == NULL) { \
    +            PyDateTime_CAPI *expected_api = NULL; \
    +            _Py_atomic_compare_exchange_ptr(&PyDateTimeAPI, &expected_api, capi); \
    +        }
    +    } while (0)
    +
     
     /* Macro for access to the UTC singleton */
     #define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
    
  4. ZeroIntensity commented on Jan 21, 2026

    @ZeroIntensity
    MemberAuthor

    I think that will probably break code that relies on PyDateTimeAPI for checking if PyDateTime_IMPORT failed, so that's probably too much for a patch release. Maybe it'll work for 3.15 though?

  5. 26 remaining items

  6. added 3 commits that reference this issue on Feb 15, 2026
  7. added 5 commits that reference this issue on Feb 28, 2026
  8. added 6 commits that reference this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions