From 37bbf6ade49a01eb8feeb47b3aba254aea28ade4 Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Fri, 24 Dec 2021 13:34:03 +1300 Subject: [PATCH] server: handle ECONNRESET in http connections In real deployments, sometimes http connections break with ECONNRESET. This causes the kj::HttpServer listener promise to break, which means no more connections are accepted. Catch this exception and restart the listener. resolves #164 --- src/server.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/server.cpp b/src/server.cpp index 2109017..e1792f3 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -137,6 +137,14 @@ void Server::listenHttp(Http &http, kj::StringPtr httpBindAddress) if(httpBindAddress.startsWith("unix:")) chmod(httpBindAddress.slice(strlen("unix:")).cStr(), 0660); return http.startServer(ioContext.lowLevelProvider->getTimer(), kj::mv(listener)); + }).catch_([this,&http,httpBindAddress](kj::Exception&&e) mutable -> kj::Promise { + if(e.getType() == kj::Exception::Type::DISCONNECTED) { + LLOG(ERROR, "HTTP disconnect, restarting server", e.getDescription()); + listenHttp(http, httpBindAddress); + return kj::READY_NOW; + } + // otherwise propagate the exception + return kj::mv(e); })); }