Skip to content

Commit 957f29c

Browse files
authored
Merge pull request #43 from youzan/hotfix-error
add error output
2 parents c500656 + 4713d80 commit 957f29c

File tree

4 files changed

+126
-14
lines changed

4 files changed

+126
-14
lines changed

zan-extension/php_swoole.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
| zan@zanphp.io so we can mail you a copy immediately. |
1515
+----------------------------------------------------------------------+
1616
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
17-
| Zan Group <zan@zanphp.io> |
17+
| Zan Group <zan@zanphp.io> |
1818
+----------------------------------------------------------------------+
1919
*/
2020

@@ -459,6 +459,9 @@ void php_swoole_get_recv_data(zval *zdata, swEventData *req, char *header, uint3
459459
void php_swoole_onConnect(swServer *serv, swDataHead *);
460460
int php_swoole_onReceive(swServer *serv, swEventData *req);
461461
void php_swoole_onClose(swServer *, swDataHead *);
462+
void php_swoole_onWorkerStart(swServer *, int worker_id);
463+
void php_swoole_onWorkerStop(swServer *, int worker_id);
464+
void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo);
462465

463466
#define php_swoole_array_get_value(ht, str, v) (sw_zend_hash_find(ht, str, sizeof(str), (void **) &v) == SUCCESS && !ZVAL_IS_NULL(v))
464467
#define php_swoole_array_get_ptr_value(ht, str, v) (sw_zend_hash_find(ht, str, strlen(str)+1, (void **) &v) == SUCCESS && !ZVAL_IS_NULL(v))

zan-extension/swoole.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ const zend_function_entry zan_functions[] =
186186
{
187187
PHP_FE(swoole_version, NULL)
188188
PHP_FE(swoole_cpu_num, NULL)
189-
189+
190190
/*------nova_packet------*/
191191
PHP_FE(nova_decode, arginfo_nova_decode)
192192
PHP_FE(nova_encode, arginfo_nova_encode)
@@ -196,7 +196,7 @@ const zend_function_entry zan_functions[] =
196196
PHP_FE(nova_get_sequence, NULL)
197197
PHP_FE(nova_get_time, NULL)
198198
PHP_FE(nova_get_ip, NULL)
199-
199+
200200
/*------swoole_event-----*/
201201
PHP_FE(swoole_event_add, arginfo_swoole_event_add)
202202
PHP_FE(swoole_event_set, arginfo_swoole_event_set)
@@ -517,7 +517,7 @@ PHP_MINIT_FUNCTION(zan)
517517
#ifdef SW_USE_REDIS
518518
swoole_redis_init(module_number TSRMLS_CC);
519519
#endif
520-
520+
521521
swoole_aio_init(module_number TSRMLS_CC);
522522
swoole_process_init(module_number TSRMLS_CC);
523523
swoole_http_server_init(module_number TSRMLS_CC);

zan-extension/swoole_http_server.c

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ swString *swoole_zlib_buffer = NULL;
5656
#endif
5757
swString *swoole_http_form_data_buffer;
5858

59+
static http_context *current_ctx = NULL;
60+
5961
enum http_global_flag
6062
{
6163
HTTP_GLOBAL_GET = 1u << 1,
@@ -86,6 +88,10 @@ zend_class_entry *swoole_http_response_class_entry_ptr;
8688
static zend_class_entry swoole_http_request_ce;
8789
zend_class_entry *swoole_http_request_class_entry_ptr;
8890

91+
static void (*old_error_handler)(int, const char *, const uint, const char*, va_list);
92+
static void worker_error_handler(int error_num, const char *error_filename, const uint error_lineno, const char *format, va_list args);
93+
static void http_onWorkerStart(swServer *serv, int worker_id);
94+
static void http_onWorkerStop(swServer *serv, int worker_id);
8995
static int http_onReceive(swServer *serv, swEventData *req);
9096
static void http_onClose(swServer *serv, swDataHead *info);
9197

@@ -114,6 +120,22 @@ static int http_trim_double_quote(zval **value, char **ptr);
114120
#define http_strncasecmp(const_str, at, length) ((length >= sizeof(const_str)-1) &&\
115121
(strncasecmp(at, ZEND_STRL(const_str)) == 0))
116122

123+
#ifdef va_copy
124+
#define call_old_error_handler(error_num, error_filename, error_lineno, format, args) \
125+
{ \
126+
va_list copy; \
127+
va_copy(copy, args); \
128+
old_error_handler(error_num, error_filename, error_lineno, format, copy); \
129+
va_end(copy); \
130+
}
131+
#else
132+
#define call_old_error_handler(error_num, error_filename, error_lineno, format, args) \
133+
{ \
134+
old_error_handler(error_num, error_filename, error_lineno, format, args); \
135+
}
136+
#endif
137+
138+
117139
//header filed format,like:Content-Type
118140
static inline void http_header_key_format(char *key, int length)
119141
{
@@ -916,6 +938,19 @@ static int http_request_message_complete(php_http_parser *parser)
916938
return 0;
917939
}
918940

941+
static void http_onWorkerStart(swServer *serv, int worker_id)
942+
{
943+
old_error_handler = zend_error_cb;
944+
zend_error_cb = worker_error_handler;
945+
php_swoole_onWorkerStart(serv, worker_id);
946+
}
947+
948+
static void http_onWorkerStop(swServer *serv, int worker_id)
949+
{
950+
zend_error_cb = old_error_handler;
951+
php_swoole_onWorkerStop(serv, worker_id);
952+
}
953+
919954
static int http_onReceive(swServer *serv, swEventData *req)
920955
{
921956
if (swEventData_is_dgram(req->info.type))
@@ -1058,6 +1093,8 @@ static int http_onReceive(swServer *serv, swEventData *req)
10581093
}
10591094
}
10601095

