Segmentation fault

82 views
Skip to first unread message

老王

unread,
Aug 15, 2011, 5:58:12 AM8/15/11
to mongodb-user
I use php5.3.6, mongodb1.8.2 (show result in pastebin.com:
http://pastebin.com/raw.php?i=kHPNQV8u)

[root@imobile-test4-181 local]# gdb php
(gdb) run /home/codebase/toolbox/mongodb.php
Starting program: /bin/php /home/codebase/toolbox/mongodb.php
warning: no loadable sections found in added symbol-file system-
supplied DSO at 0x2aaaaaaab000
[Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault.
0x00000000005891ed in mongo_db__create_fake_cursor (current=0x1a5ddf0,
cmd=0x1a63b48) at /root/php-5.3.6/ext/mongo/db.c:567
567 memcpy(".$cmd", cursor->ns+strlen(current->db), 5);
(gdb) bt
#0 0x00000000005891ed in mongo_db__create_fake_cursor
(current=0x1a5ddf0, cmd=0x1a63b48) at /root/php-5.3.6/ext/mongo/db.c:
567
#1 0x00000000005892f0 in mongo_db_cmd (current=0x1a5ddf0,
cmd=0x1a63b48) at /root/php-5.3.6/ext/mongo/db.c:603
#2 0x0000000000594836 in mongo_util_server__other_le
(server=0x1a5ddf0) at /root/php-5.3.6/ext/mongo/util/server.c:186
#3 0x0000000000594a28 in mongo_util_server__get_info
(server=0x1a5ddf0) at /root/php-5.3.6/ext/mongo/util/server.c:239
#4 0x0000000000594ef6 in mongo_util_server_get_bson_size
(server=0x1a5d058) at /root/php-5.3.6/ext/mongo/util/server.c:133
#5 0x0000000000587934 in zim_MongoCollection_insert (ht=<value
optimized out>, return_value=0x1a5f298, return_value_ptr=<value
optimized out>, this_ptr=0x1c44130, return_value_used=<value optimized
out>)
at /root/php-5.3.6/ext/mongo/collection.c:417
#6 0x00002aaaaf5d836d in xdebug_execute_internal
(current_execute_data=0x2aaab380b1e0, return_value_used=0) at /home/
codebase/software/xdebug/xdebug.c:1379
#7 0x00000000007346a5 in zend_do_fcall_common_helper_SPEC
(execute_data=0x2aaab380b1e0) at /root/php-5.3.6/Zend/
zend_vm_execute.h:318
#8 0x0000000000733e5c in execute (op_array=0x1ac56d0) at /root/
php-5.3.6/Zend/zend_vm_execute.h:107
#9 0x00002aaaaf5d7fb0 in xdebug_execute (op_array=0x1ac56d0) at /home/
codebase/software/xdebug/xdebug.c:1308
#10 0x0000000000734382 in zend_do_fcall_common_helper_SPEC
(execute_data=0x2aaab380b050) at /root/php-5.3.6/Zend/
zend_vm_execute.h:340
#11 0x0000000000733e5c in execute (op_array=0x1a5b3d0) at /root/
php-5.3.6/Zend/zend_vm_execute.h:107
#12 0x00002aaaaf5d7fb0 in xdebug_execute (op_array=0x1a5b3d0) at /home/
codebase/software/xdebug/xdebug.c:1308
#13 0x000000000070e1fd in zend_execute_scripts (type=8, retval=0x0,
file_count=3) at /root/php-5.3.6/Zend/zend.c:1202
#14 0x00000000006bdb8d in php_execute_script
(primary_file=0x7fffffffe780) at /root/php-5.3.6/main/main.c:2291
#15 0x000000000079379c in main (argc=2, argv=0x7fffffffe9f8) at /root/
php-5.3.6/sapi/cli/php_cli.c:1193
(gdb) quit

老王

unread,
Aug 15, 2011, 6:05:02 AM8/15/11
to mongodb-user
# grep -i -E 'fail|error' /var/log/messages
Aug 15 18:01:36 imobile-test4-181 kernel: php[24339]: segfault at
0000000000e15e27 rip 00000000005891ed rsp 00007fffceda88f0 error 7

is this a kernel error?

shanshan

unread,
Aug 16, 2011, 2:27:49 AM8/16/11
to mongodb-user
This is a bug indeed in php-mongo-driver.
we need to change the line 567th in db.c from
memcpy(".$cmd", cursor->ns+strlen(current->db), 5);
to
memcpy(cursor->ns+strlen(current->db),".$cmd",5);
then rebuild the library, then it will ok.

Xavier

unread,
Aug 16, 2011, 8:29:33 AM8/16/11
to mongodb-user
Hi,

I changed the line 567th to memcpy(cursor->ns
+strlen(current->db),".$cmd",5);
Then I recompiled php 5.3.6 with the php-mongo-driver extension , but
I still got the same segmentation fault error.

