Skip to content

Commit fa55358

Browse files
author
jiahao
committed
bugfix: added error handler for unimplemented event handlding.
1 parent ec0e8b5 commit fa55358

3 files changed

+32
-11
lines changed

src/ngx_http_lua_event.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern ngx_http_lua_event_actions_t ngx_http_lua_epoll;
2525
extern ngx_http_lua_event_actions_t ngx_http_lua_poll;
2626
extern ngx_http_lua_event_actions_t ngx_http_lua_kqueue;
2727

28+
extern int ngx_http_lua_event_inited;
2829

2930
#define ngx_http_lua_set_event ngx_http_lua_event_actions.set_event
3031
#define ngx_http_lua_clear_event ngx_http_lua_event_actions.clear_event
@@ -39,9 +40,6 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
3940

4041
ccf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
4142
if (ccf == NULL) {
42-
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
43-
"no \"events\" section in configuration");
44-
4543
return NGX_ERROR;
4644
}
4745

@@ -75,9 +73,6 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
7573
#endif
7674

7775
{
78-
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
79-
"invalid event type \"%V\"", ecf->name);
80-
8176
return NGX_ERROR;
8277
}
8378

src/ngx_http_lua_initworkerby.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "ngx_http_lua_event.h"
1717

1818

19+
int ngx_http_lua_event_inited = 0;
20+
1921
ngx_http_lua_event_actions_t ngx_http_lua_event_actions;
2022

2123
static u_char *ngx_http_lua_log_init_worker_error(ngx_log_t *log,
@@ -49,8 +51,8 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
4951
return NGX_OK;
5052
}
5153

52-
if (ngx_http_lua_init_event(cycle) == NGX_ERROR) {
53-
return NGX_ERROR;
54+
if (ngx_http_lua_init_event(cycle) == NGX_OK) {
55+
ngx_http_lua_event_inited = 1;
5456
}
5557

5658
/* lmcf != NULL && lmcf->lua != NULL */

src/ngx_http_lua_socket_tcp.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,19 @@ ngx_http_lua_socket_tcp(lua_State *L)
461461
return luaL_error(L, "no ctx found");
462462
}
463463

464-
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_COSOCKET);
464+
/* only a few events is suppported in init_worker_by_* */
465+
if (ngx_http_lua_event_inited) {
466+
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_COSOCKET);
467+
468+
} else if (ctx->context & NGX_HTTP_LUA_CONTEXT_BLOCKED_COSOCKET) {
469+
return luaL_error(L, "API disabled in the context of %s except when " \
470+
"using the event handling methods of poll, epoll " \
471+
"or kqueue",
472+
ngx_http_lua_context_name((ctx)->context));
473+
474+
} else {
475+
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_YIELDABLE);
476+
}
465477

466478
lua_createtable(L, 5 /* narr */, 1 /* nrec */);
467479
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
@@ -906,7 +918,19 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
906918
return luaL_error(L, "no ctx found");
907919
}
908920

909-
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_COSOCKET);
921+
/* only a few events is suppported in init_worker_by_* */
922+
if (ngx_http_lua_event_inited) {
923+
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_COSOCKET);
924+
925+
} else if (ctx->context & NGX_HTTP_LUA_CONTEXT_BLOCKED_COSOCKET) {
926+
return luaL_error(L, "API disabled in the context of %s except when " \
927+
"using the event handling methods of poll, epoll " \
928+
"or kqueue",
929+
ngx_http_lua_context_name((ctx)->context));
930+
931+
} else {
932+
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_YIELDABLE);
933+
}
910934

911935
luaL_checktype(L, 1, LUA_TTABLE);
912936

@@ -1805,7 +1829,7 @@ ngx_http_lua_socket_tcp_sslhandshake(lua_State *L)
18051829

18061830
rc = ngx_ssl_handshake(c);
18071831

1808-
dd("ngx_ssl_handshake returned %d", (int) rc);
1832+
dd("ngx_ssl_handshake returned %d", (int) rc);
18091833

18101834
if (rc == NGX_AGAIN) {
18111835
if (ctx->context & NGX_HTTP_LUA_CONTEXT_BLOCKED_COSOCKET) {

0 commit comments

Comments
 (0)