Commit 86b5f1e
authored
bugfix: prevent SIGSEGV in event timer rbtree during worker shutdown.
When a worker exits, ngx_worker_process_exit() calls
ngx_destroy_pool(cycle->pool), which fires the cycle's cleanup
handlers. One of these is ngx_http_lua_cleanup_vm, which calls
lua_close() on the worker's Lua VM. lua_close() runs LuaJIT GC
finalizers (__gc) on every live userdata, including TCP socket
cosocket userdata still bound to in-flight requests.
The __gc finalizers ngx_http_lua_socket_tcp_upstream_destroy and
ngx_http_lua_socket_downstream_destroy reach
ngx_http_lua_socket_tcp_finalize_{read,write}_part, which call
ngx_del_timer() on connection events. By that point the event
timer rbtree may already have been partially torn down by sibling
finalizers, so ngx_del_timer() corrupts the tree and the next
deletion segfaults inside ngx_rbtree_min / ngx_rbtree_delete.
Skip the cleanup when ngx_terminate or ngx_exiting is set: the
cycle pool is about to be destroyed and the kernel reclaims fds,
so unwinding events and timers from the __gc path is unnecessary
and unsafe. The same guard is already used in
ngx_http_lua_socket_tcp_setkeepalive() for the analogous case.
Reproduces under SIGTERM and SIGQUIT shutdown when there are
in-flight ngx.socket.tcp() / ngx.req.socket() coroutines, e.g.
long-lived ngx.sleep + tcpsock:receive() loops.
Crash signature:
ngx_rbtree_min src/core/ngx_rbtree.h
ngx_rbtree_delete src/core/ngx_rbtree.c
ngx_event_del_timer
ngx_http_lua_socket_tcp_finalize_read_part
ngx_http_lua_socket_tcp_finalize
ngx_http_lua_socket_tcp_cleanup
ngx_http_lua_socket_tcp_upstream_destroy (Lua __gc)
... LuaJIT GC sweep ...
ngx_http_lua_cleanup_vm
ngx_destroy_pool(cycle->pool)
ngx_worker_process_exit1 parent bbace59 commit 86b5f1e
1 file changed
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6137 | 6137 | | |
6138 | 6138 | | |
6139 | 6139 | | |
| 6140 | + | |
| 6141 | + | |
| 6142 | + | |
| 6143 | + | |
| 6144 | + | |
| 6145 | + | |
| 6146 | + | |
| 6147 | + | |
| 6148 | + | |
| 6149 | + | |
| 6150 | + | |
| 6151 | + | |
6140 | 6152 | | |
6141 | 6153 | | |
6142 | 6154 | | |
| |||
6158 | 6170 | | |
6159 | 6171 | | |
6160 | 6172 | | |
| 6173 | + | |
| 6174 | + | |
| 6175 | + | |
| 6176 | + | |
6161 | 6177 | | |
6162 | 6178 | | |
6163 | 6179 | | |
| |||
0 commit comments