Did I miss something?

老王

unread,
Aug 16, 2011, 9:19:22 PM8/16/11
to mongodb-user
@shanshan: thanks, it worked. @Xavier: u may need to do: "make clean"
at first.

老王

unread,
Aug 18, 2011, 5:00:33 AM8/18/11
to mongodb-user

Xavier

unread,
Aug 18, 2011, 12:40:21 PM8/18/11
to mongodb-user
Thanks guys, I did exactly what you wrote, with the mongo folder in
the ext/ folder, and the db.c file modified, then:

sudo make clean
rm configure
buildconf --force
sudo make
sudo make install

But still the segmentation fault error is here when doing a foreach
loop on a mongo collection...

Any idea what could be wrong?


On Aug 18, 11:00 am, 老王 <hi.laow...@gmail.com> wrote:
> https://github.com/mongodb/mongo-php-driver/commit/74c79421d786f1c8b6...

Xavier

unread,
Aug 22, 2011, 9:21:21 AM8/22/11
to mongodb-user
Anyone?

JMChia

unread,
Aug 22, 2011, 9:43:38 AM8/22/11
to mongodb-user
Hi, take a look to this

https://jira.mongodb.org/browse/PHP-256

I get segmentation fault too with 'insert' calls but with 'save' don't

Xavier

unread,
Aug 22, 2011, 10:30:35 AM8/22/11
to mongodb-user
Thanks but this is a different issue. Just got segmentation fault when
doing a foreach loop on a mongo collection.

In previous replies, a solution has been given, with the modification
of the db.c file, unfortunately in my situation it has changed
anything.

JMChia

unread,
Aug 22, 2011, 10:50:22 AM8/22/11
to mongodb-user
Maybe it's a driver error: https://jira.mongodb.org/browse/PHP-256

Have you tried with 1.2.2 ?

Xavier

unread,
Aug 23, 2011, 4:06:56 AM8/23/11
to mongodb-user
I got the same error whatever the version of mongodb used...

Kristina Chodorow

unread,
Aug 23, 2011, 11:24:13 AM8/23/11
to mongod...@googlegroups.com
Can you run it under gdb and, when it segfaults, run bt? E.g.,

$ gdb --args php myTest.php
...
gdb> r

Then, when it segfaults:

gdb> bt

and send that output?


2011/8/23 Xavier <xavier.p...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Xavier

unread,
Aug 23, 2011, 1:45:28 PM8/23/11
to mongodb-user
Ok here are the results. And it's kind of strange, seems like we got
some ibm_db2 compatibility issue:

Program received signal SIGSEGV, Segmentation fault.
_php_ibm_db2_conn (le=0x12aa920) at /home/xavier/temp/php-5.3.7/ext/
ibm_db2/ibm_db2.c:645
645 if (conn_res->flag_transaction == 1) {
(gdb) bt
#0 _php_ibm_db2_conn (le=0x12aa920) at /home/xavier/temp/php-5.3.7/
ext/ibm_db2/ibm_db2.c:645
#1 0x00000000007cc1b5 in zend_hash_apply (ht=0xfa78b8,
apply_func=0x576090 <_php_ibm_db2_conn>) at /home/xavier/temp/
php-5.3.7/Zend/zend_hash.c:674
#2 0x0000000000571e3e in zm_deactivate_ibm_db2 (type=<value optimized
out>, module_number=<value optimized out>) at /home/xavier/temp/
php-5.3.7/ext/ibm_db2/ibm_db2.c:663
#3 0x00000000007c16ac in module_registry_cleanup (module=<value
optimized out>) at /home/xavier/temp/php-5.3.7/Zend/zend_API.c:2168
#4 0x00000000007cc084 in zend_hash_reverse_apply (ht=0xfa7ec0,
apply_func=0x7c1690 <module_registry_cleanup>) at /home/xavier/temp/
php-5.3.7/Zend/zend_hash.c:757
#5 0x00000000007c00dd in zend_deactivate_modules () at /home/xavier/
temp/php-5.3.7/Zend/zend.c:867
#6 0x000000000076c945 in php_request_shutdown (dummy=<value optimized
out>) at /home/xavier/temp/php-5.3.7/main/main.c:1614
#7 0x0000000000849478 in main (argc=<value optimized out>,
argv=<value optimized out>) at /home/xavier/temp/php-5.3.7/sapi/cli/
php_cli.c:1363


As you can see it mainly complains about ibm_db2, but our php script
DOES NOT uses the ibm_db2 driver at all !

Here is our php script:

$m = new Mongo();
$db = $m->test;
$collection = $db->objects;

$URIs = array(); // PUT SOME URL STRINGS IN THERE , e.g:
array("http://www.example.com", "http://www.example2.com", etc)

$jsonStr_1 = '{"sID":"93", "img":{"$in": ["'.(implode('","',
$URIs)).'"]}}';
$jsonStr_2 = '{"_id":1, "img":1}';