1096+
current_ctx = ctx;
1097+
10611098
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
10621099
{
10631100
swError("onRequest handler error");
@@ -1068,6 +1105,8 @@ static int http_onReceive(swServer *serv, swEventData *req)
10681105
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
10691106
}
10701107

1108+
current_ctx = NULL;
1109+
10711110
//websocket user handshake
10721111
if (conn->websocket_status == WEBSOCKET_STATUS_HANDSHAKE)
10731112
{
@@ -1433,8 +1472,10 @@ static PHP_METHOD(swoole_http_server, start)
14331472
}
14341473
#endif
14351474

1436-
serv->onReceive = http_onReceive;
1437-
serv->onClose = http_onClose;
1475+
serv->onWorkerStart = http_onWorkerStart;
1476+
serv->onWorkerStop = http_onWorkerStop;
1477+
serv->onReceive = http_onReceive;
1478+
serv->onClose = http_onClose;
14381479

14391480
zval *zsetting = sw_zend_read_property(swoole_server_class_entry_ptr, getThis(), ZEND_STRL("setting"), 1 TSRMLS_CC);
14401481
int is_update = 0;
@@ -1464,7 +1505,7 @@ static PHP_METHOD(swoole_http_server, start)
14641505
if (serv->listen_list->open_websocket_protocol)
14651506
{
14661507
add_assoc_bool(zsetting, "open_websocket_protocol", 1);
1467-
}
1508+
}
14681509

