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
Updates based on Sam's review
- Assert locks are held in `_get_(C/D)Dict`
- Update self->d_dict outside `Py_BEGIN_ALLOW_THREADS`
- removed critical section AC I missed
  • Loading branch information
emmatyping committed May 21, 2025
commit d142aa849f56972b20a8df1e588b5dd540d7276c
11 changes: 2 additions & 9 deletions Modules/_zstd/clinic/decompressor.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions Modules/_zstd/compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ capsule_free_cdict(PyObject *capsule)
ZSTD_CDict *
_get_CDict(ZstdDict *self, int compressionLevel)
{
assert(PyMutex_IsLocked(&self->lock));
PyObject *level = NULL;
PyObject *capsule;
ZSTD_CDict *cdict;
Expand Down Expand Up @@ -197,11 +198,14 @@ _get_CDict(ZstdDict *self, int compressionLevel)
}

/* Add PyCapsule object to self->c_dicts if not already inserted */
if (PyDict_SetItem(self->c_dicts, level, capsule) < 0) {
Py_DECREF(capsule);
PyObject *capsule_dict;
int ret = PyDict_SetDefaultRef(self->c_dicts, level, capsule,
&capsule_dict);
Py_XDECREF(capsule_dict);
Py_DECREF(capsule);
if (ret < 0) {
goto error;
}
Py_DECREF(capsule);
}
else {
/* ZSTD_CDict instance already exists */
Expand Down
13 changes: 5 additions & 8 deletions Modules/_zstd/decompressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef struct {
static inline ZSTD_DDict *
_get_DDict(ZstdDict *self)
{
assert(PyMutex_IsLocked(&self->lock));
ZSTD_DDict *ret;

/* Already created */
Expand All @@ -70,9 +71,9 @@ _get_DDict(ZstdDict *self)
char *dict_buffer = PyBytes_AS_STRING(self->dict_content);
Py_ssize_t dict_len = Py_SIZE(self->dict_content);
Py_BEGIN_ALLOW_THREADS
self->d_dict = ZSTD_createDDict(dict_buffer,
dict_len);
ret = ZSTD_createDDict(dict_buffer, dict_len);
Py_END_ALLOW_THREADS
self->d_dict = ret;

if (self->d_dict == NULL) {
_zstd_state* const mod_state = PyType_GetModuleState(Py_TYPE(self));
Expand All @@ -84,10 +85,7 @@ _get_DDict(ZstdDict *self)
}
}

/* Don't lose any exception */
ret = self->d_dict;

return ret;
return self->d_dict;
}

/* Set decompression parameters to decompression context */
Expand Down Expand Up @@ -629,7 +627,6 @@ ZstdDecompressor_dealloc(PyObject *ob)
}

/*[clinic input]
@critical_section
@getter
_zstd.ZstdDecompressor.unused_data

Expand All @@ -641,7 +638,7 @@ decompressed, unused input data after the frame. Otherwise this will be b''.

static PyObject *
_zstd_ZstdDecompressor_unused_data_get_impl(ZstdDecompressor *self)
/*[clinic end generated code: output=f3a20940f11b6b09 input=5233800bef00df04]*/
/*[clinic end generated code: output=f3a20940f11b6b09 input=54d41ecd681a3444]*/
{
PyObject *ret;

Expand Down
Loading