Skip to content

Commit a74462e

Browse files
committed
http/util: Add workaround for broken coroutine.wrap in openresty
Closes #98
1 parent 2f70b4a commit a74462e

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ UNRELEASED
22

33
- Fix incorrect Sec-WebSocket-Protocol negotiation
44
- Fix incorrect timeout handling in `websocket:receive()`
5+
- Add workaround to allow being required in openresty (#98)
56

67

78
0.2 - 2017-05-28

http/util.lua

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,29 @@ local maybe_quote do
188188
end
189189
end
190190

191-
-- A pcall relative that can be yielded over in PUC 5.1
191+
-- A pcall-alike function that can be yielded over even in PUC 5.1
192192
local yieldable_pcall
193-
-- See if pcall can be yielded over
194-
if coroutine.wrap(function() return pcall(coroutine.yield, true) end)() then
193+
--[[ If pcall can already yield, then we want to use that.
194+
195+
However, we can't do the feature check straight away, Openresty breaks
196+
coroutine.wrap in some contexts. See #98
197+
Openresty nominally only supports LuaJIT, which always supports a yieldable
198+
pcall, so we short-circuit the feature check by checking if the 'ngx' library
199+
is loaded, plus that jit.version_num indicates LuaJIT 2.0.
200+
This combination ensures that we don't take the wrong branch if:
201+
- lua-http is being used to mock the openresty environment
202+
- openresty is compiled with something other than LuaJIT
203+
]]
204+
if (
205+
package.loaded.ngx
206+
and type(package.loaded.jit) == "table"
207+
and type(package.loaded.jit.version_num) == "number"
208+
and package.loaded.jit.version_num >= 20000
209+
)
210+
-- See if pcall can be yielded over
211+
or coroutine.wrap(function()
212+
return pcall(coroutine.yield, true) end
213+
)() then
195214
yieldable_pcall = pcall
196215
else
197216
local function handle_resume(co, ok, ...)

0 commit comments

Comments
 (0)