Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit Segmentation fault

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.

like image 353
Rudolf Vavruch Avatar asked Jun 14 '11 12:06

Rudolf Vavruch


8 Answers

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.

like image 124
hakre Avatar answered Oct 05 '22 11:10

hakre


I've had an issue with PHPUnit segfaulting and had trouble finding an answer, so hopefully this helps someone with the same issue later.

PHPUnit was segfaulting, but only:

  • If there was an error (or more than one)
  • After all tests had run but before the errors were printed

After a while I realized that it was due to failures on tests that used data providers, and specifically for data providers that passed objects with lots of recursive references. A bell finally went off and I did some digging: the problem is that when you're using data providers and a test fails, PHPUnit tries to create a string representation of the provided arguments for the failure description to tell you what failed, but this is problematic when one of the arguments has some infinite recursion. In fact, what PHPUnit does in PHPUnit_Framework_TestCase::dataToString() (around line 1612) is print out all the arguments provided by the data provider using print_r, which causes the segfault when PHP tries to create a string representation of the infinitely recursive object.

The solution I came to was:

  1. Use a single base class for all my test classes (which fortunately I was already doing)
  2. Override dataToString() in my test base class, to check for these kinds of objects in the data array (which is possible in my case because I know what these objects look like). If the object is present, I return some special value, if not I just pass it along to the parent method.
like image 20
Isaac Avatar answered Oct 05 '22 11:10

Isaac


I had similar problem and by disabling the garbge collactor in

PHPStorm => Edit configuration => Interpreter option : -d zend.enable_gc=0

Or if you are running your tests from the command line you may try adding :

-d zend.enable_gc=0

like image 30
Mohamed Salem Lamiri Avatar answered Oct 05 '22 10:10

Mohamed Salem Lamiri


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.

like image 20
cweiske Avatar answered Oct 05 '22 09:10

cweiske


In addition to https://stackoverflow.com/a/38789046/246790 which helped me a lot:

You can use PHP function gc_disable();

I have placed it in my PHPUnit bootstrap code as well with ini_set('memory_limit', -1);

like image 40
Ololo Avatar answered Oct 05 '22 11:10

Ololo


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');

  }
}
like image 37
bish Avatar answered Oct 05 '22 09:10

bish


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.

like image 24
Ryan Avatar answered Oct 05 '22 11:10

Ryan


If anyone comes across this in relation to PHPunit within Laravel

It took a while to figure out what the issue was. I was going over the differences between my current code and the previous revision and through some trial and error finally got there.

I had two different models that were both including each other with the protected $with override.

This must have been causing some kind of loop that phpunit could not deal with.

Hopefully someone finds this useful.

like image 27
Frank Avatar answered Oct 05 '22 10:10

Frank