In its current state, the HDC in cairo-win32-font.c is a static variable that can be compromised if multiple threads are attempting to create HDC's for Windows to render the fonts, making this code thread-unsafe. This also makes PangoCairo (which also relies on this code) unable to run in a thread-safe manner, which I am also trying to investigate and fix.
This bug attempts to track my try, based on someone else's patch, to tackle this issue.
Created attachment 91176 [details] [review]
Attempt to make cairo-win32-font.c thread-safe
Lufy D.Monkey's have come up with a patch that attempts to make cairo-win32-
font.c thread-safe. I have adapted his patch (which was dated back to 2010) so that it fits in the current state of the code. Basically what happens is that the HDC's creations/releases are guarded by mutexes and the created HDC's are cached by thread id's so that they will need to be created and released less often.
This does not yet fix the PangoCairo situation (although it would fare better there by reducing intermittent crashes by a whole lot, say from once every 8 runs to 30 runs or so), but I have used this cairocffi Python test to test this code, and it seems that the fix will allow the test to pass, whereas the original code will fail with a
MemoryError: ('cairo returned CAIRO_STATUS_NO_MEMORY: out of memory', 1L)
Merry Christmas, and with blessings, thank you!
How about making the static variable a TLS (Thread Local Storage) one?
- static HDC hdc;
+ static __thread HDC hdc;
-- GitLab Migration Automatic Message --
This bug has been migrated to freedesktop.org's GitLab instance and has been closed from further activity.
You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.freedesktop.org/cairo/cairo/issues/190.