LoginSignup
2
1

More than 5 years have passed since last update.

1回のリクエストで複数回 Fatal error になるケース

Posted at

PHP で Fatal error になるとその時点で処理が停止するため、1回のリクエストで2回 Fatal error にはならないはずですが、下記のような Fatal error の後でも実行できる処理で Fatal error が発生すると複数回発生することがあります。

  • register_shutdown_function で登録したシャットダウン関数
  • session_set_save_handler で登録したセッションハンドラ の writeclose
<?php
class MySessionHandler extends SessionHandler
{
    public function write($session_id, $session_data)
    {
        for (;;) {
            $arr[] = str_repeat('x', 1024*1024);
        }
        parent::write($session_id, $session_data);
    }

    public function close()
    {
        for (;;) {
            $arr[] = str_repeat('x', 1024*1024);
        }
        parent::close();
    }

}

register_shutdown_function(function () {
    for (;;) {
        $arr[] = str_repeat('x', 1024*1024);
    }
});

session_set_save_handler(new MySessionHandler(), true);
session_start();

for (;;) {
    $arr[] = str_repeat('x', 1024*1024);
}

1回のリクエストでログに複数回 Fatal error が出ているのを見て不可解に感じたけど、よく考えればおかしなことではなかった。

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 1052672 bytes) in /app/public/xxx.php on line 32
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 1052672 bytes) in /app/public/xxx.php on line 24
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 1052672 bytes) in /app/public/xxx.php on line 7
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 1052672 bytes) in /app/public/xxx.php on line 15
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1