From 5bc194c51d2c748fde7e53f8a3cc9cce63e3e591 Mon Sep 17 00:00:00 2001 From: Franciszek Koltuniuk Date: Wed, 31 May 2023 15:08:31 +0200 Subject: [PATCH 1/3] http: fix for handling on boot timers headers and request This change is a fix for handling headersTimeout and requestTimeout that causes unexpected behavior if the HTTP server is started on boot: - the connections to the server can be closed immediately with the status HTTP 408 This issue usually happens on IoT or embedded devices where the reference timestamp (returned by uv_hrtime()) is counted since boot and can be smaller than the headersTimeout or the requestTimeout value. Additionally added performance improvement to process the list of connection only if one of the timers should be processed --- src/node_http_parser.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index 7c7f6dcd974da3..763014078563a3 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -1108,9 +1108,13 @@ void ConnectionsList::Expired(const FunctionCallbackInfo& args) { const uint64_t now = uv_hrtime(); const uint64_t headers_deadline = - headers_timeout > 0 ? now - headers_timeout : 0; + (headers_timeout > 0 && now > headers_timeout) ? now - headers_timeout : 0; const uint64_t request_deadline = - request_timeout > 0 ? now - request_timeout : 0; + (request_timeout > 0 && now > request_timeout) ? now - request_timeout : 0; + + if (headers_deadline == 0 && request_deadline == 0) { + return args.GetReturnValue().Set(Array::New(isolate, 0)); + } auto iter = list->active_connections_.begin(); auto end = list->active_connections_.end(); From cd272a60ae2a639332233fba348eb0a7e895ff2e Mon Sep 17 00:00:00 2001 From: Franciszek Koltuniuk Date: Wed, 7 Jun 2023 16:20:21 +0200 Subject: [PATCH 2/3] update after autochecks --- src/node_http_parser.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index 763014078563a3..1d5404372b3130 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -1108,9 +1108,11 @@ void ConnectionsList::Expired(const FunctionCallbackInfo& args) { const uint64_t now = uv_hrtime(); const uint64_t headers_deadline = - (headers_timeout > 0 && now > headers_timeout) ? now - headers_timeout : 0; + (headers_timeout > 0 && now > headers_timeout) ? now - headers_timeout + : 0; const uint64_t request_deadline = - (request_timeout > 0 && now > request_timeout) ? now - request_timeout : 0; + (request_timeout > 0 && now > request_timeout) ? now - request_timeout + : 0; if (headers_deadline == 0 && request_deadline == 0) { return args.GetReturnValue().Set(Array::New(isolate, 0)); From fc3c8548dfe5298dd715c22e8de4637c8535401d Mon Sep 17 00:00:00 2001 From: Franciszek Koltuniuk Date: Wed, 7 Jun 2023 16:20:21 +0200 Subject: [PATCH 3/3] update in sourcode: commented the change --- src/node_http_parser.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index 1d5404372b3130..e1944d90557316 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -1106,6 +1106,11 @@ void ConnectionsList::Expired(const FunctionCallbackInfo& args) { std::swap(headers_timeout, request_timeout); } + // On IoT or embedded devices the uv_hrtime() may return the timestamp + // that is smaller than configured timeout for headers or request + // to prevent subtracting two unsigned integers + // that can yield incorrect results we should check + // if the 'now' is bigger than the timeout for headers or request const uint64_t now = uv_hrtime(); const uint64_t headers_deadline = (headers_timeout > 0 && now > headers_timeout) ? now - headers_timeout