8000 gh-133885: Use locks instead of critical sections for _zstd by emmatyping · Pull Request #134289 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-133885: Use locks instead of critical sections for _zstd #134289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Put locks around relevant functions and use PyDict_GetItemRef
  • Loading branch information
emmatyping committed May 23, 2025
commit 93dec23d0e408df618c2c942b0181edd6bcd6f8d
20 changes: 12 additions & 8 deletions Modules/_zstd/compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ _get_CDict(ZstdDict *self, int compressionLevel)
{
assert(PyMutex_IsLocked(&self->lock));
PyObject *level = NULL;
PyObject *capsule;
PyObject *capsule = NULL;
ZSTD_CDict *cdict;
int ret;


/* int level object */
Expand All @@ -166,11 +167,12 @@ _get_CDict(ZstdDict *self, int compressionLevel)
}

/* Get PyCapsule object from self->c_dicts */
capsule = PyDict_GetItemWithError(self->c_dicts, level);
ret = PyDict_GetItemRef(self->c_dicts, level, &capsule);
if (ret < 0) {
Py_XDECREF(capsule);
goto error;
}
if (capsule == NULL) {
if (PyErr_Occurred()) {
goto error;
}
/* Create ZSTD_CDict instance */
char *dict_buffer = PyBytes_AS_STRING(self->dict_content);
Py_ssize_t dict_len = Py_SIZE(self->dict_content);
Expand All @@ -197,9 +199,8 @@ _get_CDict(ZstdDict *self, int compressionLevel)
goto error;
}

/* Add PyCapsule object to self->c_dicts if not already inserted */
int ret = PyDict_SetDefaultRef(self->c_dicts, level, capsule, NULL);
Py_DECREF(capsule);
/* Add PyCapsule object to self->c_dicts */
ret = PyDict_SetItem(self->c_dicts, level, capsule);
if (ret < 0) {
goto error;
}
Expand All @@ -214,6 +215,7 @@ _get_CDict(ZstdDict *self, int compressionLevel)
cdict = NULL;
success:
Py_XDECREF(level);
Py_XDECREF(capsule);
return cdict;
}

Expand Down Expand Up @@ -422,6 +424,7 @@ static PyObject *
compress_lock_held(ZstdCompressor *self, Py_buffer *data,
ZSTD_EndDirective end_directive)
{
assert(PyMutex_IsLocked(&self->lock));
ZSTD_inBuffer in;
ZSTD_outBuffer out;
_BlocksOutputBuffer buffer = {.list = NULL};
Expand Down Expand Up @@ -504,6 +507,7 @@ mt_continue_should_break(ZSTD_inBuffer *in, ZSTD_outBuffer *out)
static PyObject *
compress_mt_continue_lock_held(ZstdCompressor *self, Py_buffer *data)
{
assert(PyMutex_IsLocked(&self->lock));
ZSTD_inBuffer in;
ZSTD_outBuffer out;
_BlocksOutputBuffer buffer = {.list = NULL};
Expand Down
2 changes: 2 additions & 0 deletions Modules/_zstd/decompressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ decompress_lock_held(ZstdDecompressor *self, ZSTD_inBuffer *in,
static void
decompressor_reset_session_lock_held(ZstdDecompressor *self)
{
assert(PyMutex_IsLocked(&self->lock));

/* Reset variables */
self->in_begin = 0;
Expand All @@ -362,6 +363,7 @@ static PyObject *
stream_decompress_lock_held(ZstdDecompressor *self, Py_buffer *data,
Py_ssize_t max_length)
{
assert(PyMutex_IsLocked(&self->lock));
ZSTD_inBuffer in;
PyObject *ret = NULL;
int use_input_buffer;
Expand Down
Loading
0