Skip to content

BUG: apply critical sections around populating the dispatch cache#27400

Merged
ngoldbaum merged 2 commits intonumpy:maintenance/2.1.xfrom
charris:backport-27392
Sep 17, 2024
Merged

BUG: apply critical sections around populating the dispatch cache#27400
ngoldbaum merged 2 commits intonumpy:maintenance/2.1.xfrom
charris:backport-27392

Conversation

@charris
Copy link
Member

@charris charris commented Sep 16, 2024

Backport of #27392.

Fixes #27386.

This moves the locking to a higher conceptual level in the code. Now we lock the entire ufunc object whenever we go into promote_and_get_info_and_ufuncimpl.

In my testing I'm not able to reproduce the duplicate identity cache entry error @jakevdp saw in the ml_dtypes CI locally after making this change. Unfortunately it's a multithreaded issue and inherently flaky, so I can't be 100% positive this fixes it.

Also side benefit of being a lot simpler!

Updating pythoncapi-compat lets us use the critical section macros without putting them inside Py_GIL_DISABLED macros. They're just open and close brace on the GIL-enabled build.

One question for @seberg or maybe @mhvk: is dtype promotion ever re-entrant? I don't think so but I'd like to double-check because I think it might be problematic if we ever recursively created critical sections here.

@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703) labels Sep 16, 2024
@charris charris added this to the 2.1.1 release milestone Sep 16, 2024
@charris
Copy link
Member Author

charris commented Sep 16, 2024

@ngoldbaum Might check dispatching.c as it differs from the original.

@ngoldbaum

This comment has been minimized.

@charris
Copy link
Member Author

charris commented Sep 16, 2024

@ngoldbaum Looks like Py_BEGIN_CRITICAL_SECTION has unexpected semantics :) I wondered why you moved the declarations out of the section.

@ngoldbaum
Copy link
Member

Oh sorry about my comment earlier, I thought you were commenting on the original PR.

This is definitely wrong, the critical section API is defined with macros that include braces so you can't put them in error handling sections. I'm not sure why the backport didn't apply cleanly - if you don't mine waiting I can probably push the correct backport to this PR sometime tomorrow. I'm out sick today.

@charris
Copy link
Member Author

charris commented Sep 16, 2024

I'm not sure why the backport didn't apply cleanly

Sebastian cleaned out the legacy promotion mode, so the protected section is much smaller in main.

@ngoldbaum
Copy link
Member

I can see why this solution wasn't obvious to me back in June! Thanks for working through this.

This looks correct to me.

I tried reproducing the error in the ml_dtypes tests using this branch and did not see them, so I think this is correct.

@ngoldbaum ngoldbaum merged commit d40c363 into numpy:maintenance/2.1.x Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants