Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Lib/test/test_free_threading/test_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import datetime
import unittest

from test.support import threading_helper
from test.support.threading_helper import run_concurrently


NTHREADS = 10


@threading_helper.requires_working_threading()
class TestDatetime(unittest.TestCase):
def test_concurrent_strftime_normalize_century(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test does not work in our current testing configuration, as it relies on the cache being empty, correct? In the CI job the whole suite runs in parallel threads, so by the time this test runs the cache is almost certainly already initialised...

d = datetime.date(2026, 4, 12)
results = []

def worker():
results.append(d.strftime("%Y-%m-%d"))

run_concurrently(worker_func=worker, nthreads=NTHREADS)
self.assertEqual(results, ["2026-04-12"] * NTHREADS)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a ThreadSanitizer data race on the static cache in ``normalize_century()`` of the datetime module under the free-threaded build.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix a ThreadSanitizer data race on the static cache in ``normalize_century()`` of the datetime module under the free-threaded build.
Fix a data race in :meth:`datetime.date.strftime` and :meth:`datetime.datetime.strftime` on the free-threaded build.

Please only include user-facing details in blurbs.

10 changes: 6 additions & 4 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1783,17 +1783,19 @@ inline static int
normalize_century(void)
{
static int cache = -1;
if (cache < 0) {
int res = FT_ATOMIC_LOAD_INT_RELAXED(cache);
if (res < 0) {
char year[5];
struct tm date = {
.tm_year = -1801,
.tm_mon = 0,
.tm_mday = 1
};
cache = (strftime(year, sizeof(year), "%Y", &date) &&
strcmp(year, "0099") != 0);
res = (strftime(year, sizeof(year), "%Y", &date) &&
strcmp(year, "0099") != 0);
FT_ATOMIC_STORE_INT_RELAXED(cache, res);
}
return cache;
return res;
}

static PyObject *
Expand Down
Loading