Skip to content

Instantly share code, notes, and snippets.

@dndx
Last active August 10, 2021 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dndx/8539137ddbdc804a31c1b667158c7efe to your computer and use it in GitHub Desktop.
Save dndx/8539137ddbdc804a31c1b667158c7efe to your computer and use it in GitHub Desktop.
semaphore_bug.lua
local sema = require("ngx.semaphore")
local s = sema.new()
local function do_post()
s:post()
--ngx.sleep(0)
print("posted")
end
ngx.timer.at(0, function()
ngx.timer.at(0, do_post) -- this fails
--do_post(s) -- this works
print(s:wait(5))
end)
@javierguerragiraldez
Copy link

just tried this, might be a bit better than pushing IO

diff --git a/spec/fixtures/mocks/lua-resty-dns/resty/dns/resolver.lua b/spec/fixtures/mocks/lua-resty-dns/resty/dns/resolver.lua
--- a/spec/fixtures/mocks/lua-resty-dns/resty/dns/resolver.lua	(revision 03093cc855ce014ff2cffbf90fe078196eb3d7c9)
+++ b/spec/fixtures/mocks/lua-resty-dns/resty/dns/resolver.lua	(date 1628582622545)
@@ -92,15 +92,22 @@
   if answer then
     -- we actually have a mock answer, return it
     ngx.log(ngx.DEBUG, LOG_PREFIX, "serving '", name, "' from mocks")
-    ngx.timer.at(0.05, function() ngx.log(ngx.INFO, "tick") end) -- FIXME: remove after OpenResty semaphore bug is fixed
     return answer, nil, tries
   end
 
   -- no mock, so invoke original resolver
   local a, b, c = old_query(self, name, options, tries)
-  ngx.timer.at(0.05, function() ngx.log(ngx.INFO, "tick") end) -- FIXME: remove after OpenResty semaphore bug is fixed
   return a, b, c
 end
 
+do
+  local semaphore = require "ngx.semaphore"
+  local old_post = semaphore.post
+  function semaphore.post(self, n)
+    old_post(self, n)
+    ngx.sleep(0)
+  end
+end
+
 
 return resolver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment