openresty / lua-resty-limit-traffic

Lua library for limiting and controlling traffic in OpenResty/ngx_lua
819 stars 150 forks source link

Thread safety #35

Closed esoterix closed 6 years ago

esoterix commented 6 years ago

Inside req.lua, rec_cdata is declared at file scope. The comment above it says: we can share the cdata here since we only need it temporarily for serialization inside the shared dict

As you know, file scope variables can be shared between nginx processes if you 'require' the lua files using 'init_by_lua_block'. Along with that, I logged the rec_cdata pointer like so:

ngx.log(ngx.ERR, "pid=", tostring(ngx.var.pid), " rec_cdata=", tostring(rec_cdata))

and it showed that the same pointer was being used by all worker processes.

agentzh commented 6 years ago

@esoterix nginx worker processes are single-threaded. So it is safe when there is no yielding (Lua coroutine yielding) in the middle of each individual use cycle of that shared Lua cdata.