PHP: recursion causes segmentation fault
The fact that attempting recursion in PHP leaves you at the mercy of segmentation faults isn’t anything new, really (though it is new to me now). Since 1999, bug reports to this effect have been knocked back, stating that this is the intended behaviour.
This is all due to technical limitation inside PHP, which I guess is reasonable. PHP pushes to the stack each time you enter a new function call, which is quick and handy, but limits your recusion depth.
What annoys me is that it’s one more reason that PHP is not very good for creating portable code. There’s no warning or PHP error before you exhaust the recursion limit, PHP just crashes (bringing down an Apache process with it if you’re using mod_php). The “safe” depth of recursion you could do on PHP is hard to predict, and is going to vary between installations and builds. I’ve seen posts online showing people doing experiments to try and figure out PHP’s recursion limit, but there’s too many factors at work – how PHP’s been built, whether it’s 32-bit or 64-bit, the type and quantity of variables you are passing as arguments or return values, and so on. In some code of my own, I’ve seen php-cgi crashing after just 20 to 30 levels of recursion. The function was passing some rather large arrays as return values.
You could argue that the same issue is going to happen in a C program, and that any decent programmer should be ready to code around this, anyway. However, in my mind PHP should be a level of abstraction higher than this sort of detail; a programmer in such a high level scripting language should not need to worry about the underlying low-level implementation. Ideally, PHP ought to put out a PHP error, allowing for the problem to be reported gracefully just as if you’d tried to call a non-existant function, and the PHP interpreter should end the script gracefully without crashing any processes.
The bottom line is that if you’re doing recursion in PHP, you should use your own method to limit the depth to a very conservative level, or perhaps rewrite it to use a big loop instead of recursion. If, like me, you’re writing a recursive descent parser, if you need to make your code portable you may be better off to rewrite it to use a big loop.
Entry filed under: Software development.