Skip to content

Commit 1dab37d

Browse files
franciszek-koltuniuk-redCeres6
authored andcommittedAug 14, 2023
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 PR-URL: nodejs#48291 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 25ff96d commit 1dab37d

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed
 

‎src/node_http_parser.cc

+13-2
Original file line numberDiff line numberDiff line change
@@ -1106,11 +1106,22 @@ void ConnectionsList::Expired(const FunctionCallbackInfo<Value>& args) {
11061106
std::swap(headers_timeout, request_timeout);
11071107
}
11081108

1109+
// On IoT or embedded devices the uv_hrtime() may return the timestamp
1110+
// that is smaller than configured timeout for headers or request
1111+
// to prevent subtracting two unsigned integers
1112+
// that can yield incorrect results we should check
1113+
// if the 'now' is bigger than the timeout for headers or request
11091114
const uint64_t now = uv_hrtime();
11101115
const uint64_t headers_deadline =
1111-
headers_timeout > 0 ? now - headers_timeout : 0;
1116+
(headers_timeout > 0 && now > headers_timeout) ? now - headers_timeout
1117+
: 0;
11121118
const uint64_t request_deadline =
1113-
request_timeout > 0 ? now - request_timeout : 0;
1119+
(request_timeout > 0 && now > request_timeout) ? now - request_timeout
1120+
: 0;
1121+
1122+
if (headers_deadline == 0 && request_deadline == 0) {
1123+
return args.GetReturnValue().Set(Array::New(isolate, 0));
1124+
}
11141125

11151126
auto iter = list->active_connections_.begin();
11161127
auto end = list->active_connections_.end();

0 commit comments

Comments
 (0)
Please sign in to comment.