2

What is wrong with this code? It works when $it = 1000 but fails with Segmentation fault: 11 when $it > 8000. There is neither a script timeout nor memory exhausted. Setting high memory_limit makes no difference.

$it = 10000;
$middleware = function($chain) {
    $chain();
};

$chain = function() use (&$chain, $it, $middleware) {
    static $index = 0;
    if ($index++ < $it) {
        $middleware($chain);
    }
};

$chain();

My system is PHP 5.6.16 with the following extensions:

Core, date, ereg, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2,
calendar, ctype, dom, hash, fileinfo, filter, ftp, gd, gettext, SPL,
iconv, json, ldap, mbstring, session, standard, mysqlnd, pcntl,
mysqli, PDO, pdo_mysql, pdo_sqlite, Phar, posix, Reflection, mysql,
shmop, SimpleXML, soap, sockets, exif, sysvmsg, sysvsem, sysvshm,
tidy, tokenizer, wddx, xml, xmlreader, xmlrpc, xmlwriter, zip, curl,
gmp, igbinary, imap, intl, mcrypt, memcache, memcached, mongo, 
mssql, OAuth, pdo_dblib, pdo_pgsql, pgsql, propro, raphf, readline,  
redis, solr, ssh2, xsl, http, mhash, xdebug
12
  • Argh, please start using varying variable names instead of just $chain everywhere, it makes it rather painful to parse.
    – Jonnix
    Dec 7, 2015 at 13:13
  • 2
    The Segmentation fault indicates the server has run out of memory. That's why it works until '$it > 8000'. Literally a stack overflow condition.
    – DFriend
    Dec 7, 2015 at 13:25
  • It does not run out of memory. The memory level it takes is much lower than maximum allowed by php
    – spajak
    Dec 7, 2015 at 13:38
  • 1
    Do you have xdebug enabled?
    – Daniel W.
    Dec 7, 2015 at 14:09
  • Yes I can enable xdebug, no difference. xdebug only fires when xdebug.max_nesting_level i lower than $it
    – spajak
    Dec 7, 2015 at 14:12

1 Answer 1

1

Have you tried changing the PHP version? Anytime i get segmentation faults i switch to PHP 7.0 and suddenly there's an accurate error message. I fix the error and switch back to PHP 5.6 and it works again. Maybe your callback throws an exception and the error handler crashes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.