Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event_base_free problems #3

Closed
Joungkyun opened this issue Jan 2, 2016 · 7 comments · Fixed by #5
Closed

event_base_free problems #3

Joungkyun opened this issue Jan 2, 2016 · 7 comments · Fixed by #5

Comments

@Joungkyun
Copy link

OS: Centos 7.2
PHP: 7.0.1
libevent: 1.4.3 or 2.0.21

The problems has 2 case. These cases has no problems under PHP 5.6.14 + mod_libevent-0.1.0.

_case 1_ codes:

<?php

function ec() { }

$sock = stream_socket_server ('tcp://0.0.0.0:2000', $errno, $errstr);
for ($i=0; $i<2; $i++) {
    $base = event_base_new();
    $ev = event_buffer_new($sock, NULL, NULL, 'ec');
    event_buffer_free($ev);
    event_base_free($base);
}
fclose ($sock);
?>

_expected:_ no result
_result:_ segfault

[root@an3 libevent]$ gdb php
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-80.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /usr/bin/php...(no debugging symbols found)...done.
(gdb) run z3.php
Starting program: /bin/php z3.php
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00007fffeae9ba07 in event_base_free () from /lib64/libevent-2.0.so.5
Missing separate debuginfos, use: debuginfo-install php-cli-7.0.1-1.an3.x86_64
(gdb) bt 1
#0  0x00007fffeae9ba07 in event_base_free () from /lib64/libevent-2.0.so.5
(More stack frames follow...)
(gdb) bt
#0  0x00007fffeae9ba07 in event_base_free () from /lib64/libevent-2.0.so.5
#1  0x00007fffeb0d39fd in _php_event_base_dtor (res=<optimized out>) at /root/rpmbuild/SOURCES/z/ext/libevent/libevent.c:153
#2  0x0000000000739341 in zend_resource_dtor ()
#3  0x00000000007393ca in list_entry_destructor ()
#4  0x0000000000735853 in zend_hash_index_del ()
#5  0x000000000079ec28 in execute_ex ()
#6  0x00000000007aa507 in zend_execute ()
#7  0x0000000000724553 in zend_execute_scripts ()
#8  0x00000000006c40b8 in php_execute_script ()
#9  0x00000000007ac125 in do_cli ()
#10 0x0000000000440dcf in main ()
(gdb) f 1
#1  0x00007fffeb0d39fd in _php_event_base_dtor (res=<optimized out>) at /root/rpmbuild/SOURCES/z/ext/libevent/libevent.c:153
153             event_base_free(base->base);
(gdb) list
148
149     ZEND_RSRC_DTOR_FUNC(_php_event_base_dtor) /* {{{ */
150     {
151             php_event_base_t *base = (php_event_base_t*)res->ptr;
152
153             event_base_free(base->base);
154             efree(base);
155     }
156     /* }}} */
157
(gdb)

_case 2_ codes:

<?php

function ec() { }

for ($i=0; $i<2; $i++) {
    $sock = stream_socket_server ('tcp://0.0.0.0:2000', $errno, $errstr);
    $base = event_base_new();
    $ev = event_buffer_new($sock, NULL, NULL, 'ec');
    event_buffer_free($ev);
    event_base_free($base);
    fclose ($sock);
}
?>

_expected:_ no result
_result:_ $sock variable is modulated after calling the first event_base_free.

PHP Warning:  event_buffer_new(): fd argument must be valid PHP stream or socket resource or a file descriptor of type long in /root/rpmbuild/SOURCES/z/ext/libevent/z3.php on line 8

Warning: event_buffer_new(): fd argument must be valid PHP stream or socket resource or a file descriptor of type long in /root/rpmbuild/SOURCES/z/ext/libevent/z3.php on line 8
PHP Warning:  event_buffer_free() expects parameter 1 to be resource, boolean given in /root/rpmbuild/SOURCES/z/ext/libevent/z3.php on line 9

Warning: event_buffer_free() expects parameter 1 to be resource, boolean given in /root/rpmbuild/SOURCES/z/ext/libevent/z3.php on line 9
PHP Warning:  fclose(): supplied resource is not a valid stream resource in /root/rpmbuild/SOURCES/z/ext/libevent/z3.php on line 11

Warning: fclose(): supplied resource is not a valid stream resource in /root/rpmbuild/SOURCES/z/ext/libevent/z3.php on line 11
@ichiriac
Copy link

ichiriac commented Jan 2, 2016

Could you fork my repository, add some phpt unit tests files and make a pull request. Future migrations will be easier

ichiriac added a commit that referenced this issue Jan 3, 2016
@ichiriac
Copy link

ichiriac commented Jan 4, 2016

Hi,

the bug comes from an allocation problem and the garbage collector, at the second pass, it destroys the event buffer BEFORE attaching it to the $ev.

After some investigations, it problably comes from a bad GC counter on the $base variable and an edge bug on php7.

I have no more time to work on this right now (I do this on my spare time), I'll come back on it maybe in a few weeks ...

@Joungkyun
Copy link
Author

solved this problem include case 1 and case 2. see also attached patch file.
And, confirm no memory leak with valgrind.

also, fixed compile error too. follow code has compile error.

-php_sock = (php_socket *)zend_fetch_resource_ex(fd, NULL, php_sockets_le_socket());
+php_sock = (php_socket *)zend_fetch_resource2_ex(fd, NULL, php_sockets_le_socket());

event_base_free__problems_no.3.diff.txt

P.S.
I have not yet accustomed github system. So instead of push, attach patch file. Thx.
The patch file has unix format(\n). On windows, use wordpad instead of notepad.

@ichiriac
Copy link

ichiriac commented Jan 9, 2016

@Joungkyun thanks for the patch, it works, i've released the code and tests according but seems to have some leaks :

https://travis-ci.org/expressif/pecl-event-libevent/jobs/101288331

To work with github click on "fork" button on the top of this page, a fork project will be created on your own profile.

Checkout your fork, make changes commit & push, and when you are ready, create a pull request from the pull requests tab (on your fork project).

Thanks again for your help,
Best Regards,
Ioan

@ichiriac ichiriac reopened this Jan 9, 2016
@Joungkyun
Copy link
Author

@ichiriac For libevent001 and libevent002, I think that PHP 7.1 environment seems to be the problem. Plez check again on PHP 7.0 environment.

And libevent003 seems there is a problem in event_base_free.

@ichiriac
Copy link

I'm actually using the master branch of PHP to run tests - it's in principle the 'stable' branch of latests release ... I've put an comment dirrectly on your pull request about the memory leaks, you can take a look at the travis logs.

By the way, thanks a lot helping me to solve this problem !

@Joungkyun
Copy link
Author

Is really master branch 'stable' branch of latests relelase? The master branch is PHP 7.1 stream and I know that PHP 7.1 stream is not stable. is the lastest stable stream PHP-7.0 branch?

ichiriac added a commit that referenced this issue Jan 27, 2016
refixed issue #3 and fixed issue #4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants