From: Remi Gacogne Date: Mon, 28 Apr 2025 10:47:39 +0000 (+0200) Subject: dnsdist: Gracefully handle timeout/response for a closed HTTP stream X-Git-Tag: dnsdist-2.0.0-alpha2~46^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F15481%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Gracefully handle timeout/response for a closed HTTP stream The remote end might very well have already closed the HTTP stream corresponding to the timeout or response we are processing. While this means we need to discard the event we were processing, it is not an unexpected event and we should thus not raise an exception since the caller cannot do anything about it. --- diff --git a/pdns/dnsdistdist/dnsdist-nghttp2-in.cc b/pdns/dnsdistdist/dnsdist-nghttp2-in.cc index 5ecc687f50..26f6d881bc 100644 --- a/pdns/dnsdistdist/dnsdist-nghttp2-in.cc +++ b/pdns/dnsdistdist/dnsdist-nghttp2-in.cc @@ -558,7 +558,12 @@ IOState IncomingHTTP2Connection::sendResponse(const struct timeval& now, TCPResp if (response.d_idstate.d_streamID == -1) { throw std::runtime_error("Invalid DoH stream ID while sending response"); } - auto& context = d_currentStreams.at(response.d_idstate.d_streamID); + auto streamIt = d_currentStreams.find(response.d_idstate.d_streamID); + if (streamIt == d_currentStreams.end()) { + /* it might have been closed by the remote end in the meantime */ + return hasPendingWrite() ? IOState::NeedWrite : IOState::Done; + } + auto& context = streamIt->second; uint32_t statusCode = 200U; std::string contentType; @@ -593,7 +598,12 @@ void IncomingHTTP2Connection::notifyIOError(const struct timeval& now, TCPRespon throw std::runtime_error("Invalid DoH stream ID while handling I/O error notification"); } - auto& context = d_currentStreams.at(response.d_idstate.d_streamID); + auto streamIt = d_currentStreams.find(response.d_idstate.d_streamID); + if (streamIt == d_currentStreams.end()) { + /* it might have been closed by the remote end in the meantime */ + return; + } + auto& context = streamIt->second; context.d_buffer = std::move(response.d_buffer); sendResponse(response.d_idstate.d_streamID, context, 502, d_ci.cs->dohFrontend->d_customResponseHeaders); }