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-99108: Release the GIL around hashlib built-in computation by gpshead · Pull Request #104675 · python/cpython

Conversation

@gpshead

@gpshead gpshead commented May 20, 2023 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This matches the GIL releasing behavior of our existing _hashopenssl
module, extending it to the HACL* built-ins.

  • Issue: Replace built-in hashlib with verified implementations from HACL* #99108

Sorry, something went wrong.

Uh oh!

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

All reactions
gpshead added 2 commits May 19, 2023 19:46
@gpshead
pythongh-99108: Release the GIL around hashlib built-in hash updates.
73094a1
This matches the GIL releasing behavior of our existing `_hashopenssl`
module, extending it to the HACL* built-ins.
@gpshead
Do the same for sha2, sha1, & md5.
621f9a6
@gpshead gpshead self-assigned this May 20, 2023
@bedevere-bot bedevere-bot mentioned this pull request May 20, 2023
Replace built-in hashlib with verified implementations from HACL* #99108
Closed
@gpshead gpshead changed the title gh-99108: Release the GIL around hashlib built-in hash updates. gh-99108: Release the GIL around hashlib built-in computation May 20, 2023
@gpshead
reword NEWS.
4cf2a19
@gpshead

gpshead commented May 20, 2023

Copy link
Copy Markdown
Member Author

@msprotz - does this make sense to you?

All reactions

Sorry, something went wrong.

Uh oh!

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

@msprotz

msprotz commented May 20, 2023

Copy link
Copy Markdown
Contributor

Do you have a reference for how the Python GC works? I'm reading https://wiki.python.org/moin/GlobalInterpreterLock, https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock, and https://docs.python.org/3/c-api/memory.html -- is there any other good resource?

Basically I'd like to understand if:

  • a compaction can be triggered while the C code no longer has the GIL (I assume not, but it'd be good to confirm), and
  • if the C code needs to increase the refcount of e.g. the input data (I also assume not, since the callstack owns the object somewhere, its refcount must remain > 1, but also would be good to confirm).

I'd like to read up a little bit before giving you a thumbs-up. Thanks!

gpshead reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@gpshead

gpshead commented May 20, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

CPython is purely references counted with no compaction or moving of objects in memory. The GC exists solely to deal with reference cycles. It never rearranges memory and never frees memory behind any object with a non-zero reference count. We've got a writeup on that at https://devguide.python.org/internals/garbage-collector/. It has been this way since Python 2.0 when the cyclic GC was introduced. (before that, reference cycles were memory leaks)

No memory returned from Python C APIs will ever be moved or freed so long as it belongs to referenced objects. The PyBytes objects the hash functions receive always have positive refcounts by definition, thus the PyBytes_AsStringAndSize() returned pointer is safely passed synchronously to other C code with GIL released as our thread owns that immutable object.

This PR applies identical logic to what _hashopenssl has done for a very long time to release the GIL. The lock per hash object is added to avoid code being able to call into the C hash state mutation APIs on a given instance from multiple threads at once. (It'd be clearly buggy code design if anyone ever did - our goal is just to avoid undefined behavior of C API misuse should anyone ever try)

All reactions

Sorry, something went wrong.

Uh oh!

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

msprotz
msprotz approved these changes May 22, 2023
View reviewed changes

@msprotz msprotz 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.

Ok that makes a lock more sense and now I understand the purpose of the lock. Thanks for the pointers!

Overall I think it would be helpful to document the intent behind this locking behavior, notably:

  • what you said about avoiding locking up the CPU in case there's lots of data to hash
  • the defensive lock that isn't strictly required but helps guard against rogue C API clients
  • the lazy initialization and the fact that the module can't assume obj->lock is non-NULL (may be uninitialized or, as I understand it, lock creation may have failed).

Other than that, as far as I can tell, this looks fine. Thanks!

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Modules/md5module.c Resolved

Uh oh!

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

Comment thread Modules/md5module.c Resolved

Uh oh!

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

Comment thread Modules/md5module.c Resolved

Uh oh!

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

@bedevere-bot bedevere-bot added the awaiting core review label May 22, 2023
@gpshead
Better describe the ENTER/LEAVE macros.
88f190f
@gpshead

gpshead commented May 22, 2023

Copy link
Copy Markdown
Member Author

Agreed, I'll work on some common code comments to apply to everywhere relevant about these patterns.

All reactions

Sorry, something went wrong.

Uh oh!

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

@gpshead
Explain the locking strategy in comments.
c8e0b01
msprotz
msprotz reviewed May 22, 2023
View reviewed changes
Comment thread Modules/_hashopenssl.c Resolved

Uh oh!

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

@gpshead
gpshead marked this pull request as ready for review May 22, 2023 23:03
@gpshead
gpshead requested a review from tiran as a code owner May 22, 2023 23:03
@gpshead gpshead added the needs backport to 3.12 only security fixes label May 22, 2023
@gpshead
gpshead enabled auto-merge (squash) May 22, 2023 23:05
gpshead added 2 commits May 22, 2023 16:05
@gpshead
Merge branch 'main' into hashlib_hacl_dropGIL
2830028
@gpshead
Merge branch 'main' into hashlib_hacl_dropGIL
0e60cec
@gpshead
gpshead merged commit 2e5d8a9 into python:main May 23, 2023
@miss-islington

miss-islington commented May 23, 2023

Copy link
Copy Markdown
Contributor

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot removed the awaiting core review label May 23, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 23, 2023
@gpshead @miss-islington
pythongh-99108: Release the GIL around hashlib built-in computation (p…
3c9484a
…ythonGH-104675)

This matches the GIL releasing behavior of our existing `_hashopenssl`
module, extending it to the HACL* built-ins.

Includes adding comments to better describe the ENTER/LEAVE macros
purpose and explain the lock strategy in both existing and new code.
(cherry picked from commit 2e5d8a9)

Co-authored-by: Gregory P. Smith <[email protected]>
@bedevere-bot

bedevere-bot commented May 23, 2023

Copy link
Copy Markdown

GH-104776 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label May 23, 2023
@gpshead
gpshead deleted the hashlib_hacl_dropGIL branch May 23, 2023 00:22
gpshead added a commit that referenced this pull request May 23, 2023
@miss-islington @gpshead
[3.12] gh-99108: Release the GIL around hashlib built-in computation (G…
9aea1f2
…H-104675) (#104776)

gh-99108: Release the GIL around hashlib built-in computation (GH-104675)

This matches the GIL releasing behavior of our existing `_hashopenssl`
module, extending it to the HACL* built-ins.

Includes adding comments to better describe the ENTER/LEAVE macros
purpose and explain the lock strategy in both existing and new code.
(cherry picked from commit 2e5d8a9)

Co-authored-by: Gregory P. Smith [Google] <[email protected]>
@erlend-aasland erlend-aasland mentioned this pull request Jun 6, 2023
3.12 backport gh 105236 #105358
Closed
@coderabbitai coderabbitai Bot mentioned this pull request Jul 30, 2026
wasm/jit backend fixes, blackhole and frame-state repairs, and 3.14 interpreter surface parity pyre-lang/pyre#890
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tiran tiran Awaiting requested review from tiran
1 more reviewer
@msprotz msprotz msprotz approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

@gpshead gpshead

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.

4 participants

@gpshead @msprotz @miss-islington @bedevere-bot
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 01402520 — 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.