Skip to content

feature: support cosocket and other yieldable APIs in init_worker_by_lua* - #409

Draft
tzssangglass wants to merge 1 commit into
openresty:masterfrom
tzssangglass:feature/init-worker-cosocket
Draft

feature: support cosocket and other yieldable APIs in init_worker_by_lua*#409
tzssangglass wants to merge 1 commit into
openresty:masterfrom
tzssangglass:feature/init-worker-cosocket

Conversation

@tzssangglass

@tzssangglass tzssangglass commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Enable TCP/UDP cosockets, ngx.sleep, ngx.semaphore, ngx.thread.spawn, and coroutine.* inside init_worker_by_lua* by running a bounded event pump when the init code yields. Ported from ngx_http_lua_module and adapted to the stream subsystem.

Key changes:

  • Add ngx_stream_lua_init_worker_pump() to drive the event loop during yields, including the posted delayed-events queue drain
  • Add ngx_stream_lua_init_worker_pump_loop() with timeout budget management
  • Add ngx_stream_lua_init_worker_toggle_accept() to disarm/re-arm listeners around the pump (prevents level-triggered busy-spin from pending accepts; mirrors nginx's reuseport/EPOLLEXCLUSIVE arming)
  • Freeze ngx.timer.at/every registrations via ctx->context gate; flush at runner exit with absolute expiry preservation (timer ordering identical to before)
  • Run the init chunk on an entry coroutine created with ngx_stream_lua_new_thread() on the global VM; finalize via finalize_fake_request on completion
  • Add lua_init_worker_timeout directive (default 0 = no timeout)
  • Add lua_init_worker_abort_on_error directive (default off)
  • Add INIT_WORKER to NGX_STREAM_LUA_CONTEXT_YIELDABLE mask in util.h
  • Change runtime error prefix to "lua entry thread aborted:" (breaking)
  • Update README: new directives and cosocket availability note

sister PR: openresty/lua-nginx-module#2521

Enable TCP/UDP cosockets, ngx.sleep, ngx.semaphore, ngx.thread.spawn,
and coroutine.* inside init_worker_by_lua* by running a bounded event
pump when the init code yields. Ported from ngx_http_lua_module and
adapted to the stream subsystem.

Key changes:
- Add ngx_stream_lua_init_worker_pump() to drive the event loop during
 yields, including the posted delayed-events queue drain
- Add ngx_stream_lua_init_worker_pump_loop() with timeout budget
 management
- Add ngx_stream_lua_init_worker_toggle_accept() to disarm/re-arm
 listeners around the pump (prevents level-triggered busy-spin from
 pending accepts; mirrors nginx's reuseport/EPOLLEXCLUSIVE arming)
- Freeze ngx.timer.at/every registrations via ctx->context gate; flush
 at runner exit with absolute expiry preservation (timer ordering
 identical to before)
- Run the init chunk on an entry coroutine created with
 ngx_stream_lua_new_thread() on the global VM; finalize via
 finalize_fake_request on completion
- Add lua_init_worker_timeout directive (default 0 = no timeout)
- Add lua_init_worker_abort_on_error directive (default off)
- Add INIT_WORKER to NGX_STREAM_LUA_CONTEXT_YIELDABLE mask in util.h
- Change runtime error prefix to "lua entry thread aborted:" (breaking)
- Update README: new directives and cosocket availability note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant