0

Recently changed to php-fpm from apache php on a very old legacy codebase. Having an issue where the whole page stops rendering with no errors in fpm or apache logs.

I wrote a quick test script to reproduce the errors

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');

for ($k = 0; $k < 1000; $k++) {
    echo "new line: " . $k;
    echo "<br>";
    print_mem();
    trigger_error("php-fpm error");
}

function print_mem()
{
    /* Currently used memory */
    $mem_usage = memory_get_usage();

    /* Peak memory usage */
    $mem_peak = memory_get_peak_usage();

    echo 'The script is now using: <strong>' . round($mem_usage / 1024) . 'KB</strong> of     memory.<br>';
    echo 'Peak usage: <strong>' . round($mem_peak / 1024) . 'KB</strong> of memory.    <br><br>';
}

We are currently cleaning up the legacy code but it is a lot of work, can you suggest a way to get over this limit?

R.Meredith
  • 184
  • 2
  • 10
  • 1
    Have you tried ini_set('log_errors_max_len', 0);? – Alex Barker Sep 16 '20 at 13:58
  • Just tried it, no difference. Thanks for your suggestion – R.Meredith Sep 16 '20 at 14:10
  • Increasing memory_limit in php.ini does allow more of the page to render but according to the loop no more memory is taken. I am wondering if fpm log takes up a little memory per loop that is not accounted for. – R.Meredith Sep 16 '20 at 14:18
  • 1
    Couple of other things may be helpful in determining what's happening: Does it always die on a particular iteration number? How long does it run before it stops executing? The most common reason FPM stops processing (vs Apache) is the max_execution_time in the fpm config. I tried your example script on my local instance (default config) and it worked without issue to 999. If it's running out of memory, you should see an error saying that. "Fatal error: Allowed memory size of xxx bytes exhausted" – Alex Barker Sep 16 '20 at 14:18
  • There is no out of a memery error in apache or fpm logs. The amount of memory given to the child process matters despite php not stating it is using more memory. It does always die on the same itteration number and it seems to matter how much is rendered to page as well as errors thrown. – R.Meredith Sep 16 '20 at 14:28

1 Answers1

1

The problem was caused by output buffering in php

changed from 4096 to On in php.ini.

; Development Value: 4096
; Production Value: 4096
; http://php.net/output-buffering
output_buffering = On

It now renders the full loop.

I am unsure why this would cause php to stop rendering, also it is not recommended to set output_buffering to On as detailed in the php.ini.

R.Meredith
  • 184
  • 2
  • 10
  • 1
    I am not happy with this answer, if anyone can explain why this is the case it would be greatly appreciated. – R.Meredith Sep 16 '20 at 15:09