# Ngx\_lua post data error/bug?

**URL:** <https://www.ruby-forum.com/t/ngx-lua-post-data-error-bug/213058>\
**Category:** NGINX\
**Created:** [October 21, 2011, 8:07pm UTC](https://www.ruby-forum.com/t/ngx-lua-post-data-error-bug/213058 "2011-10-21T20:07:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [October 21, 2011, 8:07pm UTC](https://www.ruby-forum.com/t/ngx-lua-post-data-error-bug/213058/1 "2011-10-21T20:07:11Z")

</div>

This is a follow on from:  
[http://mailman.nginx.org/pipermail/nginx/2011-October/029905.html](http://mailman.nginx.org/pipermail/nginx/2011-October/029905.html)

The conditional statements take care of the basic error but what  
happens is that when “post” requests match any of the test regexes, an  
error is thrown …  
“lua handler aborted: runtime error: attempt to yield across  
metamethod/C-call boundary blah, blah, blah”.

It appears this is due to a limitation in Lua 5.1 and this yeilding is  
a major item for Lua 5.2. However, trying to build Nginx with the lua  
module against Lua 5.2 fails…

```
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx_http_lua_conf.c

```

/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:  
In function ‘ngx\_http\_lua\_new\_thread’:  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:189:  
error: ‘LUA\_GLOBALSINDEX’ undeclared (first use in this function)  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:189:  
error: (Each undeclared identifier is reported only once  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:189:  
error: for each function it appears in.)  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:  
In function ‘ngx\_http\_lua\_del\_thread’:  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:228:  
warning: implicit declaration of function ‘lua\_getfenv’  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:234:  
warning: implicit declaration of function ‘lua\_setfenv’  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:  
In function ‘init\_ngx\_lua\_globals’:  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:507:  
error: ‘LUA\_GLOBALSINDEX’ undeclared (first use in this function)  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:  
In function ‘ngx\_http\_lua\_discard\_bufs’:  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:544:  
warning: unused parameter ‘pool’  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:  
In function ‘ngx\_http\_lua\_set\_multi\_value\_table’:  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_util.c:1167:  
warning: implicit declaration of function ‘lua\_objlen’  
make[1]: \*\*\* [objs/addon/src/ngx\_http\_lua\_util.o] Error 1  
make[1]: \*\*\* Waiting for unfinished jobs…  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_conf.c:  
In function ‘ngx\_http\_lua\_merge\_loc\_conf’:  
/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc17-0/src/ngx\_http\_lua\_conf.c:95:  
warning: unused parameter ‘cf’  
make[1]: Leaving directory `/usr/src/redhat/BUILD/nginx-1.0.8’  
make: \*\*\* [build] Error 2  
error: Bad exit status from /var/tmp/rpm-tmp.63523 (%build)

The upshot is that perhaps only LuaJIT 2.0 can be used for all  
features in recent versions of ngx\_lua.

Luckily, I took a backup of my server state so I’ll be rolling back  
and commenting out the “post” block.

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 21, 2011, 9:43pm UTC](https://www.ruby-forum.com/t/ngx-lua-post-data-error-bug/213058/2 "2011-10-21T21:43:45Z")

</div>

On 21 October 2011 21:06, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is a follow on from:  
> [lua\_need\_request\_body ... limitations?](http://mailman.nginx.org/pipermail/nginx/2011-October/029905.html)
> 
> The conditional statements take care of the basic error but what  
> happens is that when “post” requests match any of the test regexes, an  
> error is thrown …  
> “lua handler aborted: runtime error: attempt to yield across  
> metamethod/C-call boundary blah, blah, blah”.
> 
> It appears this is due to a limitation in Lua 5.1 …

Testing shows that the error is generated because of the  
‘dofile(“/etc/nginx/regex\_rules.lua”)’ line.

I.E., when the regex rules are placed directly in the  
access\_by\_lua\_file, a hit on a regex is processed as expected.  
When the rules are included using the dofile function, a hit on a  
regex results in the "attempt to yield across

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 22, 2011, 3:26am UTC](https://www.ruby-forum.com/t/ngx-lua-post-data-error-bug/213058/3 "2011-10-22T03:26:08Z")

</div>

On Sat, Oct 22, 2011 at 3:43 AM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Testing shows that the error is generated because of the  
> ‘dofile(“/etc/nginx/regex\_rules.lua”)’ line.

Lua’s “dofile” builtin is implemented as a C function in both Lua 5.1  
and LuaJIT 2.0. And when you call ngx.location.capture or ngx.exec or  
ngx.exit or ngx.req.read\_body or something like those in the  
regex\_rules.lua, it’ll effectively initiate a coroutine yield and that  
yield will run across C function boundary, which is disallowed.

I think it’s more efficient to use Lua modules and the “require”  
builtin because dofile will load the .lua file from disk at _every_  
request and it’s quite costy while “require” will only load the .lua  
module file only once unless lua\_code\_cache is turned off. Here is an  
example,

```
-- regex_rules.lua
module("regex_rules", package.seeall)
function check()
    ngx.req.read_body()
    -- other processing goes here...
end

# nginx.conf
lua_package_path 

```

“/path/to/regex\_rules.lua’s-parent-directory/?.lua;;”  
server {  
location /foo {  
access\_by\_lua ’  
local regex\_rules = require “regex\_rules”  
regex\_rules.check()  
';  
# content handler config goes here…  
}  
}

BTW, compatibility with Lua 5.2 is not maintained by ngx\_lua (yet).

Regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 22, 2011, 8:31am UTC](https://www.ruby-forum.com/t/ngx-lua-post-data-error-bug/213058/4 "2011-10-22T08:31:49Z")

</div>

On 22 October 2011 04:25, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> yield will run across C function boundary, which is disallowed.  
> ngx.req.read\_body()  
> ';  
> # content handler config goes here…  
> }  
> }
> 
> BTW, compatibility with Lua 5.2 is not maintained by ngx\_lua (yet).

Magic … Works perfectly.

You are a legend!
