Skip to content

Conversation

@yoney
Copy link
Contributor

@yoney yoney commented Oct 24, 2025

I was initially looking into the bz2 module for free-threading. The methods are already wrapped with a lock, which I believe is primarily used to release the GIL. This locking makes the methods thread-safe in free-threaded build. I replaced PyThread_acquire_lock with PyMutex, which releases the GIL when the thread is parked. This change removes some macros and allocation handling code.

cc: @mpage @colesbury

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Thanks and sorry for the back-and-forth. A small tip: when I (and I mean me explicitly) suggest something (and don't explicitly request changes), you don't need to directly follow my advice if my suggestion was phrased as a question (that question could be addressed to a wider audience).

@yoney
Copy link
Contributor Author

yoney commented Oct 25, 2025

@picnixz Thanks for the review! I’m glad we discovered zstd, thanks to @emmatyping.

Since the changes were quite small, I just created the extra commit, knowing we might need to revert if needed. This is just part of a good review process!

Copy link
Member

@emmatyping emmatyping left a comment

Choose a reason for hiding this comment

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

Thanks! I think this looks good

@emmatyping
Copy link
Member

Oh, perhaps we should add a test on free-threading to ensure we aren't getting data races and the locking is working? Feel free to take some inspiration from https://github.com/python/cpython/blob/main/Lib/test/test_zstd.py#L2668

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@yoney
Copy link
Contributor Author

yoney commented Oct 27, 2025

Oh, perhaps we should add a test on free-threading to ensure we aren't getting data races and the locking is working? Feel free to take some inspiration from https://github.com/python/cpython/blob/main/Lib/test/test_zstd.py#L2668

@emmatyping Thanks for pointing to the zstd tests! I had a local test for compressor, but after seeing the decompressor example in zstd, I also created a decompression test and added it to the PR. Thank you!

@colesbury colesbury merged commit 9479a62 into python:main Oct 27, 2025
43 checks passed
@yoney yoney deleted the ft_bz2 branch October 28, 2025 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants