GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-116738: Make cmath module thread-safe by yoney · Pull Request #142161 · python/cpython

Conversation

@yoney

@yoney yoney commented Dec 1, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

While investigating thread safety in the cmath module, I found that the module is generally thread-safe. However, there is a potential race condition during the initialization of the special value tables in cmath_exec(). This is usually safe, but with subinterpreters, a race condition could occur.

The fix converts all special value tables from runtime initialization to static initialization, eliminating the race condition.

I have also created a test to run under ThreadSanitizer. However, I am not including the test in the current PR because, although this PR addresses the issue in the cmath module, ThreadSanitizer still reports other issues below when subinterpreters and free-threading are used together. I plan to address those issues.

...
SUMMARY: ThreadSanitizer: data race /workspace/cmath_tsan/./Modules/posixmodule.c:18691 in posixmodule_exec
...
SUMMARY: ThreadSanitizer: data race /workspace/cmath_tsan/Objects/exceptions.c:4547 in _PyBuiltins_AddExceptions
...
SUMMARY: ThreadSanitizer: data race /workspace/cmath_tsan/Python/crossinterp_exceptions.h:154 in init_static_exctypes
...
...

cc: @mpage @colesbury

  • Issue: Audit all built-in modules for thread safety #116738

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@yoney
pythongh-116738: Make cmath module thread-safe
8b961ad
@bedevere-app bedevere-app Bot mentioned this pull request Dec 1, 2025
Audit all built-in modules for thread safety #116738
Open
colesbury
colesbury reviewed Dec 1, 2025
View reviewed changes

@colesbury colesbury left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Generally looks good. It's subinterpreter specific and not really free threading related (i.e., you'll have the same data races with the default GIL enabled build).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yoney reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Modules/cmathmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tools/c-analyzer/cpython/ignored.tsv Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-12-01-10-03-08.gh-issue-116738.972YsG.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@yoney
yoney marked this pull request as ready for review December 1, 2025 20:58
@yoney
yoney requested a review from ericsnowcurrently as a code owner December 1, 2025 20:58
@bedevere-app bedevere-app Bot added the awaiting review label Dec 1, 2025
@yoney
Address review comments
90be0d7
@yoney

yoney commented Dec 1, 2025

Copy link
Copy Markdown
Contributor Author

Thank you for the review, Sam!

It's subinterpreter specific and not really free threading related.

Yes, I agree, this is related to subinterpreters. I was mainly checking the module for ft-python, which is why I created the PR under gh-116738 to mark it checked.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

skirpichev
skirpichev reviewed Dec 2, 2025
View reviewed changes
Comment thread Modules/cmathmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

yoney added 3 commits December 2, 2025 11:17
@yoney
Static initialize trigonometric tables
90ab34c
@yoney
Address Windows compiler error
a365734
@yoney
Remove C(R, I) macro
64254ba
skirpichev
skirpichev reviewed Dec 3, 2025
View reviewed changes

@skirpichev skirpichev left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks good, but could you double check and verify that all functions include tests for all combinations of special values? I.e. when one component is a zero, infinity or nan, same other (or is a finite nonzero number, if the first is nonzero).

Tests are here: Lib/test/mathdata/cmath_testcases.txt (usually in a dedicated section, like "special values").

Test script
#!/bin/sh

# a.sh

file=Lib/test/mathdata/cmath_testcases.txt
funcs="acos acosh asinh atanh cosh exp log sinh sqrt tanh rect"
zeros="0.0 -0.0"
nonfinite="-inf nan inf"
all="$zeros $nonfinite [1-9]\.[0-9].* -[1-9]\.[0-9].*"
for f in $funcs
do
  for t1 in $zeros
  do
    for t2 in $zeros $nonfinite
    do
      re=" $f $t1 $t2 -> "
      grep -q "$re" $file || echo "$f $t1 $t2 - missing"
      re=" $f $t2 $t1 -> "
      grep -q "$re" $file || echo "$f $t2 $t1 - missing"
    done
  done
  for t1 in $nonfinite
  do
    for t2 in $all
    do
      re=" $f $t1 $t2 -> "
      grep -q "$re" $file || echo "$f $t1 $t2 - missing!"
      re=" $f $t2 $t1 -> "
      grep -q "$re" $file || echo "$f $t2 $t1 - missing!"
    done
  done
done

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev
skirpichev self-requested a review December 3, 2025 00:20
skirpichev
skirpichev approved these changes Dec 3, 2025
View reviewed changes

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

I've checked that special values are tested, though appreciate a second look. Please adjust PR description (an maybe title).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yoney reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Dec 3, 2025
@yoney

yoney commented Dec 3, 2025

Copy link
Copy Markdown
Contributor Author

I've checked that special values are tested, though appreciate a second look. Please adjust PR description (an maybe title).

Thank you so much, @skirpichev

I’ve checked them as well and they look good to me. I am just sharing how I verified this locally, since it’s easy to accidentally copy-paste something incorrectly and not notice it: I compared the statically initialized version with the previous version before deleting the old tables from the code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury
colesbury approved these changes Dec 3, 2025
View reviewed changes

@colesbury colesbury left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Dec 3, 2025
@colesbury colesbury added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Dec 3, 2025
@bedevere-bot

bedevere-bot commented Dec 3, 2025

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 64254ba 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F142161%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Dec 3, 2025
@colesbury

colesbury commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

Buildbot failures are unrelated

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury
colesbury merged commit 2dac9e6 into python:main Dec 4, 2025
146 of 162 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Dec 4, 2025
@colesbury colesbury added the needs backport to 3.14 bugs and security fixes label Dec 4, 2025
@miss-islington-app

miss-islington-app Bot commented Dec 4, 2025

Copy link
Copy Markdown

Thanks @yoney for the PR, and @colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 4, 2025
@yoney @miss-islington
pythongh-116738: Statically initialize special constants in cmath mod…
903a785
…ule (pythongh-142161)

The initialization during `mod_exec` wasn't thread-safe with multiple interpreters.
(cherry picked from commit 2dac9e6)

Co-authored-by: Alper <[email protected]>
@bedevere-app

bedevere-app Bot commented Dec 4, 2025

Copy link
Copy Markdown

GH-142261 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Dec 4, 2025
colesbury pushed a commit that referenced this pull request Dec 4, 2025
@miss-islington @yoney
[3.14] gh-116738: Statically initialize special constants in cmath mo…
94012d6
…dule (gh-142161) (gh-142261)

The initialization during `mod_exec` wasn't thread-safe with multiple interpreters.
(cherry picked from commit 2dac9e6)

Co-authored-by: Alper <[email protected]>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@yoney @StanFromIreland
pythongh-116738: Statically initialize special constants in cmath mod…
0f1fae1
…ule (pythongh-142161)

The initialization during `mod_exec` wasn't thread-safe with multiple interpreters.
@yoney
yoney deleted the ft_cmath branch January 27, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ZeroIntensity ZeroIntensity ZeroIntensity left review comments
@colesbury colesbury colesbury approved these changes
@skirpichev skirpichev skirpichev approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@yoney @bedevere-bot @colesbury @skirpichev @ZeroIntensity
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.