$arr_1 = json_decode($jsonStr_1, true); $arr_2 =
json_decode($jsonStr_2, true);

$res = $collection->find($arr_1, $arr_2);


foreach ($res as $item) var_dump($item);

// THIS LAST LINE IS RESPONSIBLE FOR THE SEGMENTATION FAULT ERROR.

I have even tried an alternative to the foreach loop :

$count = $res->count();

for($i=1; $i<=$count;$i++) {
$item = $res->getNext();
var_dump($item);
}

But still got the same issue.

I am wondering what could be wrong between the mongo and ibm_db2
drivers extensions ?

On Aug 23, 5:24 pm, Kristina Chodorow <krist...@10gen.com> wrote:
> Can you run it under gdb and, when it segfaults, run bt? E.g.,
>
> $ gdb --args php myTest.php
> ...
> gdb> r
>
> Then, when it segfaults:
>
> gdb> bt
>
> and send that output?
>
> 2011/8/23 Xavier <xavier.prudho...@gmail.com>

Kristina Chodorow

unread,
Aug 23, 2011, 1:59:07 PM8/23/11
to mongod...@googlegroups.com
Weird...  he MongoDB driver could be stomping on memory ibm_db2 is using, maybe.  Can you try not loading the db2 extension and see if that makes a difference?


2011/8/23 Xavier <xavier.p...@gmail.com>

Xavier

unread,
Aug 23, 2011, 2:14:05 PM8/23/11
to mongodb-user
How do I unload a module if this one has been compiled with php ?

I guess I have to re build and install php without including the
ibm_db2 extension...

On Aug 23, 7:59 pm, Kristina Chodorow <krist...@10gen.com> wrote:
> Weird... he MongoDB driver could be stomping on memory ibm_db2 is using,
> maybe. Can you try not loading the db2 extension and see if that makes a
> difference?
>
> 2011/8/23 Xavier <xavier.prudho...@gmail.com>

Xavier

unread,
Aug 23, 2011, 2:26:11 PM8/23/11
to mongodb-user
Ok I have rebuild and recompiled php without including the ibm_db2
driver extension , and you got it right, it works perfectly now.

Thus the issue is as you said, the mongodb driver is most probably
stompinb on the ibm_db2 driver memory...

What can be done to fix this issue? We really need both extensions...

Kristina Chodorow

unread,
Aug 23, 2011, 2:34:34 PM8/23/11
to mongod...@googlegroups.com
Let me take a look, maybe it'll be reproducible.


2011/8/23 Xavier <xavier.p...@gmail.com>

Xavier

unread,
Aug 23, 2011, 2:36:55 PM8/23/11
to mongodb-user
Thanks Kristina =)

I'll be waiting for good news from you .

On Aug 23, 8:34 pm, Kristina Chodorow <krist...@10gen.com> wrote:
> Let me take a look, maybe it'll be reproducible.
>
> 2011/8/23 Xavier <xavier.prudho...@gmail.com>

Kristina Chodorow

unread,
Aug 23, 2011, 5:53:30 PM8/23/11
to mongod...@googlegroups.com
Unfortunately, it looks like db2 is the one doing the stomping. 

PHP keeps a list of persistent bits of memory that is shared by all extensions.  The DB2 extension is going through the entire list and treating everything as a struct allocated by DB2.  However, the Mongo extension uses that list, too!  DB2 is trying to destroy all of Mongo's persistent structs at the end of each request.  You could file a bug with the DB2 extension, I don't know how active development is.

You might also be able to fix it yourself by adding:

if (le->type != le_pconn_struct) {
  return ZEND_HASH_APPLY_KEEP;
}

right before the line that segfaulted ( if (conn_res->flag_transaction == 1) { ), so you'd end up with:


static int _php_ibm_db2_conn (zend_rsrc_list_entry *le TSRMLS_DC)
{
    conn_handle *conn_res;
    int rc = 0;

    conn_res = (conn_handle *) le->ptr;

    if (le->type != le_pconn_struct) {
        return ZEND_HASH_APPLY_KEEP;

    }

    if (conn_res->flag_transaction == 1) {
        conn_res->flag_transaction = 0;
        if( conn_res->handle_active && conn_res->flag_pconnect ) {


in ibm_db2.c.  This makes DB2 only destroy structs DB2 "owns."  (Disclaimer: I'm not positive this will work, but it should.)

Xavier

unread,
Aug 23, 2011, 7:32:39 PM8/23/11
to mongodb-user
Yeah Thanks it works... You're a genius :)

I will report this bug to the ibm_db2 driver developer team.

Thanks again !!
> ...
>
> read more »

Kristina Chodorow

unread,
Aug 24, 2011, 11:17:29 AM8/24/11
to mongod...@googlegroups.com
Thanks :)
Reply all
Reply to author
Forward
0 new messages