Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage http.server as plugin after error #6

Closed
spigell opened this issue Jun 1, 2019 · 2 comments
Closed

Usage http.server as plugin after error #6

spigell opened this issue Jun 1, 2019 · 2 comments

Comments

@spigell
Copy link
Contributor

spigell commented Jun 1, 2019

Greetings! I try to start http service as plugin. But if main loop tries to restart server after error I got error binding for port. This is example.

local plugin = require("plugin")
local time = require("time")


local plugin_server = [[
http = require("http")
inspect = require("inspect")

s = {}

local server, err = http.server("127.0.0.1:5001")
if err then error(err) end

function s.start()
  while true do
    local req, resp = server:accept()
    print(inspect(req))

    if req then 
    
      print("received")
      resp:code(500)
      resp:done()

      error("I am broken")
    end
  end
end


s.start()
]]



server = plugin.do_string(plugin_server)
server:run()

while true do
  time.sleep(1)
  if not server:is_running() then
    print("error:", tostring(server:error()))
    server:run()
  end
end
error:	<string>:20: I am broken
stack traceback:
	[G]: in function 'error'
	<string>:20: in function 'start'
	<string>:26: in main chunk
	[G]: ?
error:	<string>:7: listen tcp 127.0.0.1:5001: bind: address already in use
stack traceback:
	[G]: in function 'error'
	<string>:7: in main chunk
	[G]: ?

Is any way to use http.server like this?
Thank you!

@vadv
Copy link
Owner

vadv commented Jun 2, 2019

good case, i will check it.
as workaround, I can suggest pcall() when processing a handler.

vadv added a commit that referenced this issue Jul 6, 2019
@vadv
Copy link
Owner

vadv commented Jul 6, 2019

in branch master now there are commits which stop the internal net.Listener on p:stop():

  if not server:is_running() then
    print("error:", tostring(server:error()))
    server:stop() -- close tcp listener
    server:run() -- now plugin bind tcp port successfully 
  end

vadv added a commit that referenced this issue Jul 6, 2019
vadv added a commit that referenced this issue Jul 6, 2019
@vadv vadv closed this as completed Jul 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants