Is there some situation that makes curl_multi_perform block?

Are there any situations that make curl_multi_perform blocked?
I am using the curl_multi interface to build an https service, but we found that the curl_multi_perform interface will block in weak network conditions.
Is there any call inside the interface that is non-asynchronous? Caused the blockage.

I want to know some reason.

New contributor

LeoN Y is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Yes, there are certain conditions under which curl_multi_perform can block, especially in weak network conditions. Here are a few potential reasons for this:

  1. DNS Resolution: If the DNS resolution takes a long time, it can cause blocking. By default, cURL performs synchronous DNS resolution, which can block the entire process if the DNS lookup is slow. You can mitigate this by using cURL with asynchronous DNS resolution, such as c-ares or similar.

  2. SSL/TLS Handshake: The SSL/TLS handshake process, which is necessary for establishing secure connections over HTTPS, can be time-consuming, especially over weak network connections. This process might block if it takes too long.

  3. Network Timeouts and Retries: In weak network conditions, packet loss and high latency can lead to repeated retries and extended timeouts, which might cause curl_multi_perform to appear blocked.

  4. Blocking Callbacks: If any of the callbacks (like the write, read, or progress callbacks) registered with cURL perform blocking operations themselves, they can cause curl_multi_perform to block.

  5. Socket Operations: While curl_multi_perform is designed to be non-blocking, the underlying socket operations might block under certain conditions, particularly if the operating system’s network stack or the server is not responsive.

To address these issues, you can:

  • Use Asynchronous DNS: Enable asynchronous DNS with c-ares. This can be done by building cURL with c-ares support and using the appropriate cURL options to enable it.

  • Tune Timeouts: Set appropriate timeouts for various stages of the connection process using options like CURLOPT_TIMEOUT, CURLOPT_CONNECTTIMEOUT, and CURLOPT_DNS_SERVERS.

  • Use Non-blocking Sockets: Ensure that sockets are set to non-blocking mode. cURL typically handles this, but it’s good to double-check.

  • Monitor and Debug: Use verbose mode (CURLOPT_VERBOSE) to get detailed logs of what cURL is doing. This can help you pinpoint where the blocking occurs.

Here is an example of how you can set some of these options:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CURLM *multi_handle;
CURL *easy_handle;
multi_handle = curl_multi_init();
easy_handle = curl_easy_init();
curl_easy_setopt(easy_handle, CURLOPT_URL, "https://example.com");
curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, 30L);
curl_easy_setopt(easy_handle, CURLOPT_CONNECTTIMEOUT, 10L);
curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1L);
curl_multi_add_handle(multi_handle, easy_handle);
int still_running;
do {
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
if (mc == CURLM_OK) {
int numfds;
mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
if (mc != CURLM_OK) {
fprintf(stderr, "curl_multi_wait() failed, code %d.n", mc);
break;
}
if (!numfds) {
fprintf(stderr, "No file descriptors ready.n");
usleep(100000); // Sleep for a bit to avoid busy-looping
}
} else {
fprintf(stderr, "curl_multi_perform() failed, code %d.n", mc);
break;
}
} while (still_running);
curl_multi_cleanup(multi_handle);
curl_easy_cleanup(easy_handle);
</code>
<code>CURLM *multi_handle; CURL *easy_handle; multi_handle = curl_multi_init(); easy_handle = curl_easy_init(); curl_easy_setopt(easy_handle, CURLOPT_URL, "https://example.com"); curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, 30L); curl_easy_setopt(easy_handle, CURLOPT_CONNECTTIMEOUT, 10L); curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1L); curl_multi_add_handle(multi_handle, easy_handle); int still_running; do { CURLMcode mc = curl_multi_perform(multi_handle, &still_running); if (mc == CURLM_OK) { int numfds; mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds); if (mc != CURLM_OK) { fprintf(stderr, "curl_multi_wait() failed, code %d.n", mc); break; } if (!numfds) { fprintf(stderr, "No file descriptors ready.n"); usleep(100000); // Sleep for a bit to avoid busy-looping } } else { fprintf(stderr, "curl_multi_perform() failed, code %d.n", mc); break; } } while (still_running); curl_multi_cleanup(multi_handle); curl_easy_cleanup(easy_handle); </code>
CURLM *multi_handle;
CURL *easy_handle;

multi_handle = curl_multi_init();
easy_handle = curl_easy_init();

curl_easy_setopt(easy_handle, CURLOPT_URL, "https://example.com");
curl_easy_setopt(easy_handle, CURLOPT_TIMEOUT, 30L);
curl_easy_setopt(easy_handle, CURLOPT_CONNECTTIMEOUT, 10L);
curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1L);

curl_multi_add_handle(multi_handle, easy_handle);

int still_running;
do {
    CURLMcode mc = curl_multi_perform(multi_handle, &still_running);

    if (mc == CURLM_OK) {
        int numfds;
        mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);

        if (mc != CURLM_OK) {
            fprintf(stderr, "curl_multi_wait() failed, code %d.n", mc);
            break;
        }

        if (!numfds) {
            fprintf(stderr, "No file descriptors ready.n");
            usleep(100000); // Sleep for a bit to avoid busy-looping
        }
    } else {
        fprintf(stderr, "curl_multi_perform() failed, code %d.n", mc);
        break;
    }
} while (still_running);

curl_multi_cleanup(multi_handle);
curl_easy_cleanup(easy_handle);

This example sets a timeout for the overall request and the connection phase, and also uses curl_multi_wait to efficiently wait for activity on the file descriptors. Adjusting these parameters can help mitigate blocking issues in weak network conditions.

New contributor

Amir hossein Mirfallahi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật