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 all 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
9 changes: 9 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
static PyMutex ft_library_mutex;
lysnikolaou marked this conversation as resolved.
Show resolved Hide resolved
#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
Loading
Loading