@@ -352,29 +352,20 @@ void HttpClient::MaybeSpawnBackgroundThread()
352352 int still_running = 1 ;
353353 std::chrono::system_clock::time_point last_free_job_timepoint =
354354 std::chrono::system_clock::now ();
355+ bool need_wait_more = false ;
355356 while (true )
356357 {
357358 CURLMsg *msg;
358359 int queued;
359360 CURLMcode mc = curl_multi_perform (self->multi_handle_ , &still_running);
360361
361- std::chrono::milliseconds wait_for;
362- #if LIBCURL_VERSION_NUM >= 0x074200
363- // only avaliable with curl_multi_poll
364- wait_for = self->background_thread_wait_for_ ;
365- #endif
366- if (self->is_shutdown .load (std::memory_order_acquire))
367- {
368- wait_for = std::chrono::milliseconds{0 };
369- }
370-
371362 // According to https://curl.se/libcurl/c/curl_multi_perform.html, when mc is not OK, we
372363 // can not curl_multi_perform it again
373364 if (mc != CURLM_OK)
374365 {
375366 self->resetMultiHandle ();
376367 }
377- else if (still_running || now - last_free_job_timepoint < wait_for )
368+ else if (still_running || need_wait_more )
378369 {
379370 // curl_multi_poll is added from libcurl 7.66.0, before 7.68.0, we can only wait util
380371 // timeout to do the rest jobs
@@ -433,13 +424,31 @@ void HttpClient::MaybeSpawnBackgroundThread()
433424 still_running = 1 ;
434425 }
435426
427+ std::chrono::system_clock::time_point now = std::chrono::system_clock::now ();
436428 if (still_running > 0 )
437429 {
438430 last_free_job_timepoint = now;
431+ need_wait_more = false ;
432+ continue ;
433+ }
434+
435+ std::chrono::milliseconds wait_for;
436+ #if LIBCURL_VERSION_NUM >= 0x074200
437+ // only avaliable with curl_multi_poll
438+ wait_for = self->background_thread_wait_for_ ;
439+ #endif
440+ if (self->is_shutdown .load (std::memory_order_acquire))
441+ {
442+ wait_for = std::chrono::milliseconds{0 };
443+ }
444+
445+ if (now - last_free_job_timepoint < wait_for)
446+ {
447+ need_wait_more = true ;
439448 continue ;
440449 }
441450
442- if (still_running == 0 && now - last_free_job_timepoint > wait_for )
451+ if (still_running == 0 )
443452 {
444453 std::lock_guard<std::mutex> lock_guard{self->background_thread_m_ };
445454 // Double check, make sure no more pending sessions after locking background thread
0 commit comments