14691510
if (is_update) {
14701511
zend_update_property(swoole_server_class_entry_ptr, getThis(), ZEND_STRL("setting"), zsetting TSRMLS_CC);
@@ -2369,3 +2410,74 @@ static PHP_METHOD(swoole_http_response, __destruct)
23692410
swoole_http_context_free(context TSRMLS_CC);
23702411
}
23712412
}
2413+
2414+
static void worker_error_handler(int error_num, const char *error_filename, const uint error_lineno, const char *format, va_list args)
2415+
{
2416+
SWOOLE_FETCH_TSRMLS;
2417+
2418+
zend_bool _old_in_compilation;
2419+
zend_execute_data *_old_current_execute_data;
2420+
2421+
_old_in_compilation = CG(in_compilation);
2422+
_old_current_execute_data = EG(current_execute_data);
2423+
2424+
if (!PG(modules_activated) || !EG(objects_store).object_buckets || !current_ctx) {
2425+
call_old_error_handler(error_num, error_filename, error_lineno, format, args);
2426+
return;
2427+
}
2428+
if (error_num == E_USER_ERROR ||
2429+
error_num == E_COMPILE_ERROR ||
2430+
error_num == E_CORE_ERROR ||
2431+
error_num == E_ERROR ||
2432+
error_num == E_PARSE) {
2433+
2434+
char buffer[1024] = {0};
2435+
size_t buffer_len;
2436+
2437+
#ifdef va_copy
2438+
va_list argcopy;
2439+
va_copy(argcopy, args);
2440+
buffer_len = vslprintf(buffer, sizeof(buffer)-1, format, argcopy);
2441+
va_end(argcopy);
2442+
#else
2443+
buffer_len = vslprintf(buffer, sizeof(buffer)-1, format, args);
2444+
#endif
2445+
if (buffer_len > sizeof(buffer) - 1 || buffer_len == (size_t)-1) {
2446+
buffer_len = sizeof(buffer) - 1;
2447+
}
2448+
2449+
current_ctx->response.status = 500;
2450+
zval *zresponse = current_ctx->response.zobject;
2451+
zval *function = NULL;
2452+
SW_MAKE_STD_ZVAL(function);
2453+
SW_ZVAL_STRING(function, "end", 1);
2454+
zval *retval = NULL;
2455+
if (!current_ctx->send_header) {
2456+
zval **args[1];
2457+
zval *buff;
2458+
SW_MAKE_STD_ZVAL(buff);
2459+
SW_ZVAL_STRINGL(buff, buffer, buffer_len, 1);
2460+
args[0] = &buff;
2461+
2462+
if (sw_call_user_function_ex(EG(function_table), &zresponse, function, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
2463+
{
2464+
swError("Write error msg failure");
2465+
}
2466+
sw_zval_ptr_dtor(&buff);
2467+
} else {
2468+
if (sw_call_user_function_ex(EG(function_table), &zresponse, function, &retval, 0, NULL, 0, NULL TSRMLS_CC) == FAILURE)
2469+
{
2470+
swError("Call response->end() failure");
2471+
}
2472+
}
2473+
if (retval) sw_zval_ptr_dtor(&retval);
2474+
sw_zval_ptr_dtor(&function);
2475+
}
2476+
zend_try {
2477+
call_old_error_handler(error_num, error_filename, error_lineno, format, args);
2478+
} zend_catch {
2479+
CG(in_compilation) = _old_in_compilation;
2480+
EG(current_execute_data) = _old_current_execute_data;
2481+
} zend_end_try();
2482+
}
2483+

zan-extension/swoole_server.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,6 @@ static void php_swoole_onShutdown(swServer *);
268268

269269
static int php_swoole_onPacket(swServer *, swEventData *);
270270

271-
static void php_swoole_onWorkerStart(swServer *, int worker_id);
272-
static void php_swoole_onWorkerStop(swServer *, int worker_id);
273-
static void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo);
274271
static void php_swoole_onUserWorkerStart(swServer *serv, swWorker *worker);
275272

276273
static int php_swoole_onTask(swServer *, swEventData *task);
@@ -728,7 +725,7 @@ static int php_swoole_onPacket(swServer *serv, swEventData *req)
728725
return SW_OK;
729726
}
730727

731-
static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
728+
void php_swoole_onWorkerStart(swServer *serv, int worker_id)
732729
{
733730

734731
SWOOLE_FETCH_TSRMLS;
@@ -787,7 +784,7 @@ static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
787784
}
788785
}
789786

790-
static void php_swoole_onWorkerStop(swServer *serv, int worker_id)
787+
void php_swoole_onWorkerStop(swServer *serv, int worker_id)
791788
{
792789

793790
SWOOLE_FETCH_TSRMLS;
@@ -901,7 +898,7 @@ static int php_swoole_onTask(swServer *serv, swEventData *req)
901898
}
902899

903900

904-
static void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo)
901+
void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo)
905902
{
906903

907904
SWOOLE_FETCH_TSRMLS;
@@ -1902,7 +1899,7 @@ PHP_METHOD(swoole_server, set)
19021899

19031900
sw_zend_call_method_with_1_params(&port_object, swoole_server_port_class_entry_ptr, NULL, "set", &retval, zset);
19041901
zend_update_property(swoole_server_class_entry_ptr, zobject, ZEND_STRL("setting"), zset TSRMLS_CC);
1905-
1902+
19061903
sw_zval_ptr_dtor(&zset);
19071904

19081905
RETURN_TRUE;

0 commit comments

Comments
 (0)