Skip to content

Commit ee555b0

Browse files
authored
Fix NetworkWinHttp error for pending requests. (#1052)
When network destroyed, all pending requests callbacks should receive OFFLINE_ERROR. To process them correctly, we need to stop CompletionThread before closing WinHttp handles. Otherwise, it will treat all pending requests as cancelled. Resolves: OLPEDGE-2247 Signed-off-by: Kostiantyn Zvieriev <ext-kostiantyn.zvieriev@here.com>
1 parent f2fea13 commit ee555b0

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

olp-cpp-sdk-core/src/http/winhttp/NetworkWinHttp.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ constexpr auto kLogTag = "WinHttp";
238238

239239
NetworkWinHttp::NetworkWinHttp(size_t max_request_count)
240240
: http_requests_(max_request_count),
241+
run_completion_thread_(true),
241242
http_session_(NULL),
242243
thread_(INVALID_HANDLE_VALUE),
243244
event_(INVALID_HANDLE_VALUE) {
@@ -272,6 +273,10 @@ NetworkWinHttp::NetworkWinHttp(size_t max_request_count)
272273
NetworkWinHttp::~NetworkWinHttp() {
273274
OLP_SDK_LOG_TRACE(kLogTag, "Destroying NetworkWinHttp, this=" << this);
274275

276+
// we should stop completion thread before closing all handles or they will be
277+
// processed as cancelled instead OFFLINE_ERROR
278+
run_completion_thread_ = false;
279+
275280
std::vector<std::shared_ptr<ResultData>> pending_results;
276281
{
277282
std::unique_lock<std::recursive_mutex> lock(mutex_);
@@ -892,14 +897,14 @@ NetworkWinHttp::Run(LPVOID arg) {
892897
}
893898

894899
void NetworkWinHttp::CompletionThread() {
895-
while (http_session_) {
900+
while (run_completion_thread_) {
896901
std::shared_ptr<ResultData> result;
897902
{
898-
if (http_session_ && results_.empty()) {
903+
if (run_completion_thread_ && results_.empty()) {
899904
WaitForSingleObject(event_, 30000); // Wait max 30 seconds
900905
ResetEvent(event_);
901906
}
902-
if (!http_session_) {
907+
if (!run_completion_thread_) {
903908
continue;
904909
}
905910

@@ -910,7 +915,7 @@ void NetworkWinHttp::CompletionThread() {
910915
}
911916
}
912917

913-
if (http_session_ && result) {
918+
if (run_completion_thread_ && result) {
914919
std::string str;
915920
int status;
916921
if (result->offset == 0 &&
@@ -964,7 +969,7 @@ void NetworkWinHttp::CompletionThread() {
964969
}
965970
}
966971

967-
if (http_session_ && !http_connections_.empty()) {
972+
if (run_completion_thread_ && !http_connections_.empty()) {
968973
// Check for timeouted connections
969974
std::unique_lock<std::recursive_mutex> lock(mutex_);
970975
std::vector<std::wstring> closed;

olp-cpp-sdk-core/src/http/winhttp/NetworkWinHttp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class NetworkWinHttp : public Network {
141141
std::vector<RequestData> http_requests_;
142142
std::queue<std::shared_ptr<ResultData>> results_;
143143

144+
bool run_completion_thread_;
144145
HINTERNET http_session_;
145146
HANDLE thread_;
146147
HANDLE event_;

0 commit comments

Comments
 (0)