We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model::findFirst() causes segmentation fault

Hi PhalconPHP!

My PhalconPHP 2.1.0-RC1 application was working fine, until I started interacting with models. Now, any code that uses a model causes the application to crash with a segmentation fault:

$ tail /var/log/apache2/error.log

[Sun Apr 24 17:56:33.085807 2016] [core:notice] [pid 17024] AH00051: child pid 17065 exit signal Segmentation fault (11), possible coredump in /etc/apache2

Apparently, there is a known issue with models and segmentation faults, which is related to PHP 7 test related issues. However, I'm new to PhalconPHP, and I want to be sure I'm not missing something stupid before I submit an issue on Github.

I'm using the following software:

  • Ubuntu 14.04 (last apt-get update && apt-get upgrade 4/24/16)
  • Apache 2.4.12
  • MySQL 5.6.30
  • PHP 7.0.5
  • Zephir 0.9.2a-dev
  • Phalcon 2.1.0-RC1 (last git pull and zephir build 4/24/16)

I have a groups table:

mysql> SHOW CREATE TABLE `groups`;
CREATE TABLE `groups` (
  `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(15) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

The groups table has a few groups:

mysql> SELECT * FROM groups;
+----+------+
| id | name |
+----+------+
|  2 | foo  |
|  1 | bar  |
+----+------+
2 rows in set (0.05 sec)

I have a Group model:

<?php

namespace App\Model;

/**
 * A group
 */
class Group extends \Phalcon\Mvc\Model
{
    /* !Private properties */

    /**
     * @var  int  the group's id
     */
    private $id;

    /**
     * @var  string  the group's *unique* name (lower-case and plural by convention)
     */
    private $name;

    /* !Get methods */

    /**
     * Returns the group's id
     *
     * @return  int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * Returns the group's name
     *
     * @return  string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /* !Set methods */

    /**
     * Sets the group's name
     *
     * @param   string  $name
     * @return  self
     */
    public function setName(string $name): self
    {
        $this->name = strtolower($name);

        return $this;
    }

    /* !Magic methods */

    /**
     * Called to initialize the model
     *
     * @return  self
     */
    public function initialize(): self
    {
        $this->setSource('groups');

        return $this;   
    }
}

I added App\Model\Group::findFirst(1) to my index.php (of course, it doesn't live here normally):

// define the root directories
define('ROOT_PROJECT', realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR);
define('ROOT_APPLICATION', realpath(__DIR__ . '/../app/') . DIRECTORY_SEPARATOR);
define('ROOT_DOCUMENT', __DIR__ . DIRECTORY_SEPARATOR);

// include the vendor autoloader
require ROOT_PROJECT . 'vendor/autoload.php';

// include the application autoloader
require ROOT_APPLICATION . 'autoload.php';

// include the services configuration (defines $di)
require ROOT_APPLICATION . 'services/services.php';

// if the application is in development
if ($di->get('environment')->isDevelopment()) {
    (new \Phalcon\Debug())->listen();
}

// this causes a segmentation fault!
\App\Model\Group::findFirst(1);

// don't start the application; we're debugging stuff
// echo (new \Phalcon\Mvc\Application($di))->handle()->getContent();

When I go to my development site, the application crashes, and Safari displays an error message saying, "Safari can't open the page, because the server unexpectedly dropped the connection."

However, I can tail Apache's error logs and see PHP encountered a segmentation fault:

$ tail /var/log/apache2/error.log

[Sun Apr 24 17:56:33.085807 2016] [core:notice] [pid 17024] AH00051: child pid 17065 exit signal Segmentation fault (11), possible coredump in /etc/apache2

I tried to use gdb to troubleshoot the issue:

$ gdb php
(gdb) run "path/to/index.php"

Program received signal SIGSEGV, Segmentation fault.
0xb5e41e76 in zephir_call_user_function () from /usr/lib/php/20151012/phalcon.so

(gdb) bt
#0  0xb5e41e76 in zephir_call_user_function () from /usr/lib/php/20151012/phalcon.so
#1  0xb5e431cb in zephir_call_class_method_aparams () from /usr/lib/php/20151012/phalcon.so
#2  0xb5e44344 in zephir_create_instance_params () from /usr/lib/php/20151012/phalcon.so
#3  0xb5d01e20 in zim_Phalcon_Di_get () from /usr/lib/php/20151012/phalcon.so
#4  0x80287668 in execute_internal ()
#5  0x801edf3f in dtrace_execute_internal ()
#6  0xb5e42ef5 in zephir_call_user_function () from /usr/lib/php/20151012/phalcon.so
#7  0xb5e431cb in zephir_call_class_method_aparams () from /usr/lib/php/20151012/phalcon.so
#8  0xb5c5d0dd in zim_Phalcon_Mvc_Model_Manager_createBuilder () from /usr/lib/php/20151012/phalcon.so
#9  0x80287668 in execute_internal ()
#10 0x801edf3f in dtrace_execute_internal ()
#11 0xb5e42ef5 in zephir_call_user_function () from /usr/lib/php/20151012/phalcon.so
#12 0xb5e431cb in zephir_call_class_method_aparams () from /usr/lib/php/20151012/phalcon.so
#13 0xb5dd1cbf in zim_Phalcon_Mvc_Model_find () from /usr/lib/php/20151012/phalcon.so
#14 0x80287668 in execute_internal ()
#15 0x801edf3f in dtrace_execute_internal ()
#16 0x8027cd24 in ?? ()
#17 0x8023acfa in execute_ex ()
#18 0x801ede3d in dtrace_execute_ex ()
#19 0x80288fcb in zend_execute ()
#20 0x801fd6f0 in zend_execute_scripts ()
#21 0x8019cc76 in php_execute_script ()
#22 0x8028b060 in ?? ()
#23 0x80071187 in main ()

(gdb) f
#0  0xb5e41e76 in zephir_call_user_function () from /usr/lib/php/20151012/phalcon.so

(gdb) info f
Stack level 0, frame at 0xbfffb860:
 eip = 0xb5e41e76 in zephir_call_user_function; saved eip = 0xb5e431cb
 called by frame at 0xbfffb8d0
 Arglist at 0xbfffb858, args: 
 Locals at 0xbfffb858, Previous frame's sp is 0xbfffb860
 Saved registers:
  ebx at 0xbfffb84c, ebp at 0xbfffb858, esi at 0xbfffb850, edi at 0xbfffb854, eip at 0xbfffb85c

(gdb) info args
No symbol "args" in current context.

I did some sanity checking by making some changes to my index.php:


// continued from above...

// comment out the problem line
// \App\Model\Group::findFirst(1);

$group = new \App\Model\Group();
$meta  = $di->get('modelsMetadata');
$strategy = $meta->getStrategy();

echo '<p>' . get_class($meta) . '</p>';
echo '<p>' . get_class($strategy) . '</p>';
echo '<p>' . var_dump($strategy->getMetaData($group, $di)) . '</p>';

This produces the following output:

Phalcon\Mvc\Model\MetaData\Memory

Phalcon\Mvc\Model\MetaData\Strategy\Introspection

array(12) { [0]=> array(2) { [0]=> string(2) "id" [1]=> string(4) "name" } [1]=> array(1) { [0]=> string(2) "id" } [2]=> array(1) { [0]=> string(4) "name" } [3]=> array(2) { [0]=> string(2) "id" [1]=> string(4) "name" } [4]=> array(2) { ["id"]=> int(0) ["name"]=> int(2) } [5]=> array(1) { ["id"]=> bool(true) } [8]=> string(2) "id" [9]=> array(2) { ["id"]=> int(1) ["name"]=> int(2) } [10]=> array(0) { } [11]=> array(0) { } [12]=> array(0) { } [13]=> array(0) { } }

However, adding a call to getAttributes() causes a segmentation fault:


// continued from above...

// comment out the problem line
// \App\Model\Group::findFirst(1);

$group = new \App\Model\Group();
$meta  = $di->get('modelsMetadata');
$strategy = $meta->getStrategy();

echo '<p>' . get_class($meta) . '</p>';
echo '<p>' . get_class($strategy) . '</p>';
echo '<p>' . var_dump($strategy->getMetaData($group, $di)) . '</p>';

// add this line to produce a segmentation fault
echo '<p>' . var_dump($meta->getAttributes($group)) . '</p>'; 

Phew! Any ideas?

I'm sorry for the wall of text, but thank you for any help! PhalconPHP is super fast, but more importantly, it's just a great framework! Thank you for your hard work PhalconPHP Team!

EDIT: Whoops! I called it Model::find() instead of Model::findFirst() in my title and description here. Sorry for the confusion.



85.5k

Hi, its because of this : https://github.com/phalcon/zephir/issues/1218

It seems like under OSX its working, but for us doesnt work also ( including myself )

Actually im not sure its working under OSX beacause andreas didn't wrote that view was rendered :D

Hi, I compiled Zephir just a few minutes ago and recompiled Phalcon but when I try to use any model I'm getting the following error from the syslog:

segfault at 0 ip xxx sp xxx error 6 in phalcon.so[7fd942b28000+3a2000]

From the PostgreSQL log:

could not receive data from client: Connection reset by peer

Also I'm getting a 502 error from nginx

The code I'm trying to run is the following:

$owner = Owner::findFirst();

The model just has the code to set the conection on initialize like this:

class Owner extends Model
{
    public $email;

    public function initialize()
    {
        $this->setConnectionService('dbRoot');
    }

I'm using the following:

  • Ubuntu 15.04
  • Nginx 1.6.2
  • PostgreSQL 9.4.6
  • PHP 7.0.5
  • Zephir 0.9.2a-dev (Build on Apr 25 2016 12:34:21 using the master branch)
  • Phalcon 2.1.0-RC1 (Build on Apr 25 2016 12:58:05 using the master branch)

I don't know if this is related to my problem or if I need to open a new thread, but I think it has some relation.

Thanks in advance!



145.0k
Accepted
answer
edited Apr '16

It's common problem with php 7.0.5. Phalcon don't works with php 7 fully stable. Just use php 5.x. Your problem is related. You don't need to create new thread. It's just not working for now. Check this - https://github.com/phalcon/cphalcon/issues/11550

edited Apr '16

Ok, cool.

Thanks @Izo for the suggestion. I'm not sure if my issue is related to phalcon/zephir PHP7 create_instance_params() issue, because I don't have a problem rendering views without the findFirst() method. However, I'm new to Phalcon and Zephir. So, what do I know haha.

Thanks @Checkmater for chiming in. It's nice to know I'm not alone haha.

Thank you @Wojciech for the heads up. I didn't realize how close I'd wandered to the edge of development haha. I just wanted to be sure I hadn't missed something in my configuration. Do you think it would help the guys and gals to post my error, stacktrace, etc to the Github issue?

Finally, thank you Phalcon Team for a great framework!

You don't need to create another issue on github. I'm already doing everything that can be done here https://github.com/phalcon/cphalcon/issues/11550. Also create_instance_params is already fixed in newest zephir. But still there is problem with phql parser which is not fully ported to php 7 and it just not works(so sad).