PHPUnit Segmentation fault

2020-02-21 05:38发布

When a PHPUnit test fails normally on my dev box (Linux Mint), it causes a "Segmentation Fault" on my Continous Integration box (Centos). Both machines are running the same version of PHPUnit. My dev box is running PHP 5.3.2-1ubuntu4.9, and the CI is PHP 5.2.17. I'd rather leave upgrading the PHP as a last resort though.

As per this thread: PHPUnit gets segmentation fault I have tried deactivating / reinstalling Xdebug. I don't have inclue.so installed.

On the CI box I currently only have two extensions active: dom from php-xml (required for phpunit) and memcache (required by my framework), all the others have been turned off.

13条回答
The star\"
2楼-- · 2020-02-21 06:12

I had the same problem and could nail it down, that I tried to write a class variable which was not definied:

My class (it's a cakePHP-class) which caused segmentation fault:

class MyClass extends AppModel {

  protected $classVariableOne;

  public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);

    $this->classVariableOne =& ClassRegistry::init('ClassVariableOne');

    // This line caused the segmentation fault as the variable doesn't exists
    $this->classVariableTwo =& ClassRegistry::init('ClassVariableTwo');

  }
}

I fixed it by adding the second variable:

class MyClass extends AppModel {

  protected $classVariableOne;
  protected $classVariableTwo; // Added this line

  public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);

    $this->classVariableOne =& ClassRegistry::init('ClassVariableOne');
    $this->classVariableTwo =& ClassRegistry::init('ClassVariableTwo');

  }
}
查看更多
乱世女痞
3楼-- · 2020-02-21 06:14

Next to what cweiske suggested, if upgrading PHP is not an option for you and you have problems to locate the source of the segfault, you can use a debugger to find out more.

You can launch gdb this way to debug a PHPUnit session:

gdb --args php /usr/bin/phpunit quiz_service_Test.php

Then type in r to run the program and/or set environment variables first.

set env MALLOC_CHECK_=3
r

You might also consider to install the debugging symbols for PHP on the system to get better results for debugging. gdb checks this on startup for you and leaves a notice how you can do so.

查看更多
家丑人穷心不美
4楼-- · 2020-02-21 06:14

When you get a segfault, upgrade your PHP to the latest version. Not only the latest in your package manager, but the latest available on php.net. If it still segfaults, you are sure that the problem has not been fixed yet in PHP itself. Don't bother trying to get rid of a segfault in old version of PHP because it might have been fixed already in a newer one.

Next step is to locating the problem: Make your test smaller and smaller until you can't remove anything (but it still segfaults). If you have that, move the test into a standalone php script that segfaults. Now you have a test script for your bug in the PHP bug tracker.

查看更多
太酷不给撩
5楼-- · 2020-02-21 06:15

Infinite recursion is normally what causes this issue for us. The symptoms of infinite recursion seem to be different when running code under phpunit, than they are when running it in other environments.

查看更多
萌系小妹纸
6楼-- · 2020-02-21 06:15

The following fixed a similar issue for me (when the output of the gdb backtrace included libcurl.so and libcrypto.so):

disable /etc/php.d/pgsql.ini:

; Enable pgsql extension module
; extension=pgsql.so

edit /etc/php.d/curl.ini to ensure that pgsql.so is included before curl:

; Enable curl extension module
extension=pgsql.so
extension=curl.so
curl.cainfo=/home/statcounter/include/config/cacert.pem
查看更多
Viruses.
7楼-- · 2020-02-21 06:19

I got into the same problem. I upgraded the PHPUnit to the 4.1 version (to run the tests) and it was able to show me the object, as pointed by Isaac.

So, if you get to this very same problem, upgrade to PHPUnit >= 4.1 and you'll be able to see the error instead of getting "Segmentation fault" message.

查看更多
登录 后发表回答