Little T in the Cog apple

Archive for December 2014

Fix for forking when using Core Foundation in PHP on OS X


For a while I've had an issue on my OwnCloud install when I open a PDF or document the browser often sits for 15 seconds before loading the file. During this time I noticed the load going up on the server and checking running processes I could see that Apache was saving a core dump, so had obviously crashed. Checking through the apache error log I could see multiple entries at the same time as the crash occurred like this:

The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
After a bit of googling it turns out I was seeing this error due to a library being used by apache having been compiled against the Core Foundation API. Some more googling turned up the "nm" command which you can use to list the symbol table of libraries, then grep for CF to see if they use the Core Foundation API. Checking the Apache module directory (nm -go /usr/libexec/apache2/* | grep CF) turned up a few modules that use the CF API but nothing that was currently being loaded, so I turned my attention to other libraries that get used by PHP by checking all libraries passed during the --configure stage. After much searching it turns out the freetype library supplied by OS X uses the CF API and I was including this library in my PHP compilation:
$ nm -go /usr/X11/lib/libfreetype.* |grep CF
/usr/X11/lib/libfreetype.6.3.16.dylib:single module:          U _CFRelease
/usr/X11/lib/libfreetype.6.3.16.dylib:single module:          U _CFStringCreateWithCString
/usr/X11/lib/libfreetype.6.dylib:single module:          U _CFRelease
/usr/X11/lib/libfreetype.6.dylib:single module:          U _CFStringCreateWithCString
/usr/X11/lib/libfreetype.dylib:single module:          U _CFRelease
/usr/X11/lib/libfreetype.dylib:single module:          U _CFStringCreateWithCString
One solution would be to simply omit freetype from the compilation, though I'm not sure what other implications this would have on the PHP module produced. A better solution is to compile libfreetype *without* the Core Foundation APIs, and this turns out to be very simple:
curl -O
tar -zxvf freetype-2.5.3.tar.gz
cd freetype-2.5.3
sudo make install
Doing this installs freetype into /usr/local so we don't mess with the system included libraries. Then we simply compile PHP following this guide but replacing the configure option "--with-freetype-dir=/usr/X11R6" with '--with-freetype-dir=/usr/local". Once the module is installed there were no longer any pauses when opening PDFs and I've yet to see that error again in the Apache error logs!