Better stability

Our production servers kept going down, I finally found the reason: sometimes the connections not closed.

This fix keeps the server stable now.

Also added an error handler for rare cases.