Skip to content

Commit

Permalink
bugfix: added error handler for unimplemented event handlding.
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahao committed Sep 3, 2021
1 parent ec0e8b5 commit fa55358
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
7 changes: 1 addition & 6 deletions src/ngx_http_lua_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern ngx_http_lua_event_actions_t ngx_http_lua_epoll;
extern ngx_http_lua_event_actions_t ngx_http_lua_poll;
extern ngx_http_lua_event_actions_t ngx_http_lua_kqueue;

extern int ngx_http_lua_event_inited;

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

ccf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
if (ccf == NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
"no \"events\" section in configuration");

return NGX_ERROR;
}

Expand Down Expand Up @@ -75,9 +73,6 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
#endif

{
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
"invalid event type \"%V\"", ecf->name);

return NGX_ERROR;
}

Expand Down
6 changes: 4 additions & 2 deletions src/ngx_http_lua_initworkerby.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "ngx_http_lua_event.h"


int ngx_http_lua_event_inited = 0;

ngx_http_lua_event_actions_t ngx_http_lua_event_actions;

static u_char *ngx_http_lua_log_init_worker_error(ngx_log_t *log,
Expand Down Expand Up @@ -49,8 +51,8 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
return NGX_OK;
}

if (ngx_http_lua_init_event(cycle) == NGX_ERROR) {
return NGX_ERROR;
if (ngx_http_lua_init_event(cycle) == NGX_OK) {
ngx_http_lua_event_inited = 1;
}

/* lmcf != NULL && lmcf->lua != NULL */
Expand Down
30 changes: 27 additions & 3 deletions src/ngx_http_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,19 @@ ngx_http_lua_socket_tcp(lua_State *L)
return luaL_error(L, "no ctx found");
}

ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_COSOCKET);
/* only a few events is suppported in init_worker_by_* */
if (ngx_http_lua_event_inited) {
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_COSOCKET);

} else if (ctx->context & NGX_HTTP_LUA_CONTEXT_BLOCKED_COSOCKET) {
return luaL_error(L, "API disabled in the context of %s except when " \
"using the event handling methods of poll, epoll " \
"or kqueue",
ngx_http_lua_context_name((ctx)->context));

} else {
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_YIELDABLE);
}

lua_createtable(L, 5 /* narr */, 1 /* nrec */);
lua_pushlightuserdata(L, ngx_http_lua_lightudata_mask(
Expand Down Expand Up @@ -906,7 +918,19 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
return luaL_error(L, "no ctx found");
}

ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_COSOCKET);
/* only a few events is suppported in init_worker_by_* */
if (ngx_http_lua_event_inited) {
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_COSOCKET);

} else if (ctx->context & NGX_HTTP_LUA_CONTEXT_BLOCKED_COSOCKET) {
return luaL_error(L, "API disabled in the context of %s except when " \
"using the event handling methods of poll, epoll " \
"or kqueue",
ngx_http_lua_context_name((ctx)->context));

} else {
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_YIELDABLE);
}

luaL_checktype(L, 1, LUA_TTABLE);

Expand Down Expand Up @@ -1805,7 +1829,7 @@ ngx_http_lua_socket_tcp_sslhandshake(lua_State *L)

rc = ngx_ssl_handshake(c);

dd("ngx_ssl_handshake returned %d", (int) rc);
dd("ngx_ssl_handshake returned %d", (int) rc);

if (rc == NGX_AGAIN) {
if (ctx->context & NGX_HTTP_LUA_CONTEXT_BLOCKED_COSOCKET) {
Expand Down

0 comments on commit fa55358

Please sign in to comment.