Skip to content

Commit 0b0a3f7

Browse files
committed
fix ping worker leak
1 parent 4835455 commit 0b0a3f7

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Provide local connection pool like java
55

66
## Requirement
77

8-
- PHP 5.3 +
8+
- PHP 5.3 + (no zts)
99
- linux 2.6+
1010
- pdo and redis extension install
1111

connect_pool.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ static void cp_add_fail_into_mem(zval *conf, char *data_source);
6161
zval_ptr_dtor(&exception);\
6262
}while(0);
6363

64+
#define CP_TEST_RETURN_FALSE(flag) ({if(flag==CP_CONNECT_PING){ \
65+
if(EG(exception)){ \
66+
zval *exception = EG(exception);\
67+
zval_ptr_dtor(&exception); \
68+
EG(exception) = NULL;\
69+
}\
70+
return CP_FALSE; \
71+
}});
72+
6473
#define CP_SEND_EXCEPTION do{zval *str;CP_SEND_EXCEPTION_ARGS(&str);zval_ptr_dtor(&str);}while(0);
6574
#define CP_INTERNAL_NORMAL_SEND_RETURN(send_data)({CP_INTERNAL_NORMAL_SEND(send_data);return CP_TRUE;})
6675
#define CP_INTERNAL_ERROR_SEND_RETURN(send_data) ({ CP_INTERNAL_ERROR_SEND(send_data);return CP_FALSE;})
6776
#define CP_SEND_EXCEPTION_RETURN do{CP_SEND_EXCEPTION;return CP_FALSE;}while(0);
68-
69-
#define CP_TEST_RETURN_FALSE(flag) ({if(flag==CP_CONNECT_PING){EG(exception) = NULL;return CP_FALSE;}})
7077
#define CP_TEST_RETURN_TRUE(flag) ({if(flag==CP_CONNECT_PING)return CP_TRUE;})
7178

