From e4d829b6c975eaa955363791cf884777dec34c24 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 29 May 2017 21:25:35 +0200 Subject: [PATCH 1/3] Improve Connection Close handling close connection on coroutine errors and improve logging around closieg connections. Release memory warlier --- httpserver.lua | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/httpserver.lua b/httpserver.lua index 681b3af..b85579b 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -31,15 +31,23 @@ return function (port) fileServeFunction(bufferedConnection, req, args) -- The bufferedConnection may still hold some data that hasn't been sent. Flush it before closing. if not bufferedConnection:flush() then + log(connection, "closing connetion", "no (more) data") connection:close() connectionThread = nil + collectgarbage() end end) local BufferedConnectionClass = dofile("httpserver-connection.lc") local bufferedConnection = BufferedConnectionClass:new(connection) local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args) - if not status then log(connection, "Error: "..err) end + if not status then + log(connection, "Error: "..err) + log(connection, "closing connetion", "error") + connection:close() + connectionThread = nil + collectgarbage() + end end local function handleRequest(connection, req) @@ -139,16 +147,25 @@ return function (port) if connectionThreadStatus == "suspended" then -- Not finished sending file, resume. local status, err = coroutine.resume(connectionThread) - if not status then log(connection:getpeer(), "Error: " .. err) end + if not status then + log(connection, "Error: "..err) + log(connection, "closing connetion", "error") + connection:close() + connectionThread = nil + collectgarbage() + end elseif connectionThreadStatus == "dead" then -- We're done sending file. + log(connection, "closing connetion","thread is dead") connection:close() connectionThread = nil + collectgarbage() end end end local function onDisconnect(connection, payload) + print("disconnected") if connectionThread then connectionThread = nil collectgarbage() From f24d6e908b1a38236ee694e0bc781de6fd970dba Mon Sep 17 00:00:00 2001 From: Gregor Date: Thu, 1 Jun 2017 04:28:52 +0200 Subject: [PATCH 2/3] Fix typo --- httpserver.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httpserver.lua b/httpserver.lua index 16bef15..0e1f16c 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -31,7 +31,7 @@ return function (port) fileServeFunction(bufferedConnection, req, args) -- The bufferedConnection may still hold some data that hasn't been sent. Flush it before closing. if not bufferedConnection:flush() then - log(connection, "closing connetion", "no (more) data") + log(connection, "closing connection", "no (more) data") connection:close() connectionThread = nil collectgarbage() @@ -43,7 +43,7 @@ return function (port) local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args) if not status then log(connection, "Error: "..err) - log(connection, "closing connetion", "error") + log(connection, "closing connection", "error") connection:close() connectionThread = nil collectgarbage() @@ -149,14 +149,14 @@ return function (port) local status, err = coroutine.resume(connectionThread) if not status then log(connection, "Error: "..err) - log(connection, "closing connetion", "error") + log(connection, "closing connection", "error") connection:close() connectionThread = nil collectgarbage() end elseif connectionThreadStatus == "dead" then -- We're done sending file. - log(connection, "closing connetion","thread is dead") + log(connection, "closing connection","thread is dead") connection:close() connectionThread = nil collectgarbage() From 122bb889c9cc5ffef5e1e9c2a853560271968c96 Mon Sep 17 00:00:00 2001 From: Gregor Date: Thu, 1 Jun 2017 04:31:10 +0200 Subject: [PATCH 3/3] comment out print statement --- httpserver.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httpserver.lua b/httpserver.lua index 0e1f16c..18cba4b 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -165,7 +165,8 @@ return function (port) end local function onDisconnect(connection, payload) - print("disconnected") +-- this should rather be a log call, but log is not available here +-- print("disconnected") if connectionThread then connectionThread = nil collectgarbage()