So I have a very odd issue.
First, I'm using PHP 5.2.6 (as part of MAMP, though this also happens on a LAMP server running 5.2.6 as well), writing a web site using Zend Framework 1.7.2.
Second, the software I'm writing does a rather complicated statistical calculation that requires a lot of memory. Usually it runs fine with 128MB set as the memory limit.
So here's the fun part.
I have a class which is exclusively responsible for getting the data it needs in, doing the calculations required, then returning the values. After it runs, I have been unsetting it, as PHP seems to think that it's still in scope and holding up the memory for it, despite finishing the function in which it is called (this was causing problems where the second run of the calculation was hitting the memory limit and the script died). After this calculation is run once inside that function, it is run again in another function to do another calculation (this is during the same run of execution). Here's where things get weird.
If I run that second calculation, the calling function runs again, despite the fact that there are absolutely no calls to that function from within the code which does this calculation. Also, no errors or warnings appear in PHP.
However, if I increase the memory limit (say, to 1024 MB), it doesn't happen. The overall calling function runs once and continues along its merry way.
So, let me draw a map below.
function A{ calls function B calls function D }
function B{ calls calculation function C }
function D{ calls calculation function C }
with 128 MB of memory, path of functions is as follows (in and back to the caller): A->B->C->B->A->D->C->D->then it runs A from the start of the function, for some reason, to the end (there's a conditional case which becomes true during this execution path, so the path doesn't repeat)
with 1GB of memory, path of functions is as follows: A->B->C->B->A->D->C->D->end of A, which is what is supposed to happen.
If I remove the second calculation from function D, it works. If I up the memory limit, it works.
Question is: have I found a bug in PHP memory management? Or perhaps in the Zend Framework? Again; by increasing the memory limit per script in php.ini, I don't see this issue. That, or remove the second instance of running this calculation which uses a lot of memory.
I'm very, very sure this is the way it happens, despite the oddness of the way it does (I've never seen this kind of jumping back to the start of a calling function). Usually what would happen in this case is that PHP would error out and die, not restart the calling function.