7279
#include "zend_exceptions.h"
@@ -406,12 +413,18 @@ int pdo_proxy_connect(zval *args, int flag) {
406413
if (zend_hash_find(Z_ARRVAL_P(args), ZEND_STRS("username"), (void **) &username) == SUCCESS)
407414
{
408415
tmp_pass[1] = username;
416+
} else
417+
{
418+
CP_INTERNAL_ERROR_SEND_RETURN("username null!");
409419
}
410420

411421
zval **password;
412422
if (zend_hash_find(Z_ARRVAL_P(args), ZEND_STRS("password"), (void **) &password) == SUCCESS)
413423
{
414424
tmp_pass[2] = password;
425+
} else
426+
{
427+
CP_INTERNAL_ERROR_SEND_RETURN("password null!");
415428
}
416429

417430
zval **options;
@@ -442,6 +455,8 @@ int pdo_proxy_connect(zval *args, int flag) {
442455
CP_SEND_EXCEPTION_RETURN;
443456
} else
444457
{
458+
if (flag == CP_CONNECT_PING)
459+
zval_ptr_dtor(&new_obj);
445460
CP_TEST_RETURN_TRUE(flag);
446461
//存起來
447462
if (zend_hash_add(&pdo_object_table, Z_STRVAL_PP(data_source), Z_STRLEN_PP(data_source), (void*) &new_obj, sizeof (zval *), NULL) == SUCCESS)
@@ -650,17 +665,23 @@ int redis_proxy_connect(zval *data_source, zval *args, int flag) {
650665

651666
if (zend_hash_find(CG(class_table), ZEND_STRS("redis"), (void **) &redis_ce) == FAILURE)
652667
{
653-
cpLog("redis class ce error\n");
668+
CP_INTERNAL_ERROR_SEND_RETURN("redis extension is not install");
654669
}
655670
object_init_ex(new_obj, *redis_ce);
656671

657672
if (zend_hash_index_find(Z_ARRVAL_P(ex_arr), 0, (void**) &ip) == SUCCESS)
658673
{
659674
tmp_pass[0] = ip;
675+
} else
676+
{
677+
CP_INTERNAL_ERROR_SEND_RETURN("redis ip null!");
660678
}
661679
if (zend_hash_index_find(Z_ARRVAL_P(ex_arr), 1, (void**) &port) == SUCCESS)
662680
{
663681
tmp_pass[1] = port;
682+
} else
683+
{
684+
CP_INTERNAL_ERROR_SEND_RETURN("redis ip null!");
664685
}
665686

666687
zval *timeout;
@@ -696,7 +717,13 @@ int redis_proxy_connect(zval *data_source, zval *args, int flag) {
696717
cp_add_fail_into_mem(args, Z_STRVAL_P(data_source));
697718
CP_SEND_EXCEPTION_RETURN;
698719
}
699-
CP_TEST_RETURN_TRUE(flag);
720+
if (flag == CP_CONNECT_PING)
721+
{
722+
zval_ptr_dtor(&new_obj);
723+
zval_ptr_dtor(&ex_arr);
724+
return CP_TRUE;
725+
}
726+
700727
if (zend_hash_index_find(Z_ARRVAL_P(ex_arr), 2, (void**) &db) == SUCCESS)
701728
{
702729
if (!cp_redis_select(new_obj, db))
@@ -826,11 +853,13 @@ static void cp_add_fail_into_mem(zval *o_arg, char *data_source) {
826853
zval *arr = CP_PING_GET_PRO(CPGL.ping_mem_addr);
827854
if (Z_TYPE_P(arr) == IS_NULL)
828855
{
829-
zval first_arr;
830-
array_init(&first_arr);
856+
zval *first_arr;
857+
MAKE_STD_ZVAL(first_arr);
858+
array_init(first_arr);
831859
add_assoc_long(args, "count", 1);
832-
add_assoc_zval(&first_arr, data_source, args);
833-
cp_ser_and_setpro(&first_arr);
860+
add_assoc_zval(first_arr, data_source, args);
861+
cp_ser_and_setpro(first_arr);
862+
zval_ptr_dtor(&first_arr);
834863
} else if (Z_TYPE_P(arr) != IS_BOOL)
835864
{
836865
zval **zval_source;

connect_pool_client.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ PHP_FUNCTION(get_disable_list) {
777777
zval *new_md5 = cpMD5(conf);
778778
if (memcmp(addr, Z_STRVAL_P(new_md5), CP_PING_MD5_LEN) == 0)
779779
{
780+
zval_ptr_dtor(&new_md5);
780781
zval *arr = CP_PING_GET_DIS(addr);
781782
if (Z_TYPE_P(arr) == IS_BOOL || Z_TYPE_P(arr) == IS_NULL)
782783
{
@@ -790,14 +791,11 @@ PHP_FUNCTION(get_disable_list) {
790791
} else
791792
{
792793
memcpy(addr, Z_STRVAL_P(new_md5), CP_PING_MD5_LEN);
794+
zval_ptr_dtor(&new_md5);
793795
int *pid = addr + CP_PING_MD5_LEN;
794796
if (*pid > 0)
795797
{
796-
int ret = kill(*pid, SIGUSR1); //清空disable和probably
797-
if (ret == -1)
798-
{
799-
zend_error(E_NOTICE, "kill failed, Error: %s [%d]", strerror(errno), errno);
800-
}
798+
kill(*pid, SIGUSR1); //清空disable和probably
801799
}
802800
array_init(return_value);
803801
}

cpPingWorker.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
static void cpPing_add_dislist(zval *dis_arr, zval **args, char *data_source) {
2020
if (Z_TYPE_P(dis_arr) == IS_NULL)
2121
{
22-
zval first_arr;
23-
array_init(&first_arr);
24-
add_assoc_zval(&first_arr, data_source, *args);
25-
cp_ser_and_setdis(&first_arr);
22+
zval *first_arr;
23+
MAKE_STD_ZVAL(first_arr);
24+
array_init(first_arr);
25+
add_assoc_zval(first_arr, data_source, *args);
26+
cp_ser_and_setdis(first_arr);
27+
zval_ptr_dtor(&first_arr);
2628
cpLog("'%s' insert into disable list", data_source);
2729
} else if (Z_TYPE_P(dis_arr) != IS_BOOL)
2830
{

0 commit comments

Comments
 (0)