#40858 [Asn]: Thread safety issue in GD
| From: | nlopess@php.net | Date: | Thu, 22 Mar 2007 23:15:13 +0000 |
| Subject: | #40858 [Asn]: Thread safety issue in GD | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-110702@lists.php.net to get a copy of this message | ||
ID: 40858
Updated by: nlopess@php.net
Reported By: scottmacvicar at ntlworld dot com
Status: Assigned
Bug Type: GD related
Operating System: RHEL 4
PHP Version: 5.2.1
Assigned To: pajoye
New Comment:
yep, having a statically declared mutex would fix the problems we are
having. I've a patch identical to yours
(http://mega.ist.utl.pt/~ncpl/libgd_mutexes.txt) for a while, but we are
still waiting for a win32 implementation to merge the patches (there is
another open bug report in the gd bugs db about a similar issue) to
definitely fix those nasty TS issues.
Previous Comments:
------------------------------------------------------------------------
[2007-03-21 01:37:18] scottmacvicar at ntlworld dot com
Patch is available at
http://public.vbulletin.com/bugs/php/gd-mutex-patch.txt
Quick explanation:
* The mutex is created at module startup and destroyed at module
shutdown courtesy of two new helper functions within the libGD code
base.
* The mutex is no longer destroyed at request shutdown.
* Locking is performed prior to checking fontCache to prevent the race
condition we're seeing.
This can't be merged back into libGD at the moment since it breaks
backwards compatibility by assuming a mutex has been allocated, the way
to fix this for pthread is fairly easy.
#define gdMutexDeclare(x) pthread_mutex_t x
#define gdMutexDeclare(x) pthread_mutex_t x
becomes
#define gdMutexDeclare(x) static pthread_mutex_t x =
PTHREAD_MUTEX_INITIALIZER
#define gdMutexSetup(x)
That would just leave the win32 implementation to deal with via
DLLMain, a quick patch to add that would only take a few minutes though
I don't have VC6 installed yet.
------------------------------------------------------------------------
[2007-03-21 00:48:56] pajoye@php.net
"Moving the code to PHP_MINIT fixes the problem for PHP but the
threading options of GD doesn't extend itself to this setup since the
function to create the mutex can be called more than once in a short
period of time."
Do you mean for libGD itself used a library in a random project? If
yes, a solution is very similar to this one. We have to do it at load
time (like DLLMain on windows). Many other libraries (and many related
to FT2, with ft2 cache or their own) rely on this solution. We can give
it a try, at least.
"I've got a patch that implements the PHP solution running on our
boxes, I can provide it if interested."
You can always post it, I still did not get the time to do it, so any
help is welcome.
------------------------------------------------------------------------
[2007-03-21 00:40:40] scottmacvicar at ntlworld dot com
Moving the code to PHP_MINIT fixes the problem for PHP but the
threading options of GD doesn't extend itself to this setup since the
function to create the mutex can be called more than once in a short
period of time.
This small race condition could be fixed by using pthread_once to
ensure the mutexes are allocated only once but I'm unsure of the Win32
equivalent.
I've got a patch that implements the PHP solution running on our boxes,
I can provide it if interested.
------------------------------------------------------------------------
[2007-03-19 19:27:31] pajoye@php.net
Did we not put it inside in the latest version of the last patch? I do
not have the code at hand but as far as I remember Nuno updated it. I
will check again tonight.
------------------------------------------------------------------------
[2007-03-19 19:24:54] tony2001@php.net
>The first problem looks weird. It defeats the whole purpose of the
mutex.
The !=NULL check is out of the mutex protected block.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/40858
--
Edit this bug report at http://bugs.php.net/?id=40858&edit=1