@@ -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