Skip to content

Commit a09b4b4

Browse files
authored
Merge pull request marcoskirsch#91 from HHHartmann/BetterLoggingAndMoreCloseConnections
Improve logging and cleanup when ConnectionThread failes
2 parents b6b3fec + 122bb88 commit a09b4b4

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

httpserver.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@ return function (port)
3131
fileServeFunction(bufferedConnection, req, args)
3232
-- The bufferedConnection may still hold some data that hasn't been sent. Flush it before closing.
3333
if not bufferedConnection:flush() then
34+
log(connection, "closing connection", "no (more) data")
3435
connection:close()
3536
connectionThread = nil
37+
collectgarbage()
3638
end
3739
end)
3840

3941
local BufferedConnectionClass = dofile("httpserver-connection.lc")
4042
local bufferedConnection = BufferedConnectionClass:new(connection)
4143
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
42-
if not status then log(connection, "Error: "..err) end
44+
if not status then
45+
log(connection, "Error: "..err)
46+
log(connection, "closing connection", "error")
47+
connection:close()
48+
connectionThread = nil
49+
collectgarbage()
50+
end
4351
end
4452

4553
local function handleRequest(connection, req)
@@ -139,16 +147,26 @@ return function (port)
139147
if connectionThreadStatus == "suspended" then
140148
-- Not finished sending file, resume.
141149
local status, err = coroutine.resume(connectionThread)
142-
if not status then log(connection:getpeer(), "Error: " .. err) end
150+
if not status then
151+
log(connection, "Error: "..err)
152+
log(connection, "closing connection", "error")
153+
connection:close()
154+
connectionThread = nil
155+
collectgarbage()
156+
end
143157
elseif connectionThreadStatus == "dead" then
144158
-- We're done sending file.
159+
log(connection, "closing connection","thread is dead")
145160
connection:close()
146161
connectionThread = nil
162+
collectgarbage()
147163
end
148164
end
149165
end
150166

151167
local function onDisconnect(connection, payload)
168+
-- this should rather be a log call, but log is not available here
169+
-- print("disconnected")
152170
if connectionThread then
153171
connectionThread = nil
154172
collectgarbage()

0 commit comments

Comments
 (0)