Neopallium/lua-handlers — pull request #5 by terminar (2013-10-24 13:10:39+00:00)
Title: problem with keepalive-closing socket

I had some really curious problem on two clients using a lua-handlers server.
Every 25 http request when ~38kb data had to be delivered to the client, the client had a socket error: connection unexpectedly closed.

I first tracked it down to hconnection.lua/send_body() ~ line 355 where the sock:send(chunk) call is done.
Instead of err=="blocked" i got err=="closed" on these two clients.

Later trying to fix it i found that due to the entry in hconnection.lua/on_headers_complete ~line 430

max_requests <= 0 then self.need_close = true
line

the socket will be self.sock:shutdown in hconnection.lua/on_message_complete~line 481.

Now the main problem happens at connection.lua/sock_recv_data(self) ~ 255.
The socket is :shutdown() half the way and sock:recv is called in the repeat.
The next evaluation of err will tell that the socket has shut down and an error is handled instead of just sending the next data and then close the socket.

I tried to create a patch here in the branch checking for self.shutdown_waiting (which is now set on the self.sock:shutdown(...) line.

It works now but i am not sure if this is the best solution for the problem.

It is really really hard to reproduce the problem but it exists.