58
  • Is PHP (as of 5.2) thread-safe on Linux/UNIX?
  • Would it be possible to use it with Apache Worker-MPM or Event-MPM?

The facts I gathered so far are inconclusive:

  • Default binaries included in most distributions have ZTS disabled, so I'm aware, that I'd have to recompile them.
  • In theory Zend Engine (core PHP) with ZTS enabled is thread-safe.
  • It's said that some modules might not be thread-safe, but I haven't found any list of modules that are or that are not.
  • PHP FAQ states pretty much same as above.

What's your experience?

It's not only about segmentation faults ("access violations" in Windows nomenclature). There is a lot more to thread safety.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
vartec
  • 118,560
  • 34
  • 206
  • 238
  • 1
    While this is a hot topic, it is a very good question. Something either is, or is not thread safe .. 'somewhere in the middle' is a good topic to discuss here. – Tim Post Mar 25 '09 at 12:21
  • See official PHP warning: "*[We do not recommend using a threaded MPM in production with Apache 2](http://web.archive.org/web/20160412170643/http://www.php.net/manual/en/install.unix.apache2.php)*". Also related: http://web.archive.org/web/20160412171006/https://docs.newrelic.com/docs/agents/php-agent/troubleshooting/threaded-apache-worker-mpms – Pacerier Apr 12 '16 at 17:08

3 Answers3

26

I know gettext and set_locale is not threadsafe. PHP should not be used with a threaded MPM.

PHP Isn't Thread-Safe Yet.
Running PHP not threaded.

Community
  • 1
  • 1
OIS
  • 9,161
  • 1
  • 29
  • 41
  • Yes, I've read that article before. But it doesn't give much facts. Also segfaults (or "access violation") hasn't really got much to do with thread safety. – vartec Mar 25 '09 at 12:23
  • 2
    Yes they do. If the segfaults are as a result of memory access violations due to incorrect threaded access to shared variables, then that's exactly the problem you are looking to avoid. It's *not only* about segfaults/access violations, but if it does segfault then don't bother looking any further because it's definitely not thread safe. – Mahmoud Al-Qudsi Apr 22 '10 at 12:10
  • Yeah. I'm using nginx with php-fpm because of gettext not being thread-safe: http://stackoverflow.com/questions/1646249/php-gettext-problems-like-non-thread-safe/6726570#6726570 – Stann Jul 17 '11 at 20:38
  • 5
    The article is nearing **10** years old now. Is PHP Isn't Thread-Safe Yet? – Pacerier Aug 14 '15 at 05:53
  • @OIS, Btw how/where did you get that info from? – Pacerier Aug 18 '15 at 03:25
  • @OIS, Nice, is there a compilation of all functions that are thread un/safe? Or do we need to go through each and every function to see if they are thread un/safe? – Pacerier Aug 18 '15 at 13:37
  • @Pacerier you can [download the manual](http://php.net/download-docs.php) and grep for **warning** and **thread**? Or ask on [chat](http://chat.stackoverflow.com/rooms/11/php) here or on [reddit](http://reddit.com/r/php). – OIS Aug 18 '15 at 14:00
  • @OIS, Btw, is this method reliable? Does the manual even **guarantee** that it will warn about thread-safetyness for usages that have such threading problems? I was thinking more along the lines of a document which has a "compilation" or list (e.g. http://php.net/manual/en/migration54.incompatible.php) of thread-safety things to watchout for. – Pacerier Apr 12 '16 at 16:54
  • Apart from **setlocale** and **gettext**, I have never seen another non-thread-safe PHP function. – rustyx Jan 18 '17 at 20:06
  • gettext and setlocale: Best to avoid them. You can easily make a translation storage layer easily and use intl or whatever for formatting. locale is generally problematic threads or not, just being global. The worst thing about it is that it impacts the format of (string)$int for example. – jgmjgm Feb 08 '19 at 19:57
6

See Where can I get libraries needed to compile some of the optional PHP extensions? for a list of thread-safe and nonthread-safe extensions (* marked are not thread-safe and others are).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
AbdolHosein
  • 1
  • 1
  • 1
  • This list work for extensions, but doesn't list thread unsafe features of **non**-extensions. E.g [`setlocale`](http://php.net/manual/en/function.setlocale.php#refsect1-function.setlocale-notes) as mentioned by userOIS above. – Pacerier Apr 12 '16 at 17:00
5

A better question might be, "Is the following PHP code going to trigger access violations if MPM is used?" Or, "Have you experienced odd behavior likely attributed to concurrency issues using the following functions?"

Otherwise, it's Russian roulette. If you're using some packaged application, it may work just fine now but break a month from now when a new version of the application comes out.

I strongly advise against using MPM with PHP in general. However, if you have some small code to run, you could post it, and we could tell you if you're going to hit a pitfall.

Tim Post
  • 32,014
  • 15
  • 104
  • 162
  • 2
    actually code may be not thread-safe and never cause any segfault/access violation. – vartec Mar 25 '09 at 12:27
  • 1
    @TimPost, I don't quite understand why you say it's "Russian roulette". If the code is indeed thread-safe, then it'll be safe to use **regardless** of how many concurrent threads are calling it. Even if you unlucky at "Russian roulette" (by that I suppose you mean the concurrent threads interleaves at every step and in all the worst possible places), it's still safe if the code is thread-safe and we wouldn't get a segfault. – Pacerier Aug 14 '15 at 06:03