Skip to content
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

Fix SEGFAULT from calling FT_New_Face/FT_Done_Face in multiple threads #8496

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
10 changes: 10 additions & 0 deletions src/_imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ struct {
/* font objects */

static FT_Library library;
#ifdef Py_GIL_DISABLED
PyMutex ft_library_mutex;
#endif

typedef struct {
PyObject_HEAD FT_Face face;
Expand Down Expand Up @@ -187,7 +190,9 @@ getfont(PyObject *self_, PyObject *args, PyObject *kw) {

if (filename && font_bytes_size <= 0) {
self->font_bytes = NULL;
MUTEX_LOCK(&ft_library_mutex);
error = FT_New_Face(library, filename, index, &self->face);
MUTEX_UNLOCK(&ft_library_mutex);
} else {
/* need to have allocated storage for font_bytes for the life of the object.*/
/* Don't free this before FT_Done_Face */
Expand All @@ -197,13 +202,15 @@ getfont(PyObject *self_, PyObject *args, PyObject *kw) {
}
if (!error) {
memcpy(self->font_bytes, font_bytes, (size_t)font_bytes_size);
MUTEX_LOCK(&ft_library_mutex);
error = FT_New_Memory_Face(
library,
(FT_Byte *)self->font_bytes,
font_bytes_size,
index,
&self->face
);
MUTEX_UNLOCK(&ft_library_mutex);
}
}

Expand Down Expand Up @@ -1433,7 +1440,9 @@ font_setvaraxes(FontObject *self, PyObject *args) {
static void
font_dealloc(FontObject *self) {
if (self->face) {
MUTEX_LOCK(&ft_library_mutex);
FT_Done_Face(self->face);
MUTEX_UNLOCK(&ft_library_mutex);
}
if (self->font_bytes) {
PyMem_Free(self->font_bytes);
Expand Down Expand Up @@ -1639,6 +1648,7 @@ PyInit__imagingft(void) {
}

#ifdef Py_GIL_DISABLED
ft_library_mutex = (PyMutex){0};
lysnikolaou marked this conversation as resolved.
Show resolved Hide resolved
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif

Expand Down
Loading
Loading