Skip to content

Commit 994c672

Browse files
author
jiahao
committed
bugfix: added error handler for unimplemented event handlding.
1 parent 5609bcd commit 994c672

File tree

3 files changed

+32
-11
lines changed

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
@@ -459,7 +459,19 @@ ngx_http_lua_socket_tcp(lua_State *L)
459459
return luaL_error(L, "no ctx found");
460460
}
461461

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

464476
lua_createtable(L, 5 /* narr */, 1 /* nrec */);
465477
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
@@ -904,7 +916,19 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
904916
return luaL_error(L, "no ctx found");
905917
}
906918

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

909933
luaL_checktype(L, 1, LUA_TTABLE);
910934

@@ -1796,7 +1820,7 @@ ngx_http_lua_socket_tcp_sslhandshake(lua_State *L)
17961820

17971821
rc = ngx_ssl_handshake(c);
17981822

1799-
dd("ngx_ssl_handshake returned %d", (int) rc);
1823+
dd("ngx_ssl_handshake returned %d", (int) rc);
18001824

18011825
if (rc == NGX_AGAIN) {
18021826
if (ctx->context & NGX_HTTP_LUA_CONTEXT_BLOCKED_COSOCKET) {

0 commit comments

Comments
 (0)