Skip to content

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

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

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

Conversation

@tzssangglass

@tzssangglass tzssangglass commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Enable TCP/UDP cosockets, ngx.sleep, ngx.semaphore, ngx.thread.spawn, coroutine., and ngx.run_worker_thread inside init_worker_by_lua by running a bounded event pump when the init code yields.

Key changes:

  • Add ngx_http_lua_init_worker_pump() to drive the event loop during yields
  • Add ngx_http_lua_init_worker_pump_loop() with timeout budget management
  • Add ngx_http_lua_init_worker_toggle_accept() to disarm/re-arm listeners around the pump (prevents level-triggered busy-spin from pending accepts)
  • Freeze ngx.timer.at registrations via ctx->context gate; flush at runner exit with absolute expiry preservation (timer ordering identical to before)
  • 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_HTTP_LUA_CONTEXT_YIELDABLE mask in util.h
  • Change runtime error prefix to "lua entry thread aborted:" (breaking)
  • Update README: new directives, cosocket availability, 118 API context lines

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.

sister PR: openresty/stream-lua-nginx-module#409

@tzssangglass tzssangglass changed the title feat: support cosocket and other yieldable APIs in init_worker_by_lua* feature: support cosocket and other yieldable APIs in init_worker_by_lua* Sep 6, 2026
@tzssangglass
tzssangglass force-pushed the init-worker-cosocket-runner branch 2 times, most recently from 47e76c4 to 9b1ac2f Compare September 6, 2026 07:29
Enable TCP/UDP cosockets, ngx.sleep, ngx.semaphore, ngx.thread.spawn,
coroutine.*, and ngx.run_worker_thread inside init_worker_by_lua* by
running a bounded event pump when the init code yields.

Key changes:
- Add ngx_http_lua_init_worker_pump() to drive the event loop during yields
- Add ngx_http_lua_init_worker_pump_loop() with timeout budget management
- Add ngx_http_lua_init_worker_toggle_accept() to disarm/re-arm listeners
 around the pump (prevents level-triggered busy-spin from pending accepts)
- Freeze ngx.timer.at registrations via ctx->context gate; flush at runner
 exit with absolute expiry preservation (timer ordering identical to before)
- 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_HTTP_LUA_CONTEXT_YIELDABLE mask in util.h
- Change runtime error prefix to "lua entry thread aborted:" (breaking)
- Update README: new directives, cosocket availability, 118 API context lines
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